c++ incorrect floating point arithmetic

前端 未结 3 1050
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 13:07

For the following program:

#include 
#include 
using namespace std;
int main()
{
    for (float a = 1.0; a < 10; a++)
              


        
3条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 13:38

    First of all, since your code does 1.0/a, it gives you double (1.0 is a double value, 1.0f is float) as the rules of C++ (and C) always extends a smaller type to the larger one if the operands of an operation is different size (so, int + char makes the char into an int before adding the values, long + int will make the int long, etc, etc).

    Second floating point values have a set number of bits for the "number". In float, that is 23 bits (+ 1 'hidden' bit), and in double it's 52 bits (+1). Yet get approximately 3 digits per bit (exactly: log2(10), if we use decimal number representation), so a 23 bit number gives approximately 7-8 digits, a 53 bit number approximately 16-17 digits. The remainder is just "noise" caused by the last few bits of the number not evening out when converting to a decimal number.

    To have infinite precision, we would have to either store the value as a fraction, or have an infinite number of bits. And of course, we could have some other finite precision, such as 100 bits, but I'm sure you'd complain about that too, because it would just have another 15 or so digits before it "goes wrong".

提交回复
热议问题