-2

As I started to write tests for my calculations I noticed the following issue:

>>> np.multiply(0.55, 20)
11.0
>>> np.multiply(np.float(0.55), 20)
11.0
>>> np.multiply(np.float128(0.55), 20)
11.000000000000000888
>>> np.ceil(np.multiply(0.55, 20))
11.0
>>> np.ceil(np.multiply(np.float128(0.55), 20))
12.0

As seen in the example using the np.float128 class results in a different solution of the calculation. I've been using the numpy functions(e.g., np.multiply) in combination with the np.float128 class because I thought that this would give me more precise calculation results.

So my question is: What is the best way to handle rather complex calculations in python?

I would be fine with loosing high precision if I'm getting correct results.

UnusualWays
  • 177
  • 1
  • 2
  • 11
  • 1
    Why don't you use `round` before `ceil`? – mozway Sep 07 '22 at 19:18
  • 1
    If your correctness relies on decimal exact calculation, any form of binary floating point will betray you from time to time. Rounding off to some fixed smaller number of places before getting the ceiling will help here, but there will always be edge cases. [floating point math is broken](https://stackoverflow.com/q/588004/364696) if you rely on precise results. – ShadowRanger Sep 07 '22 at 19:39
  • @mozway because for that use case the calculation needs to ceil. – UnusualWays Sep 08 '22 at 05:42
  • @ShadowRanger What would be a better approach to calculate in python? – UnusualWays Sep 08 '22 at 05:42
  • This is not a python limitation but something general to binary floating-point math. NB. rounding to the number of digits around the desired precision before ceiling should handle most cases – mozway Sep 08 '22 at 06:26
  • 1
    @UnusualWays: It's *far* less efficient than `numpy` types, but the `decimal` module provides exact decimal precision (infinite repeating decimals of course can't be represented precisely), and the `fractions` module provides effectively infinite precision (at the cost of unbounded amounts of time to compute results). – ShadowRanger Sep 08 '22 at 11:19

1 Answers1

-2

np.float128 does result in higher precision compared to np.float, as seen in your example. You have to take the order of execution into account here!

The important question is, what level of precision is good enough for your application (the result)? When you got this figured out, you can determine the level of precision for the individual floating point variables, to reach this goal.

Valanthor
  • 1
  • 1