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?
2 Antworten
+ 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
+ 1
Thank you!