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

Floating Point Exception while running the program

I am trying to execute the program using OpenMP and see the time of execution but I keep getting Floating - point - exception. Without the design.cpp file I won't be able to instantiate the DUT in the test bench. So included that file in other…
Abhi
  • 1
  • 1
0
votes
1 answer

How can I avoid dividing by zero without too many conditionals?

I have an integer parameter which is supposed to control how many times in a particular run an event occurs. For example, if the number of iterations for each run is 1000, then the parameter FREQ would be 5, if I wanted to have the event occur…
0
votes
1 answer

Floating Point Exception with no float or double variable

I am getting a problem with following code that "Floating point exception, core dumped" but I have not even one float or double variable. By using checking printf, i observed that it happens in isPrimeFunction, where there execution jams. /* PROBLEM…
0
votes
1 answer

How to properly avoid SIGFPE and overflow on arithmetic operations

I've been trying to create a Fraction class as complete as possible, to learn C++, classes and related stuff on my own. Among other things, I wanted to ensure some level of "protection" against floating point exceptions and…
0
votes
1 answer

Floating point exception in GCD code

I was creating a program to calculate the gcd of two numbers by using the Euclidian method but I got a floating point exception error. What should I do? /* Euclidian Greatest Common Divisor Key Lemma if gcd(a,b) = gcd(a',b) = gcd(b,a') Where a'…
itachi99
  • 11
  • 1
  • 1
0
votes
1 answer

Floating point exception error while finding ''Perfect Number" using user defined functions

I have to write a program with checks "Perfect Numbers" in a specific range given by the user. I made a user-defined function to check if a particular number is a perfect number. If it is then 1 is returned, if not then 0. And then I use an if…
0
votes
1 answer

C - Floating Point Exception (core dumped)

This function is supposed to, pixel by pixel, blur the image by turning the color of each pixel on the mean of the colors around it in a 2n+1 "radius". (The part where it skips to the next pixel is already implemented, don't worry). I successfully…
Asfourhundred
  • 3,003
  • 2
  • 13
  • 18
0
votes
0 answers

Making floating-point exceptions explicit at run time

Suppose I have a C program that manifests a floating-point overflow, such as in the example below. int main(){ double x,y; x=1e+300; y=x*x; printf ("x = %.5g; y = %.5g, MAX = %.5g\n", x, y, DBL_MAX); } My goal is to catch the…
zell
  • 9,830
  • 10
  • 62
  • 115
0
votes
1 answer

Unusual Floating point exception (core dumped) Error with C

I am currently a student, trying to get factorials to print out as prime numbers multiplied to certain exponents like so: 5! = (2^3)(3^1)(5^1) However, I keep getting an unusual error, which occurs right after using scanf to retrieve my input (By…
0
votes
1 answer

floating point exception when finding intersection

consider this code: Point findIntersection(Line l1, Line l2) { int T1, T2; T2 = (l2.d.x*(l1.p.y-l2.p.y) + l2.d.y*(l2.p.x-l1.p.x))/(l1.d.x*l2.d.y - l1.d.y*l2.d.x); T1 = (l1.p.x+l1.d.x*T2-l2.p.x)/l2.d.x; if (T1>0 && 0
Ace shinigami
  • 1,374
  • 2
  • 12
  • 25
0
votes
1 answer

Vigenere.c CS50 Floating Point Exception (Core Dumped)

I am working on the Vigenere exercise from Harvard's CS50 (in case you noticed I'm using string and not str). My program gives me a Floating Point Exception error when I use "a" in the keyword. It actually gives me that error when I use "a" by…
EcuaCode
  • 11
  • 5
0
votes
1 answer

Assembly - Floating Point Exception Received

I am attempting to write a program that calculates download times but I keep receiving a "floating point exception" error on output. Any help is appreciated as I have attempted to debug this code for quite some time and simply cannot identify the…
0
votes
1 answer

Sort function giving floating point exception for a large input of 0's

I have written a code for this problem: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so…
user2223211
  • 165
  • 1
  • 2
  • 7
0
votes
1 answer

_controlfp does not prevent DivideByZeroException

I wrote the following lines in C# using System; using System.Runtime.InteropServices; namespace ConsoleApplication1 { class Program { [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] extern static uint…
0
votes
0 answers

c++ dynamic array Floating Point exception

For my homework I had to design an arraylist in c++ using only 1d arrays and pointers to make the array dynamic. I have done ample testing and my functions work correctly, but when I use the main that the teacher has provided me I get this floating…
spstephens
  • 75
  • 1
  • 7