add two double given wrong result

前端 未结 5 1155
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 10:33

I\'m using the following piece of code and under some mysterious circumstances the result of the addition is not as it\'s supposed to be:

double _west = 9.482935         


        
5条回答
  •  死守一世寂寞
    2021-01-22 11:14

    You cannot represent every floating point number in the decimal system with a floating point in a binary system accurately, this isn't even directly related to how "small" the decimal number is, some numbers just don't "fit" in base-2 nicely.

    Using a longer bit width can help in most cases, but not always.

    To specify your constants in Decimal (128-bit floating point ) precision, use this declaration:

    decimal _west = 9.482935905456543m;
    decimal _off = 0.00000093248155508263153m;
    decimal _lon = _west + _off;
    

提交回复
热议问题