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:
Is Inf
an overflow exception?
If is, why C compilers don't return Inf
?
If not, can I get an overflow exception in Matlab?
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.
Operations on floating-point numbers can produce results that are not numerical values. Examples:
sqrt(-1.0)
)1.0 / 0.0
)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:
_control87()
to control the FPU flags.feenableexcept
).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
This is obviously completely different from floating-points, since integer types cannot represent Inf or NaN:
uint8(200) + uint8(200)
will be uint8(255)
).MATLAB implements the IEEE Standard 754 for floating point operations. This standard has five defined exceptions:
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:
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.
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).
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).
See answer 1.
For more fun and in-depth examples, here is a nice PDF.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With