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
1
vote
3 answers

What are the dangers of floating point exceptions on invalid input?

I ran some fuzzying on dcraw and found a floating point exception. What are the dangers of this? It reads some length plen from the corrupted file and computes foo[i % plen]. If plen == 0 then this is undefined by the standard and gcc throws a…
Unapiedra
  • 15,037
  • 12
  • 64
  • 93
1
vote
1 answer

Questions on "forrtl: error (65): floating invalid"

I've been running a huge Fortran code with Intel compiler version 13.1.3.192 under the debug mode (with -O0 -g -traceback -fpe3 flags being turned on). It gave me the following output message: ... ... forrtl: warning (402): fort: (1): In call to…
elfsummer
  • 147
  • 1
  • 4
  • 10
1
vote
1 answer

Method to ensure GetHashCode() overload returns the same for semi-equal R3 float vectors

This one is for the binary and primitive experts. I am implementing a float R3 vector struct and my definition for "equality" is actually "mostly equal." Specifically, for all coords of the compared vectors Abs( (a[i] - b[i]) / (a[i] + b[i]) ) <…
1
vote
2 answers

Floating point: Disable specific exception

I set tow functions, one to enable exception for floating point and one to disable exception. In the code bellow, I have enabled tow exception in one time (_EM_ZERODIVIDE and _EM_OVERFLOW), after I need to disable just _EM_ZERODIVIDE and let…
Phiber
  • 1,041
  • 5
  • 17
  • 40
1
vote
1 answer

Floating Point introspection in VS2010 - how do I check without breaking?

I've sort of gone around the houses here, and I'd thought I'd found a solution. It certainly seems to correctly identify the problems I know about, but also leads to unexplained crashes in about half of all system test cases. The problem is that…
1
vote
1 answer

fegetenv() clears exception mask on x86_64-linux

Let's take the following program: #include #include int main (void) { fenv_t e; printf ("%d\n", fegetexcept () & FE_INVALID ? 1 : 0); feenableexcept (FE_INVALID); printf ("%d\n", fegetexcept () & FE_INVALID ? 1 : 0); …
F'x
  • 12,105
  • 7
  • 71
  • 123
1
vote
1 answer

Floating point exceptions in x86 NASM assembly using div instruction

I have an assignment where I must take input of a number, and figure out all prime numbers up to but not exceeding that number. For example if I entered 9 into the program, it should print 3, 5, and 7. My plan to determine if a number is prime or…
Slavic_Donut
  • 51
  • 1
  • 8
1
vote
0 answers

Floating-point exceptions in multi-threaded application

Does the floating point status word work on a per-thread or per-process basis? I have a program, compiled for x86 32-bit, which at several points in the code calls _status87() at several points in the code. I am trying to figure out whether this…
tt293
  • 500
  • 4
  • 14
1
vote
1 answer

extremely slow program from using AVX instructions

I'm trying to write a geometric mean sqrt(a * b) using AVX intrinsics, but it runs slower than molasses! int main() { int count = 0; for (int i = 0; i < 100000000; ++i) { __m128i v8n_a = _mm_set1_epi16((++count) % 16), v8n_b =…
Yale Zhang
  • 1,447
  • 12
  • 30
1
vote
0 answers

MCH1206 floating point overflow condition

IBM OS400 C ILE program Does anyone know when MCH1206 is issued in a C program? The C program does not have any error handling defined. In my environment, whenever there is an overflow, - e.g. result = pow(a,b) If there is an overflow, result is…
1
vote
1 answer

Python time.sleep() running into Floating Point exception

I am using the Python time.sleep() function in a GNURadio program. However, in spite of me having provided a floating point argument, the code runs into an unexpected Floating Point Exception. Please find the relevant code snippet below (please…
0
votes
0 answers

Floating point exception without traceback in python GTK

I have a GTK python program I am developing. I need to change the look of the window based on the type of activity the user is doing. I'm using a Gtk.Grid() object that I was planning on just calling widget.hide() on the things I need to appear and…
narnie
  • 1,742
  • 1
  • 18
  • 34
0
votes
0 answers

Rounding a floating-point number to a given precision

If I give a double value and an integer value, I want to round that double to the precision of the integer value accurately. In the end, I want to get a double value which is more accurate as the output. I tried it using the boost multi-precision…
0
votes
0 answers

Setting an MXCSR exception flag manually and triggering a floating point exception if applicable

How can I set a SSE floating point exception by instruction and tigger the execution of this floating point trap at the same time if this exception isn't masked ?
Bonita Montero
  • 2,817
  • 9
  • 22
0
votes
1 answer

How to Trap Floating-Point Exceptions On Rosetta 2

In a related question, How to trap floating-point exceptions on M1 Macs?, someone wanted to understand how to make the following code work natively on macOS hosted by a machine using the M1 processor: #include // for sqrt() …