473,883 Members | 1,697 Online

# checking double for Inf or NaN - how?

Hi,

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

------
' Check if number is finite.
Local u:Double = 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( x:Double ) ' assumes Intel byte order

Const EXP_BITS = %01111111111100 000000000000000 000

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 = %01111111111100 000000000000000 000

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
May 18 '06
55 8356
"jacob navia" <ja***@jacob.re mcomp.fr> wrote
Richard Heathfield a écrit :
jacob navia said:

"Portabilit y" for you means "taking the worst features of each
implementation" .

That's inevitable. The convoy travels at the speed of the slowest ship.

--
www.personal.leeds.ac.uk/~bgy1mm
May 20 '06 #41
Malcolm a écrit :
"Richard Heathfield" <in*****@invali d.invalid> wrote
pete said:

Richard Heathfield wrote:
C99 features are, quite simply, not even remotely as portable as C89
features.

I like the added restrictions of C99, such as no implicit int
and requiring protypes for all functions.

Likewise. Shame about the return value from main, but I stick it in
anyway,
of course.

Implicit int was a fossil and it's time it went.
However prototypes are just a nuisance. There's some case for them in a
header file, where they describe the interface to the file, but the only
reason for them in other places is to allow the compiler to check arguments
on one pass. A modern compiler should be intelligent enough to do this
without a prototype to help it along.

To know the argument for a function without prototypes the compiler
should have the source code for all functions, including those in the
libraries the program is using. This would imply that the source code
for all the system would have to be processed, loaded into memory,
before any checks could be done.

This is completely impossible, so *some* type of prototype declaration
is needed. In Pascal you have the 'interface' declarations, in C# you
have a similar construct... etc!

May 20 '06 #42
On 2006-05-20, jacob navia <ja***@jacob.re mcomp.fr> wrote:
Malcolm a écrit :
Implicit int was a fossil and it's time it went.
However prototypes are just a nuisance. There's some case for them in a
header file, where they describe the interface to the file, but the only
reason for them in other places is to allow the compiler to check arguments
on one pass. A modern compiler should be intelligent enough to do this
without a prototype to help it along.

To know the argument for a function without prototypes the compiler
should have the source code for all functions, including those in the
libraries the program is using. This would imply that the source code
for all the system would have to be processed, loaded into memory,
before any checks could be done.

This is completely impossible, so *some* type of prototype declaration
is needed. In Pascal you have the 'interface' declarations, in C# you
have a similar construct... etc!

Not to mention the fact that maintenance programmers want a prototype
to give them some idea of how a function works!

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
Get your game faces on, because this is not a game.
May 20 '06 #43
"jacob navia" <ja***@jacob.re mcomp.fr> wrote
Malcolm a écrit :
"Richard Heathfield" <in*****@invali d.invalid> wrote

However prototypes are just a nuisance. There's some case for them in a
header file, where they describe the interface to the file, but the only
reason for them in other places is to allow the compiler to check
arguments on one pass. A modern compiler should be intelligent enough to
do this without a prototype to help it along.

To know the argument for a function without prototypes the compiler should
have the source code for all functions, including those in the libraries
the program is using. This would imply that the source code for all the
system would have to be processed, loaded into memory, before any checks
could be done.

This is completely impossible, so *some* type of prototype declaration is
needed. In Pascal you have the 'interface' declarations, in C# you have a
similar construct... etc!

The library should contain information about the signature of the functions
it contains.

The problem might be the weight of libraries without this feature, and no
way of retrieving information from them.
--
www.personal.leeds.ac.uk/~bgy1mm

May 21 '06 #44
Malcolm said:
"jacob navia" <ja***@jacob.re mcomp.fr> wrote
Malcolm a écrit :
"Richard Heathfield" <in*****@invali d.invalid> wrote

No, Richard Heathfield didn't write any of the stuff quoted in your article.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 21 '06 #45

"Richard Heathfield" <in*****@invali d.invalid> wrote in message
news:0K******** *************** *******@bt.com. ..
Malcolm said:
"jacob navia" <ja***@jacob.re mcomp.fr> wrote
Malcolm a écrit :
"Richard Heathfield" <in*****@invali d.invalid> wrote

No, Richard Heathfield didn't write any of the stuff quoted in your
article.

Think of the attribution lines as prototypes.
Initially I included the bit about implicit int, but then I realised that
mentioned that point, so I snipped it. But I forgot to update the

That's the problem when you have information in two places at once.
--
www.personal.leeds.ac.uk/~bgy1mm

May 21 '06 #46
Richard Heathfield wrote:
Malcolm said:
"jacob navia" <ja***@jacob.re mcomp.fr> wrote
Malcolm a écrit :
"Richard Heathfield" <in*****@invali d.invalid> wrote

No, Richard Heathfield didn't write any of the stuff quoted in your
article.

You going to sue him?

Brian

May 21 '06 #47
Default User said:
Richard Heathfield wrote:
Malcolm said:
> "jacob navia" <ja***@jacob.re mcomp.fr> wrote
>> Malcolm a écrit :
>>> "Richard Heathfield" <in*****@invali d.invalid> wrote

No, Richard Heathfield didn't write any of the stuff quoted in your
article.

You going to sue him?

I wasn't planning on it, no. I don't consider litigation to be the best way
to settle Usenet disagreements.

But having said that... with a name like Malcolm, he's probably Scottish -
so maybe I'll make an exception in his case. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 21 '06 #48
In article <0K************ *************** ***@bt.com>,
Richard Heathfield <in*****@invali d.invalid> wrote:
Malcolm said:
"jacob navia" <ja***@jacob.re mcomp.fr> wrote
Malcolm a écrit :
"Richard Heathfield" <in*****@invali d.invalid> wrote

No, Richard Heathfield didn't write any of the stuff quoted in your article.

and I didn't have anything to do with prior articles in this thread.

Also, this is off-topic for comp.lang.c. Is there a alt.flame.attri butions?

Gordon L. Burditt
Jun 3 '06 #49
Gordon Burditt wrote:
In article <0K************ *************** ***@bt.com>,
Richard Heathfield <in*****@invali d.invalid> wrote:
Malcolm said:
"jacob navia" <ja***@jacob.re mcomp.fr> wrote
Malcolm a écrit :
> "Richard Heathfield" <in*****@invali d.invalid> wrote

No, Richard Heathfield didn't write any of the stuff quoted in your article.

and I didn't have anything to do with prior articles in this thread.

Also, this is off-topic for comp.lang.c. Is there a alt.flame.attri butions?

It was a simple case of forgetting to do a final snip after deleting the
last piece of Richard's text. It did not lead to major disputes the way
you claim.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Jun 3 '06 #50

This thread has been closed and replies have been disabled. Please start a new discussion.