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
1 answer

floating point exceptions in LINUX -- how to convert them into something useful

I've got some piece of code calculating a vector of functions of a vector of independent variables (and parameters): struct instance { double m_adParam1, m_adParam2; std::array calculate(const std::array&x)…
1
vote
1 answer

How to let a Python program raise an alarm whenever a floating-point overflow or underflow occurs?

I have a python program P that calls numpy, scipy and many other libraries in scientific computing. I can modify program P but cannot modify the libraries it calls. Now I want the program P raises an alarm whenever a floating-point overflow or…
zell
  • 9,830
  • 10
  • 62
  • 115
1
vote
1 answer

Floating Point exceptions raised by external 64-bit library crashes my application, even though exception masks are in place

I have stumbled upon a stubborn problem in my task to port a Delphi 5 32-bit application to a Delphi 10.4 64-bit application. I have succeeded in porting the application to a Delphi 10 32-bit application, but when converting to 64-bit I also need to…
1
vote
1 answer

Is it considered normal that f = NAN may cause raising floating-point exceptions?

C2x (as well as previous): The macro NAN is defined if and only if the implementation supports quiet NaNs for the float type. It expands to a constant expression of type float representing a quiet NaN. Sample code (t0a.c) #include…
pmor
  • 5,392
  • 4
  • 17
  • 36
1
vote
7 answers

Simple question about 'floating point exception' in C

I have the following C program: #include int main() { double x=0; double y=0/x; if (y==1) printf("y=1\n"); else printf("y=%f\n",y); if (y!=1) printf("y!=1\n"); else printf("y=%f\n",y); return 0; } The output I get is…
Moshe
  • 205
  • 1
  • 6
  • 8
1
vote
1 answer

Why QNAN == QNAN does not lead to raising FE_INVALID exception?

Code (t125.c): #include #include #include #if _MSC_VER #pragma fenv_access (on) #else #pragma STDC FENV_ACCESS ON #endif void show_fe_exceptions(void) { printf("exceptions raised: "); if…
pmor
  • 5,392
  • 4
  • 17
  • 36
1
vote
0 answers

feraiseexcept: different behavior between compilers and lack of documentation for implementation-defined behavior

Sample code (t91.c): #include #include #if _MSC_VER #pragma fenv_access (on) #else #pragma STDC FENV_ACCESS ON #endif void show_fe_exceptions(void) { printf("exceptions raised:"); if(fetestexcept(FE_DIVBYZERO)) printf("…
pmor
  • 5,392
  • 4
  • 17
  • 36
1
vote
2 answers

(C++) I am trying to read and output a random line from a text file and I keep getting "Floating Point Exception (Core Dumped)" when running it

Basically what the title says. I'm trying to write code that will take in a random word from a file called "words.txt" and output it. I run it and keep getting the error "Floating Point Exception (Core Dumped)". Here's the code: #include…
Anthony O
  • 25
  • 4
1
vote
0 answers

Throwing a C++ exception with LibUnwind on PowerPC loaded sets random floating point exception traps

I'm currently debugging some failure in PyTorch which is a Python library with a C++ extension, so there is some C++ code called by the Python code. The failure happens because some floating point exception traps are getting set before a seemingly…
Flamefire
  • 5,313
  • 3
  • 35
  • 70
1
vote
1 answer

To what lengths should I go in order to avoid raising `FE_INEXACT` in library code?

I'm creating a library in C that contains common data structures, convenience functions, etc. that is intended for general use. Within, I've implemented a dynamic array, and I've chosen the golden ratio as the growth factor for the reason explained…
Isaac Saffold
  • 1,116
  • 1
  • 11
  • 20
1
vote
2 answers

Are floating-point numbers used without an epsilon always a code-smell?

This question is very simple. It is related to but definitely not a dupe of: Most unpatched Tomcat webservers are vulnerable, who's at fault? Seen the amazing amount of things that can go wrong with floating-point numbers (including, but not…
SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
1
vote
1 answer

x86 Assembly: Division Floating Point Exception dividing by 11

I'm trying to divide 859091 by 11 to obtain the quotient and the remainder, but I'm getting Floating Point Exception on line: div bx This is my code for SASM: %include "io.inc" section .data dividend dd 859091 divisor dw 11 section…
1
vote
1 answer

Floating point exception on double -> unsigned __int64 typecast in C++ Builder 10.1

I'm running into an issue with certain double to unsigned __int64 typecasts in C++ Builder 10.1 Berlin throwing a floating point invalid operation exception($C0000090). My application allows for user input, so I need to be able to handle any values,…
Nenad
  • 13
  • 2
1
vote
1 answer

What leads to the floating point exception "1.#IO"

I'm debugging a simulation with many calculations in many Fortran files. I am used to seeing some floating point exceptions (-1.#QNB or 1.#QNAN) for errors that involve division by zero or operations that exceed the maximum size for floating point…
1
vote
0 answers

Visual Studio C# 2010/2013 - break on float NaN

I found this very useful question on how to break on invalid float operations in C++: Visual Studio C++ 2008 / 2010 - break on float NaN. How can I achieve the same thing in C#?
Grzenio
  • 35,875
  • 47
  • 158
  • 240