Dec-27-2021, 12:07 PM
(Dec-27-2021, 10:30 AM)Jeff900 Wrote: It might be explainable, maybe even mathematical, but I still can't figure out why this happens. Someone has an idea?Basic answer about Floating-Point arithmetic.
Floating Point Arithmetic: Issues and Limitations
In most cases it doesn't matter as it close enough,
there is decimal that can help eg if doing financial calculation where this can matter.
from decimal import Decimal pennies = input('Enter pennies: ') dollars = Decimal(pennies) * Decimal('.01') print(f'{pennies} pennies are worth ${dollars} in dollars')
Output:Enter pennies: 35
35 pennies are worth $0.35 in dollars
from decimal import Decimal for item in range(10): print(Decimal(item) * Decimal('0.4'))
Output:0.0
0.4
0.8
1.2
1.6
2.0
2.4
2.8
3.2
3.6