Debug C++ code: Catch first NaN appearance [duplicate] - c++

This question already has an answer here:
Stopping the debugger when a NaN floating point number is produced
(1 answer)
Closed 7 years ago.
Is there a simple way to check a C++ code in a debugger for the very first appearance of a NaN value?

The answer is given here: https://stackoverflow.com/a/5394095/1326595
Just include
#include <fenv.h>
and than add the following line to the code:
feenableexcept(FE_INVALID | FE_OVERFLOW);
The debugger is than able to capture the signal and shows the very first occurrence of a NaN.

By IEEE standard the following condition is false for NaN's:
val == val
and you could use it to trigger assert or software breakpoint, but beware of compiler optimizations. Probably, in debug build it would not get optimized away

You can put an assert(val >= 0 || val <= 0) to catch a NaN

Related

what is meaning of while(t-- >0) [duplicate]

This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 1 year ago.
In various codes where time limit exceeded, by using while(t-- >0) instead of while(t--) code runs successfully. I don't know about while(t-- >0) , I read it somewhere in the codechef examples solution.
In practice, they are equivalent. It's just a matter of taste. Some think that while(t-- > 0) is clearer than while(t--).
As long as t is not negative, the two are completely equivalent. But on the other hand, a negative t would most likely indicate a bug with a loop like that.

Floating Point Exception for seemingly no reason [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My code is throwing me a floating point exception here, but I can't figure out why.
int i = this -> numerator;
while(i > 1){
if ((this -> numerator % i == 0) && (this -> denominator % i == 0)) {
this -> numerator = this -> numerator / i;
this -> denominator = this -> denominator / i;
}
i = i - 1;
}
The goal is to simplify a fraction. As you can see, things only get mod by i and i > 1. Same goes for division. Strangely it will keep throwing the error even if I comment out the code within the while loop, but the code works fine if I get rid of the while loop completely. The same thing happened when I tried to use a for loop instead. What am I missing?
Strangely it will keep throwing the error even if I comment out the code within the while loop,
Then your problem is not in this bloc of code. Which, so far, I compiled and run with no error.
but the code works fine if I get rid of the while loop completely
Then, it's likely you are doing something with variable i after this bloc of code that makes the floating point exception.
Run your code with a debugger to check where it fails.

Is it ok to assume 0 is false and 1 is true? [duplicate]

This question already has answers here:
Does true equal to 1 and false equal to 0? [duplicate]
(2 answers)
Closed 7 years ago.
I've seen code where a condition is evaluated based on an int variable instead of bool.
int condition
cin >> condition;
if (!condition)
//do something
I know that C++ supports that 0 is false and 1 is true, but is this safe code? Is it supported by all C++ compilers as a standard?
Could it also be a bad practice considering that you might switch to another language and find out that this kind of code isn't supported?
I mean, is it good practice at all?
Yes it is safe to make that assumption. The standard defines the implicit conversion of various primitives to bool, and all the numeric types basically state that 0 converts to false and any non-zero value converts to true.

How can I define a number with a valid range of -PI to PI? [duplicate]

This question already has answers here:
C: How to wrap a float to the interval [-pi, pi)
(15 answers)
Closed 9 years ago.
I'm wondering if it's possible to define a custom data type which can only take a value between -3.1415926535897932 and 3.1415926535897931.
The idea is that a value below or above the range would automatically "wrap-around", eliminating the need to write code to do the conversion and also eliminating the possibility of error somewhere.
Yes it is possible. In the method that sets the value check to see if it outside the limits and if so do whatever operation you want to do to force it to be inside the limits. fmod is one good choice for operation.

How to trace a NaN in C++

I am going to do some math calculations using C++ . The input floating point number is a valid number, but after the calculations, the resulting value is NaN. I would like to trace the point where NaN value appears (possibly using GDB), instead of inserting a lot of isNan() into the code. But I found that even code like this will not trigger an exception when a NaN value appears.
double dirty = 0.0;
double nanvalue = 0.0/dirty;
Could anyone suggest a method for tracing the NaN or turning a NaN into an exception?
Since you mention using gdb, here's a solution that works with gcc -- you want the
functions defined in fenv.h :
#define _GNU_SOURCE
#include <fenv.h>
#include <stdio.h>
int main(int argc, char **argv)
{
double dirty = 0.0;
feenableexcept(FE_ALL_EXCEPT & ~FE_INEXACT); // Enable all floating point exceptions but FE_INEXACT
double nanval=0.0/dirty;
printf("Succeeded! dirty=%lf, nanval=%lf\n",dirty,nanval);
}
Running the above program produces the output "Floating point exception". Without
the call to feenableexcept, the "Succeeded!" message is printed.
If you were to write a signal handler for SIGFPE, that might be a good place to
set a breakpoint and get the traceback you want. (Disclaimer: haven't tried it!)
In Visual Studio you can use the _controlfp function to set the behavior of floating-point calculations (see http://msdn.microsoft.com/en-us/library/e9b52ceh(VS.80).aspx). Maybe there is a similar variant for your platform.
Some notes on floating point programming can be found on http://ds9a.nl/fp/ including the difference between 1/0 and 1.0/0 etc, and what a NaN is and how it acts.
One can enable so-called "signaling NaN". That should make it easily possible to make the debugger find the correct position.
Via google, I found this for enabling signaling NaNs in C++, no idea if it works:
std::numeric_limits::signaling_NaN();
Usefulness of signaling NaN?