0

Tip calculator question ?methods?

so first i came up with: bill = int(input()) ammountwithtip=bill*1.2 print(ammountwithtip-bill) but when the ammount is 268, it prints 53.599999999999966 . Can sy explain? I figured out , that when i write this: bill = int(input()) print(bill*20/100) I get normal outputs. Whats the difference?

17th Jan 2023, 7:05 PM
Dávid Erk
Dávid Erk - avatar
2 Respostas
+ 2
Floating-point values, which have a decimal point, can always be tricky to calculate with. A float value is stored on a fixed size memory location. Even though floats can represent very tiny and very large numbers, but their precision is limited. Python can store about 17 decimal digits. But even if it displays a nice looking number like 53.6, still usually in the background there is some rounding happening, because internally the computer stores bits (0s and 1s) which do not precisely translate to decimal value. Some more explanation: https://docs.python.org/3/tutorial/floatingpoint.html
17th Jan 2023, 7:24 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thank you!
17th Jan 2023, 7:25 PM
Dávid Erk
Dávid Erk - avatar