Velocity Reviews > checking double for Inf or NaN - how?

# checking double for Inf or NaN - how?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a

 05-18-2006
Hi,

I found the code below from
http://www.blitzbasic.com/Community/...hp?topic=55633

------
' Check if number is finite.

Local uouble = 2 ' also works for floats

For Local n = 1 To 12
Print isfinite(u) + " " + u
u :* u
Next

Print
Print

u = u - u
Print isfinite(u) + " " + u

Function isfinite( xouble ) ' assumes Intel byte order

Const EXP_BITS = %01111111111100000000000000000000

Local bits = ( Int Ptr Varptr x )[1] ' [0] for Mac?

Return ( bits & EXP_BITS ) <> EXP_BITS ' exponent is all 1s for
infinity or NaN

End Function

------

I need something like that - but the author writes that "This is intel
only. Anybody want to make it multiplatform? It shouldn't be difficult,
but I don't have a Mac".

The code above doesn't look like "standard C" to me... But I would like
this function to work *preferable* under both linux, mac and windows
pc's....

My guess is that the code should be changed to something like:

int(double testval)
{
Const EXP_BITS = %01111111111100000000000000000000

return (testval && EXP_BITS); /* returns either 0 or 1 ? */
}

But I guess EXP_BITS is completely wrong defined, so how to make it work
- and are there better existing ways to check for Inf or NaN???

It would be nice with 2 functions: One that tests for plus/minus Inf and
one that checks for NaN, if there's any difference? I didn't really
understood the bit-pattern-difference between NaN and Inf...

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a

 05-18-2006
Martin Jørgensen wrote:
-snip-

> It would be nice with 2 functions: One that tests for plus/minus Inf and
> one that checks for NaN, if there's any difference? I didn't really
> understood the bit-pattern-difference between NaN and Inf...

Basically I have a variable that contains -1.#IND00000000000...
according to MS visual studio 2005... I thought that this was (-inf) so

....
if( val < 0)
do something <- never got here....

I hope somebody can figure this out?

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

Joe Smith
Guest
Posts: n/a

 05-18-2006

"Martin Jørgensen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I found the code below from
> http://www.blitzbasic.com/Community/...hp?topic=55633
>
> ------
> ' Check if number is finite.
>
>
> Local uouble = 2 ' also works for floats
>
> For Local n = 1 To 12
> Print isfinite(u) + " " + u
> u :* u
> Next
>
> Print
> Print
>
> u = u - u
> Print isfinite(u) + " " + u
>
>
> Function isfinite( xouble ) ' assumes Intel byte order
>
> Const EXP_BITS = %01111111111100000000000000000000
>
> Local bits = ( Int Ptr Varptr x )[1] ' [0] for Mac?
>
> Return ( bits & EXP_BITS ) <> EXP_BITS ' exponent is all 1s for infinity
> or NaN
>
> End Function
>
>
> ------
>
> I need something like that - but the author writes that "This is intel
> only. Anybody want to make it multiplatform? It shouldn't be difficult,
> but I don't have a Mac".
>
> The code above doesn't look like "standard C" to me... But I would like
> this function to work *preferable* under both linux, mac and windows
> pc's....
>
> My guess is that the code should be changed to something like:
>
> int(double testval)
> {
> Const EXP_BITS = %01111111111100000000000000000000
>
> return (testval && EXP_BITS); /* returns either 0 or 1 ? */
> }
>
> But I guess EXP_BITS is completely wrong defined, so how to make it work -
> and are there better existing ways to check for Inf or NaN???
>
> It would be nice with 2 functions: One that tests for plus/minus Inf and
> one that checks for NaN, if there's any difference? I didn't really
> understood the bit-pattern-difference between NaN and Inf...

The source snippet might look different to you than me, but it seems to be
missing about ten tokens in order to be C. joe

=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a

 05-18-2006
Martin Jørgensen wrote:
-snip-

