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
27
votes
4 answers

Visual Studio C++ 2008 / 2010 - break on float NaN

Is there any way to set up Visual Studio (just upgraded from 2008 to 2010) to break, as if an assertion failed, whenever any floating point number becomes NaN, QNAN, INF, etc? Up until now I have just been using the assert(x == x) trick, but I would…
19
votes
2 answers

Enabling floating point interrupts on Mac OS X Intel

On Linux, feenableexcept and fedisableexcept can be used to control the generation of SIGFPE interrupts on floating point exceptions. How can I do this on Mac OS X Intel? Inline assembly for enabling floating point interrupts is provided in…
Geoffrey Irving
  • 6,483
  • 4
  • 32
  • 40
16
votes
2 answers

When does underflow occur?

I get into a situation where calculating 1.77e-308/10 triggers an underflow exception, but calculating 1.777e-308/10 does not. This is strange because: Underflow occurs when the true result of a floating point operation is smaller in magnitude…
zell
  • 9,830
  • 10
  • 62
  • 115
12
votes
5 answers

Why does integer division by -1 (negative one) result in FPE?

I have an assignment of expaining some seemingly strange behaviors of C code (running on x86). I can easily complete everything else but this one has really confused me. Code snippet 1 outputs -2147483648 int a = 0x80000000; int b = a /…
iBug
  • 35,554
  • 7
  • 89
  • 134
10
votes
2 answers

Causes for NaN in C++ application that do no raise a floating point exception

To find the cause of floating point variables beeing set to NaN in my C++ program I enabled floating point exceptions like this: #include feenableexcept(FE_INVALID | FE_OVERFLOW); I know it works because when I write: int val =…
Nathan
  • 7,099
  • 14
  • 61
  • 125
9
votes
1 answer

An invalid floating point operation occurred. SQL Server 2008

I have weird problem with this code: if I run it like shown below I get error: An invalid floating point operation occurred. But if I change parameter @Longitude to -98.508730 (notice only last digit changed) code works just fine. The code is…
bobetko
  • 5,019
  • 14
  • 58
  • 85
9
votes
4 answers

what languages expose IEEE 754 traps to the developer?

I'd like to play with those traps for educational purpose. A common problem with the default behavior in numerical calculus is that we "miss" the Nan (or +-inf) that appeared in a wrong operation. Default behavior is propagation through the…
nraynaud
  • 4,924
  • 7
  • 39
  • 54
9
votes
1 answer

C: unordered floating-point comparison does not raise FE_INVALID

I've run into an issue with floating-point comparisons. When comparing a value with NaN using the < operator, I expect the FE_INVALID flag to be set. The < operator should raise the flag according to the C11 standard (and also according to…
Stefan Mach
  • 121
  • 5
9
votes
1 answer

Force all QNaN to instead be normal NaN (SNaN) so exceptions are thrown

I've configured Visual Studio to throw floating point exceptions via the _controlfp function. This works for NAN and INF, but not QNAN. I.e. Quiet NaNs don't cause an exception to be raised. Is there any function, or config option for Visual…
Dave
  • 1,304
  • 1
  • 15
  • 19
8
votes
2 answers

"Safe" SIMD arithmetic on aligned vectors of odd size?

Let's say I have some 16-bytes aligned structure, that just wraps 3xFloat32 array: #[repr(C, align(16))] pub struct Vector(pub [f32; 3]); Now I want to divide two instances of it, like this: use core::arch::x86_64; let a = Vector([1f32, 2f32,…
L117
  • 526
  • 4
  • 13
8
votes
1 answer

Masking exceptions in Delphi

For days I am struggling hard (but in vain) with the exception masks. I have developed an application that makes heavy floating point calculations on hundreds of thousands of records. Obviously the code must be able to handle exceptions, especially…
7
votes
1 answer

Python/numpy: catch IEEE-754 "inexact" exception

numpy allows one to handle IEEE-754 exceptions originating from floating-point by using np.seterr appropriately. However seterr only supports the following keywords each corresponding to a IEEE-754 exception: divide – Treatment for division by…
Anakhand
  • 2,838
  • 1
  • 22
  • 50
7
votes
1 answer

Why does this code get floating point exception when there is no float data-type?

I'm not dividing by zero and there is no float datatype in my code, I still get floating point exception. #include #include #include #include #include using namespace std; int main() { unsigned…
Breakpoint
  • 428
  • 6
  • 19
7
votes
2 answers

How to solve undefined reference to functions in fenv.h when using uclibc?

I'm trying to test some of the functions in fenv.h, however, when I compile the below function ld fails with undefined reference to 'feclearexcept' and undefined reference to 'fetestexcept'. I'm running hardened gentoo compiled against uclibc, and I…
ragingSloth
  • 1,094
  • 8
  • 22
6
votes
1 answer

Are vectorize optimisations incompatible with floating point exception handling, or is there a bug in g++-11?

The behaviour of the code snippet below is different when compiled with g++-11.1.1 with different optimisation options. The snippet has floating point exception handling enabled. I don't think algorithmic details are important (it's a bit of 3D…
Bill
  • 468
  • 3
  • 9
1
2 3
9 10