Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/numpy: catch IEEE-754 "inexact" exception

numpy allows one to handle IEEE-754 exceptions originating from floating-point by using np.seterr appropriately. However seterr only supports the following keywords each corresponding to a IEEE-754 exception:

  • divide – Treatment for division by zero.
  • under – Treatment for floating-point overflow.
  • over – Treatment for floating-point overflow.
  • invalid – Treatment for invalid floating-point operation.

However, there's no keyword for the "inexact" IEEE-754 exception. How can one handle that in Python?

like image 562
Anakhand Avatar asked Jul 03 '26 21:07

Anakhand


1 Answers

I am not sure, but it may be because most floating point operations raise this exception due to inherent error in representation of floating point numbers using finite precision binary numbers. The rounding is employed to fit the result into limited size and hence the numbers are inexact. This is so natural, and hence probably they expect programmers to be aware of this fact and expect them not to compare two float numbers for equality.

like image 64
Kishor Bhoyar Avatar answered Jul 06 '26 11:07

Kishor Bhoyar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!