Why does 1.0f + 0.0000000171785715f return 1f?

An hour later, trying to find an error in my code, I finally found the reason. I tried to add a very small float in 1f, but nothing happened. When trying to figure out why I found that adding a small float to 0f works fine.

Why is this happening? Is this related to "order of magnitude"? Is there any workaround?

Thanks in advance.

Edit:

Switching to double precision or decimal is not currently an option.

+3
source share
8 answers

(32 ) 7 . , , , , , 1. , . 1, ... .

double, . , , .

EDIT:. , double , Kahan, akuhn .

, float . , , , .

+21

+7

, , , , , , .

, , 0, , 0, .

.

+4

, - . , , decimal. .

+3
+3

( double), / . float , (, 2 ^ 16). . , .

, .

+2

f , . , .

0

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


All Articles