Reputation: 128
I was doing some coding and suddenly wondered with a strange behavior of floor(). The piece of line that caused error is mentioned below:
printf("%f",floor(310.96*100));
and the output was 31095.0000
.
Why is this happening?
Upvotes: 1
Views: 82
Reputation: 25166
This is a typical floating point issue. The constant value 310.96
is not equally representable as a float number. Instead the closest float value representation is 310.9599914550781
.
You can try out your self here. Multipled that by 100
and truncated with floor()
results in your 31095.0000
Upvotes: 1
Reputation: 20764
Floating point numbers are not 100% exact 310.96*100 might result in 31095.99999999... hence your result, see also this
Upvotes: 0