Python/numpy: catch IEEE-754 "inexact" exception
Asked Answered
C

1

8

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?

Callaghan answered 6/10, 2020 at 10:42 Comment(3)
I might suspect it is not supported because it is rarely useful. Inexact exceptions are routine in ordinary floating-point code and are only useful in expertly crafted code using floating-point operations for special purposes.Stationmaster
Can you tell us what are your goals ultimately? There are other ways to tell if a floating-point calculation has been inexact.Execrative
I've heard that some code in case of Inexact was raised, may adjust the input values and redo the calculation(s) until the output value (i.e. result) is exact. Though, I've never seen such code.Yazzie
O
1

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.

Octastyle answered 28/12, 2022 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.