+ 1

How come the answer to the code below is different from the actual math?

Code: print((456/10**2)*(10**2)) Using python the out put is equal (455.99999999999994) But using the actual math it should be exactly (456) Please guys can you explain why their is a difference

23rd Jul 2022, 5:49 PM
No One
5 Answers
+ 2
Hi, No One ! There’s a lot to read about this on the web. Try for example to start with this link: https://www.geeksforgeeks.org/floating-point-error-in-JUMP_LINK__&&__python__&&__JUMP_LINK/amp/
23rd Jul 2022, 6:53 PM
Per Bratthammar
Per Bratthammar - avatar
+ 2
Hi, againNo One ! print(int(456/10**2) * (10**2)) This is : = int(4.56) * 100 = 4 * 100 = 400
23rd Jul 2022, 8:45 PM
Per Bratthammar
Per Bratthammar - avatar
+ 1
Thank you But I also have another question which is when I tried to change it to integer like this:- print(int(456/10**2)*(10**2)) It outputs only 400 but why not 456 or 455 after all they are also integer like 400
23rd Jul 2022, 7:04 PM
No One
+ 1
Python performs calculations in binary system. Try dividing in binary and convert to decimal.
25th Jul 2022, 4:55 PM
Chistotin Igor
Chistotin Igor - avatar
0
Thank you 😊
24th Jul 2022, 3:57 AM
No One