Float cast reduces the value by 1

When issuing (float) 33554329L, the result is 33554328. If the number then returns to the length, the value remains at 33554328, is there any explanation for this.

Using VS2005 in C ++ [not controlled]

+3
source share
4 answers

The 32-bit float has 23 bits for the mantissa, which are 8,388,608 different values. This means that the accuracy is about 7 significant decimal digits. Your number has 8 decimal significant digits, so you see a loss of precision in the last significant digit.
Here's more info on the float view.

- 64 52 , 4 503 599 627 370 496 (16- ) , , 15-16 .

A decimal - , . # , , , ++. , , ++.

+7

(3 +) .

0-1. , . , , ( , ).

: ( ).

+2
source

The accuracy of the representation of various floating-point types depends on their size. For a 32-bit float, you can expect about 7 digits of accuracy. For double, it's about 16 digits.

I highly recommend reading floating point messages and various advantages and disadvantages. This will save you a lot of trouble in the long run, especially if things like comparisons don't work as you expect.

+1
source

Source: https://habr.com/ru/post/1792454/


All Articles