Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overflow vs Inf

When I enter a number greater than max double in Matlab that is approximately 1.79769e+308, for example 10^309, it returns Inf. For educational purposes, I want to get overflow exception like C compilers that return an overflow error message, not Inf. My questions are:

  1. Is Inf an overflow exception?

  2. If is, why C compilers don't return Inf?

  3. If not, can I get an overflow exception in Matlab?

  4. Is there any difference between Inf and an overflow exception at all?

Also I don't want check Inf in Matlab and then throw an exception with error() function.

like image 256
Dante Avatar asked Mar 18 '23 02:03

Dante


2 Answers

1) Floating-points in C/C++

Operations on floating-point numbers can produce results that are not numerical values. Examples:

  • the result of an operation is a complex number (think sqrt(-1.0))
  • the result of an operation is undefined (think 1.0 / 0.0)
  • the result of an operation is too large to be represented
  • an operation is performed where one of the operands is already NaN or Inf

The philosophy of IEEE754 is to not trap such exceptions by default, but to produce special values (Inf and NaN), and allow computation to continue normally without interrupting the program. It is up to the user to test for such results and treat them separately (like isinf and isnan functions in MATLAB).

There exist two types of NaN values: NaN (Quiet NaN) and sNaN (Signaling NaN). Normally all arithmetic operations of floating-point numbers will produce the quiet type (not the signaling type) when the operation cannot be successfully completed.

There are (platform-dependent) functions to control the floating-point environment and catch FP exceptions:

  • Win32 API has _control87() to control the FPU flags.
  • POSIX/Linux systems typically handle FP exception by trapping the SIGFPE signal (see feenableexcept).
  • SunOS/Solaris has its own functions as well (see chapter 4 in Numerical Computation Guide by Sun/Oracle)
  • C99/C++11 introduced the fenv header with functions that control the floating-point exception flags.

For instance, check out how Python implements the FP exception control module for different platforms: https://hg.python.org/cpython/file/tip/Modules/fpectlmodule.c

2) Integers in C/C++

This is obviously completely different from floating-points, since integer types cannot represent Inf or NaN:

  • unsigned integers use modular arithmetic (so values wrap-around if the result exceeds the largest integer). This means that the result of an unsigned arithmetic operation is always "mathematically defined" and never overflows. Compare this to MATLAB which uses saturation arithmetic for integers (uint8(200) + uint8(200) will be uint8(255)).
  • signed integer overflow on the other hand is undefined behavior.
  • integer division by zero is undefined behavior.
like image 58
Amro Avatar answered Mar 20 '23 17:03

Amro


Floating Point

MATLAB implements the IEEE Standard 754 for floating point operations. This standard has five defined exceptions:

  1. Invalid Operation
  2. Division by Zero
  3. Overflow
  4. Underflow
  5. Inexact

As noted by the GNU C Library, these exceptions are indicated by a status word but do not terminate the program. Instead, an exception-dependent default value is returned; the value may be an actual number or a special value Special values in MATLAB are Inf, -Inf, NaN, and -0; these MATLAB symbols are used in place of the official standard's reserved binary representations for readability and usability (a bit of nice syntactic sugar). Operations on the special values are well-defined and operate in an intuitive way.

With this information in hand, the answers to the questions are:

  1. Inf means that an operation was performed that raised one of the above exceptions (namely, 1, 2, or 3), and Inf was determined to be the default return value.

  2. Depending on how the C program is written, what compiler is being used, and what hardware is present, INFINITY and NaN are special values that can be returned by a C operation. It depends on if-and-how the IEEE-754 standard was implemented. The C99 has IEEE-754 implementation as part of the standard, but it is ultimately up to the compiler on how the implementation works (this can be complicated by aggressive optimizations and standard options like rounding modes).

  3. A return value of Inf or -Inf indicates that an Overflow exception may have happened, but it could also be an Invalid Operation or Division by Zero. I don't think MATLAB will tell you which it is (though maybe you have access to that information via compiled MEX files, but I'm unfamiliar with those).

  4. See answer 1.

For more fun and in-depth examples, here is a nice PDF.


Integers

Integers do not behave as above in MATLAB. If an operation on an integer of a specified bit size will exceed the maximum value of that class, it will be set to the maximum value and vice versa for negatives (if signed). In other words, MATLAB integers do not wrap.

like image 36
TroyHaskin Avatar answered Mar 20 '23 15:03

TroyHaskin