incorrect value after casting double to a float

后端 未结 2 1305
北荒
北荒 2021-01-22 03:16

I have some C code performing high precision arithmetic compiled by gcc(gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)).The final result of the calculation is a double which has a

2条回答
  •  盖世英雄少女心
    2021-01-22 03:57

    A float has much less precision than a double; you lose about half the digits. So at best you'd be seeing the 622.0799 portion (rounded up to 622.0800). The difference you see is probably caused by the rounding mode in use.

    Here are the actual numbers:

    • As double: 622.0799999586118929073563776910305023193359375
    • As float: 622.08001708984375 (internal representation: 1142654239)
    • Immediately-preceding float: 622.0799560546875 (internal representation: 1142654238)

    The internal representations are values generated using Java's Float.floatToIntBits. You can also use Float.intBitsToFloat to get back a floating-point number.

提交回复
热议问题