variant double subtype exceed max value

2019-03-01 16:36发布

问题:

when I'm looking at the "Variant Data Type" documantation, it says that variant with subtype of double can support a positive value of maximum "1.79769313486232E308" (15 digits) and that "An error occurs when Variant variables containing Currency, Decimal, and Double values exceed their respective ranges." However, when I'm running the following code:

y = 999999999999999999999999999
y = CStr(CDBL(y))
MsgBox y

I do not recive an error, instead I am getting a msgbox with the following output: "1e+27" (27 is the number of digits in y).

What is the explantion to this? how is "y" stored in the memory?

I havn't found an answer nither in variant documentation nor in CDbl function documentation.

Thanks.

回答1:

On assignment of a numerical quantity to a variant, the vbscript runtime will pick the best possible variant type to match that quantity.

In your case, the literal is too large for an integral type so it assigns it to the double "subtype".

Converting a double of this magnitude to a string will produce a string in scientific notation.

As for your documentation

"1.79769313486232E308" (15 digits)

is a little misleading. Read it as 15 significant figures which is about the precision level for a floating point double precision type.