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
2
votes
2 answers

Trapping floating point exceptions and signal handling on Apple silicon

To trap floating point exceptions on MacOS, I use an extension that provides feenableexcept functionality. The original extension (written in 2009) is here http://www-personal.umich.edu/~williams/archive/computation/fe-handling-example.c NOTE: If…
Donna
  • 1,390
  • 1
  • 14
  • 30
2
votes
0 answers

How to catch a floating point error after fpectl has been removed?

Python 3.8.12 removed the fpectl module. This module enabled catching floating point errors as standard Python exceptions. I have a Python programme which uses a module which, in turns, calls a closed-source C library. The C library sometimes causes…
2
votes
0 answers

Masked and unmasked FP exceptions in the same instruction: are all bits set in MXCSR before handling the unmasked exception?

I am reading AMD and Intel manuals and find that when masked and unmasked exceptions coexist, the SIMD execution unit will give priority to the unmasked exceptions (before or after calculation) and then process the masked exceptions, but is it also…
2
votes
2 answers

Subtracting close together numbers in php

Why does output 0.0099999999999909 What am i missing here? This is php 5.2.
2
votes
3 answers

(C#) Should I Guard Against Infinity/-Infinity in my calculations? I.e. Throw an Overflow Exception?

Looking for some advice regarding Infinity/-Infinity in C#. I'm currently building Geometry classes, for shapes like Rectangle/Circle etc. The user will provide the input for Width/Depth and Diameter respectively. These properties will be double…
JB101UK
  • 33
  • 1
  • 8
2
votes
1 answer

strange floating point exception

I've got a floating point exception in huge application after some changes. I tried to comment my changes and found that FPE happens when I enable one simple function call. api::getMaxSize(); which simply returns value. looks like this int…
axe
  • 21
  • 3
2
votes
3 answers

Why am I getting "floating point exception (core dumped)"?

So when I enter the input of positives or positives and negatives the code works fine, but just entering negatives result in floating point error, I know dividing by zero would result in this, but I'm diving by the number of inputs #include…
Luis
  • 21
  • 3
2
votes
1 answer

Perfectly fine division throws floating point exception

I have a mystery floating point exception. I catch it by doing: feenableexcept( FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW ); Then the second division (last line) in this code: const float dx = other.px[ j ] - curx; const float dy = other.py[ j ]…
Bram
  • 7,440
  • 3
  • 52
  • 94
2
votes
1 answer

How to avoid floating point exception when using pow?

I use a C library which uses the pow function on two double values double a = pow(b, c) At the moment, I have b = 0.62 and c = 1504, which means that a should be nearly 0 (3.6e-312). But I have a floating point exception. How to avoid it and…
2
votes
1 answer

SIGFPE error with gfortran 4.8.5 handling

I am using a computational fluid dynamics software that is compiled with gfortran version 4.8.5 on Ubuntu 16.04 LTS. The software can be compiled with either single precision or double precision and the -O3 optimization option. As I do not have the…
gansub
  • 1,164
  • 3
  • 20
  • 47
2
votes
0 answers

Floating point exception (SIGFPE) when doing weighted average of 4 numbers

I'm creating an assembly language program with NASM on Linux. I'm trying to do a weighted average of 4 numbers where the 4th number entered has double the weighting of the others. If I use the numbers 30, 40, 50, 60, I'm calculating the weighted…
2
votes
1 answer

Enabling floating point exceptions on MinGW GCC?

How does one enable floating point exceptions on MinGW GCC, where feenableexcept is missing? Even reasonably complete solutions don't actually catch this, though it would appear that they intend to. I would prefer minimal code that is close to…
Chris Chiasson
  • 547
  • 8
  • 17
2
votes
2 answers

Trapping floating point exceptions in mixed C/Fortran code

I apologize for asking a question that has been asked numerous times before. But after several searches, I realize that I may have a fundamental misunderstanding between how FPEs are to be treated in C/C++ vs. how they are treated in Fortran. In…
Donna
  • 1,390
  • 1
  • 14
  • 30
2
votes
2 answers

Why am I getting a floating point exception error?

I am a novice programmer, so please deal with me. I'm writing a program to analyze data. When I execute the program, I receive a "floating point exception" and I have no clue why. Here is the section of code where the error is originating from. From…
Tyler
  • 27
  • 1
  • 5
1
vote
1 answer

How to check if a floating point exception happened using Go?

I'm trying to create a program that receives an expression with floating point numbers and shows if any exceptions occurred. I know that if I wrote it in C, it would be like this: #include #include //floating point math happens…
mello_
  • 11
  • 1
1 2
3
9 10