Search code examples
idevb6constantsmaxminimum

How do I declare MAX_DOUBLE in VB6?


According to the MSDN help for VB6

Floating-point values can be expressed as mmmEeee or mmmDeee, in which mmm is the mantissa and eee is the exponent (a power of 10). The highest positive value of a Single data type is 3.402823E+38, or 3.4 times 10 to the 38th power; the highest positive value of a Double data type is 1.79769313486232D+308, or about 1.8 times 10 to the 308th power. Using D to separate the mantissa and exponent in a numeric literal causes the value to be treated as a Double data type. Likewise, using E in the same fashion treats the value as a Single data type.

Now in the VB6 IDE I've tried to enter this

const MAX_DOUBLE as Double = 1.79769313486232D+308

however, as soon as I move away from that line the IDE throws an Error 6 (Overflow)

An overflow results when you try to make an assignment that exceeds the limitations of the target of the assignment. ...

So how do I get MAX_DOUBLE (and MIN_DOUBLE for that matter) defined?


Solution

  • Does it have to be a Const? You can get the exact value of MAX_DOUBLE into a variable by setting the correct bit pattern using CopyMemory from a Byte array.

    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Dim Max As Double
    Dim Idx As Long
    Dim Bits(0 To 7) As Byte
    
    For Idx = 0 To 5
       Bits(Idx) = 255
    Next
    Bits(6) = 239 ' = 11101111
    Bits(7) = 127
    
    For Idx = 0 To 7
       CopyMemory ByVal VarPtr(Max) + Idx, Bits(Idx), 1
    Next
    
    Debug.Print Max
    

    Edit: I forgot that you also asked about MIN_DOUBLE, which is even easier.

    Dim Min As Double
    Dim Bits As Byte
    
    Bits = 1
    CopyMemory ByVal VarPtr(Min), Bits, 1
    
    Debug.Print Min