Floating point arithemtic np.ceil

Viewed 39

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.

1 Answers

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.

Related