Casting result double to int is wrong

There seems to be some kind of obscure rounding error when I run the following code:

int roundedTotal = (int)(PriorityJob * 100.0);

Initially PriorityJob = 1.4and roundedTotal- undefined. Assessment PriorityJob * 100.0at this point gives 140. Then roundedTotal = 139.

Apparently, 140.0 is interpreted as 139.99999. Is this a flaw in the floating point engine? I have never seen anything like it.

+5
source share
1 answer

Almost every modern computer uses a binary representation for floating point numbers.

, 1/3 = 0.33333333... , 1/10 (, , , 1.4), . , , "" .

: (int)(PriorityJob * 100.0 + 0.5)

+10

All Articles