在哪里可以找到免费或开源的C ++库进行BCD数学运算?

问题描述

|                                                                                                                   关闭。这个问题是题外话。它当前不接受答案。                                                      

解决方法

        干得好。我只是写了这个,并且正在将其设为公共领域。 它将无符号的bcd转换为无符号的int,反之亦然。使用bcd2i()将BCD转换为无符号整数,进行所需的任何数学运算,然后使用i2bcd()将数字带回BCD。
unsigned int bcd2i(unsigned int bcd) {
    unsigned int decimalMultiplier = 1;
    unsigned int digit;
    unsigned int i = 0;
    while (bcd > 0) {
        digit = bcd & 0xF;
        i += digit * decimalMultiplier;
        decimalMultiplier *= 10;
        bcd >>= 4;
    }
    return i;
}

unsigned int i2bcd(unsigned int i) {
    unsigned int binaryShift = 0;  
    unsigned int digit;
    unsigned int bcd = 0;
    while (i > 0) {
        digit = i % 10;
        bcd += (digit << binaryShift);
        binaryShift += 4;
        i /= 10;
    }
    return bcd;
}
// Thanks to EmbeddedGuy for bug fix: changed init value to 0 from 1 


#include <iostream>
using namespace std;

int main() {
int tests[] = {81986,3740,103141,27616,1038,56975,38083,26722,72358,2017,34259};

int testCount = sizeof(tests)/sizeof(tests[0]);

cout << \"Testing bcd2i(i2bcd(test)) on 10 cases\" << endl;
for (int testIndex=0; testIndex<testCount; testIndex++) {
    int bcd = i2bcd(tests[testIndex]);
    int i = bcd2i(bcd);
    if (i != tests[testIndex]) {
        cout << \"Test failed: \" << tests[testIndex] << \" >> \" << bcd << \" >> \" << i << endl;
        return 1;
    }
}
cout << \"Test passed\" << endl;
return 0;
}
    ,        据我所知,转换错误并不总是可以接受的。由于无法避免错误,因此有时必须进行BCD计算。例如,XBCD_Math是功能齐全的BCD浮点库。     ,        数学是数学-没关系,您是在2底,10底还是16底进行加法或乘法运算,答案始终相同。 我不知道如何对输入和输出进行编码,但是您所需要做的就是将BCD转换为整数,像通常一样进行数学运算,最后从整数转换为BCD。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...