Why is it like this?

Hello.
I have some following line of codes…

print(((0.08 * 500 * 5) * 45) / 100)
print(((0.08 * 500 * 5) / 100) * 45)

print(((0.08 * 500 * 6) * 45) / 100)
print(((0.08 * 500 * 6) / 100) * 45)

print(((0.08 * 500 * 7) * 45) / 100)
print(((0.08 * 500 * 7) / 100) * 45)

print(((0.08 * 500 * 8) * 45) / 100)
print(((0.08 * 500 * 8) / 100) * 45)

print(((0.08 * 500 * 9) * 45) / 100)
print(((0.08 * 500 * 9) / 100) * 45)

print(((0.08 * 500 * 17) * 45) / 100)
print(((0.08 * 500 * 17) / 100) * 45)

print(((0.08 * 500 * 19) * 45) / 100)
print(((0.08 * 500 * 19) / 100) * 45)

OUTPUTS:

90.0
90.0
108.0
108.0
126.0
125.99999999999999
144.0
144.0
162.0
162.0
306.0
306.0
342.0
342.0

================================
Why do I get this Output (125.99999999999999) for the line marked as italics

This link might be helpful.

Have you ever used a scientific calculator? And you’ve done some calculation and got 7.99999999999 or 8.000000000001 instead of 8?

Its the same thing.

See:

https://www.startpage.com/sp/search?query=floating+point+so+inaccurate

1 Like