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

Compiling GROMACS with Intel oneapi icc/icpc has undefined feenableexcept/fedisableexcept

I am posting here since I don't think GROMACS is going to know my issue. I have MacOS Big Sur and download Intel C/C++ compiler because it is supposed to make this program faster. I can compile with clang and GNU gcc but not Intel. The cmake…
Brian Wiley
  • 485
  • 2
  • 11
  • 21
0
votes
0 answers

floating point exception with a postfix calculator

This is my attempt to code a postfix calculator using a stack. I have two functions pop and push and the variable l indicate the last element. When executed with the terminal, it should be like this: ./postfix 3 2 + 5 instead I get the error :…
0
votes
1 answer

what are the cases when a floating point exception happens other than a divide by zero?

i was writing some code to test the checksum for a credit card via an algorithm known as Luhn’s algorithm and verify if it's a valid card number or not the code i wrote is the program compiled fine but when i gave an input it said floating point…
dxuian
  • 1
  • 1
0
votes
1 answer

How do I get C++ signaling_nan() to throw an exception, without needing if tests?

I am trying to use signaling_nan() to automatically throw an exception if a variable whose value will be set post-initialization, is used before being set. I have tried using the floating point exception functionalities via fenv.h, however that…
0
votes
0 answers

Which floating point exceptions make NaNs?

Of the five floating point exception categories defined in C++, namely: FE_INEXACT FE_DIVBYZERO FE_UNDERFLOW FE_OVERFLOW FE_INVALID ...which of these can result in a NaN value? Also - are there any operations that can result in a NaN value, but…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
0
votes
0 answers

NAN used in arithmetic / comparison operations: different results between compilers wrt raising of floating-point exceptions: how to explain?

Context: for the following sample code different compilers produce different results wrt raising of floating-point exceptions. Question: how to explain the difference? Sample code (t125.c): #include #include #include…
pmor
  • 5,392
  • 4
  • 17
  • 36
0
votes
0 answers

Basic program returning an unexpected value

I've written a program to do some basic operations on a long number from the user. Depending on whether the final product ends in a 0 or not, the program printfs "valid" or "invalid". I tried it with some 10 digit numbers and it worked fine, but…
BlueKhakis
  • 293
  • 1
  • 8
0
votes
1 answer

How to use try-catch to catch floating point errors?

#include #include #pragma fenv_access (on) int main(int, char**argv) { unsigned int fp_control_word; _controlfp_s(&fp_control_word, 0, 0); const unsigned int new_fp_control_word = fp_control_word | _EM_INVALID |…
user12411795
0
votes
0 answers

How to solve this Floating Point expection: 8 error

This program is suppose to take a json in the format of a dict. The keys are zipcodes and the values are vectors of rent prices. When i run it using g++ I get a floating point exception 8: error. To my knowledge that error only triggers if the value…
0
votes
1 answer

ffmpeg segmentation fails with floating point exception

I'm trying to segment a video using the following command: ffmpeg -i /home/user/videos/0001.MP4 -codec copy -map 0 -f segment -segment_frames 66 /tmp/boos/0001/0001_%03d.MP4 After working for a few seconds, the output being frame= 251 fps=0.0…
0
votes
2 answers

Floating point exception (core dumped) for Linear Congruential Generator

I'm finishing up my header file for a Linear Congruential Generator (LCG) based cipher program. It receives two unsigned long values (m and c) and generates the LCG struct using these values. In my getA() I'm trying to have it add the temp variable…
0
votes
0 answers

Floating Point Exception in Assembly [NASM]

According to some tutorials I've seen, there's a number of ways to do things in Assembly. I'm at a marker here on whether I can do this or not. I'm ordering a book on Assembly, but right now I need to know the thing that is wrong with my High - Low…
onex4
  • 3
  • 3
0
votes
0 answers

floating point exception while using CNNs with keras

I am trying to implement a simple CNN in keras for regression. However, as I increase the number of layers of my network, it gives a floating point exception. My model is defined below model.py def create_model(image_shape): config =…
0
votes
0 answers

Floating point exception with big numbers and inaccurate results when changing integers to longs

I was given this problem which gives you 2 integers n and m, n representing the number of figures of the second number, and the second number is a binary number. For example: n= 2 m= 11 After figuring out the binary number, you have…
0
votes
0 answers

(C++) How to fix error: Terminated due to signal: FLOATING POINT EXCEPTION (8)

I am practicing some book problems, and I am not able to understand why is there a "Terminated due to signal: FLOATING POINT EXCEPTION (8)" error. If possible, could you please explain why is there an error and how to fix it. Thank you for your…
Santiagopph48
  • 131
  • 1
  • 2
  • 8