Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding floats with representation error to the closest and correct result

I have a situation when the classic representation error in Python started to be a problem: I need them to be used for Matrix operations in Numpy and the decimal type is not supported yet.

You all know that if I do 111.85 * 111.85 I will get 12510.422499999999 but if I round(12510.422499999999, 4) I can get the proper result which is of course 12510.4225.

But the actual questions are:

  • Is this round stuff a good idea and a good practice?
  • Will this work for all cases? sometimes the decimals position where the ..999 decimals could be more
  • And finally, How to get the appropriate number of decimal positions to use with the round for all possible values?
like image 732
gerosalesc Avatar asked Jan 06 '16 19:01

gerosalesc


2 Answers

Even if you round the number, the decimal still won't be exactly what you expect it to be. Python may display only the most significant bits, but the underlying decimal inaccuracy is still there.

The python docs devote a section to it

Many users are not aware of the approximation because of the way values are displayed. Python only prints a decimal approximation to the true decimal value of the binary approximation stored by the machine. On most machines, if Python were to print the true decimal value of the binary approximation stored for 0.1, it would have to display

>>> 0.1
0.1000000000000000055511151231257827021181583404541015625

That is more digits than most people find useful, so Python keeps the number of digits manageable by displaying a rounded value instead

>>> 1 / 10
0.1

For most use cases, the inaccuracy can safely be ignored. If you are dealing with extremely small or extremely large numbers or need accuracy out to many decimal places, you can use the decimal library

 >>> Decimal('111.85') * Decimal('111.85')
 Decimal('12510.4225')
like image 112
Brendan Abel Avatar answered Sep 30 '22 08:09

Brendan Abel


The real issue here is to identify how the representation error could start to be a problem in the first place.

You should not be using the representation for human-readable printing of floats, you should be using for example str.format or some other presentation method.

Keep your numbers as numbers for all calculations (whether you do that with floats or Decimals or whatever...) and only round or truncate things at presentation.

like image 35
wim Avatar answered Sep 30 '22 07:09

wim