> understood the bit-pattern-difference between NaN and Inf...

Addition: You can generate -1.#IND000000... by:

#include math.h

....
var = sqrt (-10);
....

var = -1.#IND000000.., according to MS visual studio 2005 (just in case
somebody wants to try it out).

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

bert
Guest
Posts: n/a

 05-18-2006

Martin Jørgensen wrote:
> Martin Jørgensen wrote:
> -snip-
>
> > It would be nice with 2 functions: One that tests for plus/minus Inf and
> > one that checks for NaN, if there's any difference? I didn't really
> > understood the bit-pattern-difference between NaN and Inf...

>
> Basically I have a variable that contains -1.#IND00000000000...
> according to MS visual studio 2005... I thought that this was (-inf) so
>
> ...
> if( val < 0)
> do something <- never got here....
>
> I hope somebody can figure this out?
> Martin Jørgensen

The IEEE floating point standard says that any comparison
in which one operand is a NaN should evaluate to FALSE.
So the following counter-intuitive test meets your needs:

if (val == val)
do_nothing ( );
else
report_NaN (val);

unless an over-eager (and incorrect) compiler optimises
half of it away . . .
--

pete
Guest
Posts: n/a

 05-18-2006
Martin Jørgensen wrote:

> Basically I have a variable that contains -1.#IND00000000000...
> according to MS visual studio 2005...
> I thought that this was (-inf) so
>
> ...
> if( val < 0)
> do something <- never got here....
>
> I hope somebody can figure this out?

exp(DBL_MAX) is a range error.
sqrt(-1) is a domain error.

/* BEGIN new.c */

#include <math.h>
#include <float.h>
#include <stdio.h>

int main(void)
{
double x = -exp(DBL_MAX);
double y = sqrt(-1);

printf("-exp(DBL_MAX) is %f\n", x);
printf("sqrt(-1) is %f\n", y);
return 0;
}

/* END new.c */

