0

If we use a double in c# or a float in python, simple calculations using variables of 1 decimal place seem to not be correct. In both python and c#, if I perform the calculation 5.2 - 5.1, I get the answer 0.10000000000000053, when it should obviously be 0.1.

Can someone please explain to me why this is?

Also, in c#, we can use the decimal type to avoid this issue. Is there a similar type in python I can use?

  • 1
    See https://docs.python.org/3.8/library/decimal.html – canton7 Mar 06 '20 at 10:27
  • "if I perform the calculation 5.2 - 5.1" --> Code did not do "5.2 - 5.1" Instead it subtracted a number near 5.1 from a number near 5.2. Not all numbers are exactly representational. 5.1, 5.2 are two of those. – chux - Reinstate Monica Mar 07 '20 at 03:38

0 Answers0