Testing for infinity


I would like to test if the value returned in a double is infinity. I tried comparing if the bytes of the double were equal to 0x7F00000000000000 and I tried if(atof(“infinity”) == doubleVal) but the first always returned false and the second always returned true. I see a function infinity( ) in mathALib that looked like it might be helpful but I’m not sure how to include that lib. I can compile using that function but get a link error once on the cRIO. So really two questions here:

  1. How to link with a lib like mathALib?
  2. A nice way to check for infinity (and maybe NaN while your at it)?


I don’t have an answer to your question, but I’m curious how infinity/NaN somehow is useful in your program :slight_smile:

Are you dividing by zero somewhere?

I am using a gear tooth sensor and am using the GearTooth class. I call GetPeriod() from the Counter superclass to calculate a rate. At very slow speeds it will sometimes return Inf because no teeth have passed by since I last called GetPeriod I guess. I want to detect Inf and play some games with the rate value I am returning.

I do believe there’s a bug in the FPGA with Encoder and GearTooth’s GetRate/GetPeriod functions. Could be wrong…

I don’t think its a bug. I asked him how much time between the last two pulses you saw? He replied, haven’t seen any pulses.

In labview, at least, you can configure the timer so you can define what the time period is where if no pulses are seen, it is considered stopped. You can also get a stopped boolean value. I expect there is something similar in C++ if you dig around in the library. You could then use the stopped value instead of testing for infinity.

Ah good point, there are two function in Counter SetMaxPeriod and SetUpdateWhenEmpty which are there to configure this sort of behavior. Those are likely the best solution to my problem. Still if anyone can answer the two original questions I would appreciate it.

I don’t have WindRiver anywhere near me at the moment, but assuming math.h exists, there are functions like isinf(x) and/or the constants FP_INFINITE and FP_NAN in the floating point standards. So you might be able to write

#include <math.h>

if (isinf(1.0/0.0)) {
do something

On most development systems, including math.h that means you also need to link against the math library (-lm). I don’t know if WindRiver would do that automatically.

Linking with the math lib was one of my issues. I tried -lm from my linux experience but the compiler didn’t recognize it.

It’s a single mode OS… if you have a header that defines the symbols and you are not trying to statically link the library in, then you just need to load your library. The symbols will be looked up at runtime.


We were doing this exact same thing!
I know how we tested for NaN, but we didn’t use infinity.
If a number is equal to NaN, it is not equal to itself

double nan = 1/0*8; //NaN
if (nan != nan) {
// nan is equal to NaN