N869
7.12.1 Treatment of error conditions
[#1] The behavior of each of the functions in <math.h> is
specified for all representable values of its input
arguments, except where stated otherwise.
[#2] For all functions, a domain error occurs if an input
argument is outside the domain over which the mathematical
function is defined. The description of each function lists
any required domain errors; an implementation may define
additional domain errors, provided that such errors are
consistent with the mathematical definition of the
function.182) On a domain error, the function returns an
implementation-defined value; whether the integer expression
errno acquires the value EDOM is implementation-defined.
[#3] Similarly, a range error occurs if the mathematical
result of the function cannot be represented in an object of
the specified type, due to extreme magnitude. A floating
result overflows if the magnitude of the mathematical result
is finite but so large that the mathematical result cannot
be represented, without extraordinary roundoff error, in an
object of the specified type. If a floating result
overflows and default rounding is in effect, or if the
mathematical result is an exact infinity (for example
log(0.0)), then the function returns the value of the macro
HUGE_VAL, HUGE_VALF, or HUGE_VALL according to the return
type, with the same sign as the correct value of the
function; whether errno acquires the value ERANGE when a
range error occurs is implementation-defined. The result
underflows if the magnitude of the mathematical result is so
small that the mathematical result cannot be
represented, without extraordinary roundoff error, in an
object of the
specified type. If the result underflows, the function
returns a value whose magnitude is no greater than the
smallest normalized positive number in the specified type
and is otherwise implementation-defined; whether errno
acquires the value ERANGE is implementation-defined.

--
pete

=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a

 05-18-2006
pete wrote:
> Martin Jørgensen wrote:
>
>
>>Basically I have a variable that contains -1.#IND00000000000...
>>according to MS visual studio 2005...
>>I thought that this was (-inf) so
>>
>>...
>> if( val < 0)
>> do something <- never got here....
>>
>>I hope somebody can figure this out?

>
>
> exp(DBL_MAX) is a range error.
> sqrt(-1) is a domain error.

-snip-

Yes, thanks but you're only printing out the values using printf(). You
didn't make the if-test...

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a

 05-18-2006
bert wrote:
> Martin Jørgensen wrote:
>
>>Martin Jørgensen wrote:
>>-snip-
>>
>>
>>>It would be nice with 2 functions: One that tests for plus/minus Inf and
>>>one that checks for NaN, if there's any difference? I didn't really
>>>understood the bit-pattern-difference between NaN and Inf...

>>
>>Basically I have a variable that contains -1.#IND00000000000...
>>according to MS visual studio 2005... I thought that this was (-inf) so
>>
>>...
>> if( val < 0)
>> do something <- never got here....
>>
>>I hope somebody can figure this out?
>>Martin Jørgensen

>
>
> The IEEE floating point standard says that any comparison
> in which one operand is a NaN should evaluate to FALSE.
> So the following counter-intuitive test meets your needs:
>
> if (val == val)
> do_nothing ( );
> else
> report_NaN (val);

Ok, thanks a lot - it works here and it was a lot easier than I
feared... And how do I check for plus/minus inf ?

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk

jacob navia
Guest
Posts: n/a

 05-18-2006
Martin Jørgensen a écrit :
> Hi,
>
> I found the code below from
> http://www.blitzbasic.com/Community/...hp?topic=55633
>
> ------
> ' Check if number is finite.
>
>
> Local uouble = 2 ' also works for floats
>
> For Local n = 1 To 12
> Print isfinite(u) + " " + u
> u :* u
> Next
>
> Print
> Print
>
> u = u - u
> Print isfinite(u) + " " + u
>
>
> Function isfinite( xouble ) ' assumes Intel byte order
>
> Const EXP_BITS = %01111111111100000000000000000000
>
> Local bits = ( Int Ptr Varptr x )[1] ' [0] for Mac?
>
> Return ( bits & EXP_BITS ) <> EXP_BITS ' exponent is all 1s for
> infinity or NaN
>
> End Function
>
>
> ------
>
> I need something like that - but the author writes that "This is intel
> only. Anybody want to make it multiplatform? It shouldn't be difficult,
> but I don't have a Mac".
>
> The code above doesn't look like "standard C" to me... But I would like
> this function to work *preferable* under both linux, mac and windows
> pc's....
>
> My guess is that the code should be changed to something like:
>
> int(double testval)
> {
> Const EXP_BITS = %01111111111100000000000000000000
>
> return (testval && EXP_BITS); /* returns either 0 or 1 ? */
> }
>
> But I guess EXP_BITS is completely wrong defined, so how to make it work
> - and are there better existing ways to check for Inf or NaN???
>
> It would be nice with 2 functions: One that tests for plus/minus Inf and
> one that checks for NaN, if there's any difference? I didn't really
> understood the bit-pattern-difference between NaN and Inf...
>
>
> Best regards / Med venlig hilsen
> Martin Jørgensen
>

Standard C has several functions for this:
1) isfinite: returns true if its argument is finite
2) isinf: returns true if its argument is infinite
3) isnormal: returns true if its argument is normal
4) fpclassify returns the type of its argument.

See the documentation of your compiler system.

P.S. This will work in standard C, not in Visual Studio.

=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Guest
Posts: n/a

 05-18-2006
jacob navia wrote:
-snip-

> Standard C has several functions for this:
> 1) isfinite: returns true if its argument is finite
> 2) isinf: returns true if its argument is infinite
> 3) isnormal: returns true if its argument is normal
> 4) fpclassify returns the type of its argument.
>
> See the documentation of your compiler system.

Ok.

> P.S. This will work in standard C, not in Visual Studio.

Is visual studio 2005 not standard C? I think I disabled something
that's on by default so I think it understands standard C at least... At
least I've had no problems yet with standard C on it (or is it ANSI C?
Don't know if there's any difference)....

Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk