Jan-17-2021, 11:25 PM
(Jan-17-2021, 10:28 PM)bowlofred Wrote: Beware floating point math. Many decimals cannot be represented exactly. Either check for approximate solutions, or do your math with integers and then divide by 10 later. Otherwise you will run into problems.
>>> 0.3 + 0.3 + 0.3 + 0.1 == 1.0 FalseWorkaround 1
>>> 3 + 3 + 3 + 1 == 10 TrueWorkaround 2
>>> 0.99 < 0.3 + 0.3 + 0.3 + 0.1 < 1.01 TrueWorkaround 3
>>> decimal.Decimal('0.3') + decimal.Decimal('0.3') + decimal.Decimal('0.3') + decimal.Decimal('0.1') == decimal.Decimal('1.0') True
Many thanks, I did not notice that before!!!