制作一个小的C ++大型精度类,一切似乎工作得体,但添加,如果我一起添加0xffffffff和0x04我得到0xffff0003当我应该得到0x0100000003。这是问题的功能:
mpfl operator+(const mpfl &lhs, const mpfl &rhs)
{
unsigned long i;
mpfl ret(0);
mpfl trhs(rhs);
for (i = lhs.nbytes; i >= 0; i--)
{
if (
(unsigned short)lhs.data[i].data + (unsigned short)trhs.data[i].data
> (unsigned short)255
) {
if (i > 0)
{
ret.data[i].carry = 1;
ret.data[0].carry = 0;
}
else
{
ret.data[0].carry = 1;
}
}
else
ret.data[i].carry = 0;
ret.data[i].data = lhs.data[i].data + trhs.data[i].data;
if (i < lhs.nbytes)
{
if (ret.data[i].data == 255 && ret.data[i + 1].carry == 1)
increment(&trhs, i + 1);
ret.data[i].data += ret.data[i + 1].carry;
}
if (i == 0) break;
}
return ret;
}
这里是完整源代码的链接(github使这更容易,因为它有很多)
标题:https://github.com/phyrrus9/mpfl/blob/master/mpfl.h
class:https://github.com/phyrrus9/mpfl/blob/master/mpfl.cpp
驱动程序:https://github.com/phyrrus9/mpfl/blob/master/main.cpp
小唯快跑啊
慕莱坞森