4

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?

bugmagnet
  • 7,631
  • 8
  • 69
  • 131
  • 1
    If the IDE shows you that message as soon as you move away from the line, you might want to switch off Auto Syntax Check in the options. Many people find those incessant message boxes irritating. http://stackoverflow.com/questions/664370/your-favorite-visual-basic-6-tools-and-tips/667225#667225 – MarkJ May 30 '09 at 06:52
  • I'm not sure what you'd even use this for. Test to see if a variable is greater than MAX_DOUBLE? LOL – Bob May 31 '09 at 16:09
  • @Bob: lots of uses, for instance as a sentinel value. http://en.wikipedia.org/wiki/Sentinel_value – MarkJ Jun 01 '09 at 13:08
  • But isn't that one of the major reasons we have Null? Yes, it implies the use of Variant but at least it saves you from the hazards of Magic Number values that fall within the natural range of valid ones. Performance is important, but not as important as avoiding engineered-in pitfalls. – Bob Jun 05 '09 at 21:11

4 Answers4

6

Edit: Solved it!

Const test As Double = 1.79769313486231E+308 + 5.88768018655736E+293

Double checked it down to the binary level, that should be as high as you can go. You can keep adding values like 1 etc but it yields a number equal to, not greater than. Output is this: 01111111|11101111|11111111|11111111|11111111|11111111|11111111|11111111 Which is indeed DoubleMax

Old: You could just use Positive infinity.

Community
  • 1
  • 1
Oorang
  • 6,630
  • 1
  • 35
  • 52
  • True. I suppose I wanted it just for completeness's sake. In the same way that the inventors of VB just _had to have IMP and EQV as well as AND, OR, NOT and XOR. – bugmagnet Jun 01 '09 at 07:58
  • OK, I did some digging around and updated the code to include positive and negative values. Doesn't quite get it to you in a const, but it's progress. – Oorang Jun 01 '09 at 09:18
  • I just confirmed this gives the same result as raven's answer using CopyMemory (the result of each shows the same in the Locals window and ``theOne = theOther`` resolves to ``True`` – Mark E. May 08 '22 at 00:52
4

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
raven
  • 18,004
  • 16
  • 81
  • 112
Jim Mack
  • 209
  • 1
  • 2
  • 3
    +1 You might want to include the declare for CopyMemory just for completeness. Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" ( hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long) Obviously we must always remember to credit Bruce McKinney whenever we even think of posting CopyMemory :) http://support.microsoft.com/kb/129947 – MarkJ Jun 02 '09 at 17:12
  • This is cool to see a Double built one byte at a time. I wonder though about it referring to a byte array as Bits(0 to 7). It makes it look like you're building a single byte with 8 bits, but you're actually filling an array of 8 bytes, right? – Mark E. May 08 '22 at 01:03
0

Obvious pragmatic workaround: reduce the number slightly.

Const MAX_DOUBLE As Double = 1.79769313486231E+308

I imagine that'll be adequate in most situations.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • In fact, the underlying value isn't ...6232, it's rounded to that for display. It's actually about ...6231557, so your suggestion of ...6231 is close enough for any practical purpose. – Jim Mack May 30 '09 at 21:43
-2

Use an "E" for exponent in the number instead of a "D" like this below.

Public Const MAX_DOUBLE = 1.79769313486232E+308

[edit]

Take a look at this link below, scroll to the bottom. This concrete code example shows how this construct is being employed. Hopefully this helps.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22555684.html

James
  • 12,636
  • 12
  • 67
  • 104