Tuesday, May 19, 2009

[VB6] MAXDOUBLE, MINDOUBLE, +INFINITY, -INFINITY and NaN

VB6 doesn't seem to be able to specify that a Double contains MAXDOUBLE, MINDOUBLE, +INFINITY, -INFINITY or NaN. (I could be wrong on that as there may be some undocumented feature hiding in there somewhere.)

Anyway, I came up with a way of storing these values into Doubles so that they could be used in a Complex Numbers library I've been writing.

Essentially, I create an 8 byte array, load it up with the relevant values, and then, using API calls and the VarPtr function, store the contents of the array into the storage used by the Double.

Here's the code. First the declarations.


Then the routine that does the work.


Finally, a slice out of the Complex Numbers project demonstrating the use of some of these declarations.


Enjoy!


© Copyright Bruce M. Axtens, 2009

5 comments:

Anonymous said...

Your quiet NaN seems to have the first 4 bits of the mantissa being 0000. That's a signalling NaN, isn't it. I think your byteArray(6) should be set to &FF.

Rick Regan said...

You've defined mindbl as -maxdbl. Usually, DBL_MIN (as it would be called in C) is the smallest POSITIVE number -- actually, the smallest normalized positive number -- which is 2^-1022.

In 'raw' format, 2^-1022 is the floating-point number with a sign field of 0, an exponent field of 1, and a fraction field of 0 (see my post http://www.exploringbinary.com/what-powers-of-two-look-like-inside-a-computer/ for more info).

Doctor Binary said...

You've defined mindbl as -maxdbl. Usually, DBL_MIN (as it would be called in C) is the smallest POSITIVE number -- actually, the smallest normalized positive number -- which is 2^-1022.

In 'raw' format, 2^-1022 is the floating-point number with a sign field of 0, an exponent field of 1, and a fraction field of 0 (see my post http://www.exploringbinary.com/what-powers-of-two-look-like-inside-a-computer/ for more info).

Mark said...

There is a much simpler way to get +infinity and -infinity.
Dim fPosInf As Double, fNegInf as Double
on error resume next
fPosInf=1/0
fNegInf=-fPosInf

From StackOverflow:
http://stackoverflow.com/questions/885994/how-do-you-get-vb6-to-initialize-doubles-with-infinity-infinity-and-nan/886148#886148

Bruce M. Axtens said...

Ah, yes, that link ... ummm ... you see, err, 'boost' is me. I posted this article and then went to StackOverflow in search of a better way of doing things. And they provided me with more than one.