Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Testing for infinity (http://www.chiefdelphi.com/forums/showthread.php?t=91487)

jwakeman 11-02-2011 09:17

Testing for infinity
 
Hi,

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)?

Thanks!

davidalln 11-02-2011 10:42

Re: Testing for infinity
 
I don't have an answer to your question, but I'm curious how infinity/NaN somehow is useful in your program :)

Are you dividing by zero somewhere?

jwakeman 11-02-2011 12:54

Re: Testing for infinity
 
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.

basicxman 11-02-2011 12:59

Re: Testing for infinity
 
Quote:

Originally Posted by jwakeman (Post 1020589)
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...

jwakeman 11-02-2011 13:44

Re: Testing for infinity
 
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.

Joe Ross 11-02-2011 14:24

Re: Testing for infinity
 
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.

jwakeman 11-02-2011 14:54

Re: 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.

mandrews281 12-02-2011 23:03

Re: Testing for infinity
 
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.

jwakeman 13-02-2011 08:41

Re: Testing for infinity
 
Linking with the math lib was one of my issues. I tried -lm from my linux experience but the compiler didn't recognize it.

jhersh 13-02-2011 15:21

Re: Testing for infinity
 
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.

-Joe

tmiddlet 14-02-2011 23:11

Re: Testing for infinity
 
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
}


All times are GMT -5. The time now is 17:43.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi