Just a quick question here about adding very large integers together.
I'm trying to add say: 148991+ 76561197960265728 - within a vbscript
routine and my result is: 7.65611979604147E+16. Is there anyway to get
that to print out the full solution rather than the shortened version?
Thank you very much in advance.
Regards,
Nick
CDec(148991 + 76561197960265728)
in VB I get....
76561197960414700
But CDec seems to be missing in VBScript.
While the Decimal type is actually a variant
of subtype decimal, I'm not aware of any
method available to script that can directly
alter a variant's subtype.
--
HTH,
Bob Barrows
Thanks both of you for the quick responses:
If you do the math using a regular scientific calculator, you'll get
148991 + 76561197960265728 = 76561197960414719 (this is the correct
response)
When I do the math using 'formatnumber(148991+ 76561197960265728)',
I'm returning '76,561,197,960,414,700.00' - which seems to me it's
just zero'ing out the outer 2 digits.
My original guess was that vbscript isn't capable of such math with
large digits?
Thank you guys so much for your time.
I use the FormatNumber function. For example:
lngValue1 = 148991
lngValue2 = 76561197960265728
lngValue3 = lngValue1 + lngValue2
Wscript.Echo FormatNumber(lngValue3, 0)
The second parameter specifies the number of digits after the decimal point.
However, VBScript will display 0's after the first 15 digits. The above
results in:
76,561,197,960,414,700
Internally, VBScript is accurate to 53 bits, which is almost 16 digits. For
example:
Wscript.Echo FormatNumber(lngValue3 - 76561000000000000, 0)
results in 197,960,414,720. We get one extra digit (approximately).
Depending on your purpose, if you need more accuracy you must break up the
numbers into high and low parts and code your own math methods.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
---------
As I explained in my other post, the problem is that all values in VBScript
are only accurate to 53 bits, which is just over 15 decimal digits. However,
you can code methods to get more digits. For example, here is a crude
function to add two integers:
=========
Wscript.Echo AddInts(148991, 76561197960265728)
Function AddInts(lngValue1, lngValue2)
Dim V1Hi, V1Lo, V2Hi, V2Lo
Dim Temp1, Temp2, Carry, A1, A2, A3
V1Hi = Fix(lngValue1 / (10^10))
V1Lo = lngValue1 - (V1Hi * (10^10))
V2Hi = Fix(lngValue2 / (10^10))
V2Lo = lngValue2 - (V2Hi * (10^10))
Temp1 = V1Lo + V2Lo
Carry = Fix(Temp1 / (10^10))
A3 = Temp1 - (Carry * (10^10))
Temp2 = V1Hi + V2Hi + Carry
A1 = Fix(Temp2 / (10^10))
A2 = Temp2 - (A1 * (10^10))
AddInts = CStr(A1) & CStr(A2) & CStr(A3)
End Function
========
The trick is to break up all integers into high and low parts and do the
math "manually" (handling intermediate values like the carry). You need to
check that all intermediate values have no more than 15 digits. The above
adds 10 digit numbers, so the intermediate result is at most 11 digits. If
you tried a similar function to multiply, 10 digits would be too many, since
intermediate results would have up to 20 digits. You get the idea. The point
is that it can be done, but is not pretty. Also, you need to be aware of the
limitations of VBScript functions. I use the Fix function because it has no
limitations. The CInt function, for example, overflows at 32768.
Oh, I didn't get that you were concerned with the rounding. Sorry. Yes,
vbscript has limitations with large numbers. See Richard's reply.
--
HTH,
Bob Barrows
Worked perfect Richard. You are the man! Happy New Year!