Questions tagged [floating-point-exceptions]

Code that make heavy use of floating point computations need to successfully alert the user when a particular mathematical operation results in an invalid computation. Examples are divide by 0 (which results in a mathematical 'inf'), a "Not-A-Number" or NaN, and so on. Va Compilers and hardware must work together to provide the programming with these alerts, so the solutions are often hardware and compiler specific.

Mathematical operations involving floating point numbers can result in undefined or non-arithmetic values. Common invalid operations encountered include "divide by zero", which results in a mathematical 'Inf', or 0/0, which resutls in a Not-a-Number (NaN). Other invalid operations in overflow, underflow and loss of precision due to rounding.

Propagation of invalid values

Many scientific software codes rely heavily on floating point operations, and so determining the origin of these floating point exceptions can be a painstaking task. The problem for the programmer is that these invalid values can propagate through the code according to extended arithmetic rules for these values. A NaN plus any number is always a NaN. Inf + 1 is still Inf.

Trapping invalid values

Most compilers allow for this propagation. The programmer may only realize something has gone wrong when the output is filled with NaNs or Infs, long after the first invalid operation occurred.
Often, the programmer would rather trap these errors on their first occurrence. To trap these "floating-point-exceptions", one must enable certain compiler or software options. For example, in fortran, compiler flags automatically trap the first occurrence of invalid operations. In C/C++, routines available in may be used to signal to the underlying hardware the occurrence of chosen exceptions should halt execution of the program, or take some other programmer-specified action.

136 questions
6
votes
1 answer

How to avoid floating point exceptions in unused SIMD lanes

I like to run my code with floating point exceptions enabled. I do this under Linux using: feenableexcept( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW ); So far so good. The issue I am having, is that sometimes the compiler (I use clang8) decides to…
Bram
  • 7,440
  • 3
  • 52
  • 94
6
votes
3 answers

Floating point exception (core dumped)

I have a long program, which consists of one header file, and two source files, in the first one I have written the implementations of the functions, and in the second one (which is my main), I call and execute them. Though, at one point I get an…
user1726549
5
votes
1 answer

Delphi XE2, TWebBrowser, float divide by zero

In Delphi 2010 and Delphi 2007 I am using Set8087CW on WebBrowserBeforeNavigate / WebBrowserDocumentComplete to prevent FPU errors inside ActiveX to cripple my application. But somehow this is not working in Delphi XE2, at least when in 64bit mode.…
Tom
  • 3,587
  • 9
  • 69
  • 124
4
votes
0 answers

Do you still (in the 2020s) need to mask floating-point exceptions in a Win32 Delphi OpenGL application?

It is common knowledge that you should mask floating-point exceptions if you use OpenGL in your Win32 Delphi application. In fact, the RTL more or less does this behind your back, because the OpenGL unit has the following initialization…
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
4
votes
2 answers

How to trap floating-point exceptions on M1 Macs?

The way to trap floating-point exceptions is architecture-dependent. This is code I have tested successfully on an Intel (x86) Mac: it takes the square root of a negative number twice, once before, and once after, enabling floating-point exception…
4
votes
1 answer

Why is fetestexcept in C++ compiled to a function call rather than inlined

I am evaluating the usage (clearing and querying) of Floating-Point Exceptions in performance-critical/"hot" code. Looking at the binary produced I noticed that neither GCC nor Clang expand the call to an inline sequence of instructions that I would…
Ikaros
  • 395
  • 1
  • 10
4
votes
1 answer

Different optimization in VS2015 vs VS2013 causes floating point exception

I have a small example of issue which came up during the transition from VS2013 to VS2015. In VS2015 further mentioned code example causes floating-point invalid operation. int main() { unsigned int enableBits = _EM_OVERFLOW | _EM_ZERODIVIDE |…
4
votes
3 answers

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…
Dante
  • 611
  • 1
  • 7
  • 21
4
votes
2 answers

Floating Point Exception Caused By rand() In c++

I have an issue that I can't seem to solve. I am randomly generating numbers in order to determine if my numbers are relativity prime. Here is the function that gives me a Floating Point Exception: bool modularExponentiationTest(unsigned long long…
Alex
  • 426
  • 1
  • 6
  • 15
3
votes
3 answers

Why is this generating Floating point exception?

This is the second example of wikipedia SIGFPE page. #include int main(void) { volatile int x=INT_MIN; volatile int y=-1; x=x/y; return 0; } It is inverting the sign to positive of INT_MIN. How can it be FPE?
Furqan
  • 268
  • 1
  • 13
3
votes
1 answer

SSE/AVX floating point convert exceptions

Suppose an SSE register contains one or more packed values not representable as a 32-bit int (such as Inf or NaN), and a convert-to-int is invoked, such as _mm_cvtpd_epi32 / cvtpd2dq. Is it safe, i.e. is the behavior defined? Does it break control…
jcai
  • 3,448
  • 3
  • 21
  • 36
3
votes
0 answers

How to disable Floating Point Exceptions 100% of the time in Visual Studio Native C++

My multithreaded native C++ application throws INEXACT Floating Point exceptions (FPE) after I added code for one thread to access a property in Windows Management Information (WMI) via the COM interface (using CoInitalize and related functions).…
3
votes
1 answer

Floating point trap with C program using valgrind

I'm trying to figure out how I can find the exact position where my simulation code written in C generates NaNs. I'm using plain C89 (but I can move to C99/C11) and gcc 4.4.5 on Debian squeeze. Apparently there is a patch for valgrind that should be…
Alberto
  • 67
  • 5
3
votes
2 answers

"Floating-point invalid operation" when inputting float to a stringstream

I have a simple piece of code that extracts a float from a FORTRAN-generated REAL array, and then inserts it into a stream for logging. Although this works for the first 30 cases, on the 31st it crashes with a "Floating-point invalid…
Mike Sadler
  • 1,750
  • 1
  • 20
  • 37
3
votes
2 answers

Do doubles suffer from overflow?

Is it possible to have an overflow (wrap around) with a double or a float? What will happen if the maximum (or minimum) value is reached on x86 or x64 hardware?
1
2
3
9 10