Calculations on LARGE numbers

F

Frinton

Hi,

I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it
doesn't get it quite right. Its always somewhere between 10 and and 5000 out
:(

I have a suspition is could be down to one of the number functions I am
using along the way but im not sure.

Any help or guidance would be greatly appriciated.

Thanks in advance

Fred
 
R

Richard Mueller

Fred said:
I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it
doesn't get it quite right. Its always somewhere between 10 and and 5000
out :(

I have a suspition is could be down to one of the number functions I am
using along the way but im not sure.

Any help or guidance would be greatly appriciated.

VB and VBScript integers are Long datatypes (32-bit) and range
from -2,147,483,648 to 2,147,483,647, which is -2^31 to 2^31-1. Larger
values are handled as Float datatype which is 64-bit. Your large number is
stored as an approximation.
 
E

Egbert Nierop \(MVP for IIS\)

Frinton said:
Hi,

I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it
doesn't get it quite right. Its always somewhere between 10 and and 5000
out :(

The best is to convert it to currency.
dim v
v=CCur(10)
v=v * ccur(103030303030.1034)
 
E

Evertjan.

Frinton wrote on 13 mrt 2006 in microsoft.public.inetserver.asp.general:
I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it
doesn't get it quite right. Its always somewhere between 10 and and
5000 out
:(

I have a suspition is could be down to one of the number functions I
am using along the way but im not sure.

Any help or guidance would be greatly appriciated.

<http://en.wikipedia.org/wiki/Long_division>
 
F

Frinton

Thanks for the reply

Is there any work around to this?

Im storing the values in SQL, is BigInt the largest exact number possible?

Thanks

Fred
 
F

Frinton

Thanks for the reply

Its not the calculation that is causing me the problem its the storing of
values

Fred
 
T

Tim Slattery

Frinton said:
Thanks for the reply

Its not the calculation that is causing me the problem its the storing of
values

In VB or VBScript there's probably no way out of this. Java (not
JavaScript) has a BigInteger class, and there are classes available
for C++ that can handle integers of arbitrary precision.

Arithmetic operations in these classes would not be fast, but they
would preserve and use the full precision.
 
J

Jerold Schulman

Hi,

I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it
doesn't get it quite right. Its always somewhere between 10 and and 5000 out
:(

I have a suspition is could be down to one of the number functions I am
using along the way but im not sure.

Any help or guidance would be greatly appriciated.

Thanks in advance

Fred
See tip 4533 » How do I perform accurate and/or complex math in a batch? 11-Dec-01
in the 'Tips & Tricks' at http://www.jsifaq.com

When I type the following at a CMD.EXE prompt:
for /f %i in ('domath //nologo "Round(7768489957892578474792094/12280)"') do @echo %i

I get 6.32613188753467E+20




Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com
 
F

Frinton

Thanks for the reply

The trouble is when I format this result into a "readable" number (using
vbscripts FormatNumber function) it comes out as 632,613,188,753,467,000,000

The true answer and the one im looking for is

632,613,188,753,467,302,507

Regards

Fred
 
R

Richard Mueller

Hi,

As I recall BigInt is 64-bit, but valid values range from -2^63
(-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807).
VB.NET can handle values in this range, but VB6 and VBScript cannot. In VB6
I've used Currency datatypes to handle large numbers. VB6 Currency values
are 64-bit scaled integers (4 digits past the decimal, saved internally as
an integer), so the maximum value is 922,337,203,685,477.5807. I'm not sure
about VBScript currency values. I've also written my own functions to do
math by breaking the value up into parts, like two 31-bit values (high and
low parts). However, my functions multiplied numbers. I remember having to
code my own CInt Function because the VB function bombs out at 2^15.
Division seems harder.

Your value is 7.77 x 10^24, which would require an 84-bit register. You
could represent it as two 42-bit numbers. However, even the 42-bit values
are too large for VB6 or VBScript. That leaves you with representing the
value as 3 30-bit numbers.

x = a * 2^60 + b * 2^30 + c

That would be a lot of work.
 
D

Dave Anderson

Frinton said:
I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter
what I do it doesn't get it quite right. Its always
somewhere between 10 and and 5000 out :(

Check out the multiple-precision libraries here:
http://www.ohdave.com/rsa/



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
R

Richard Mueller

I wonder if that confirms that the value was handled internally as a
currency, a scaled 64-bit integer. I note that I get the same answer if I
omit the Round function. I used the following VBScript program:

x= Eval("(7768489957892578474792094/12280)")

Wscript.Echo FormatNumber(x, 4)
 
E

Evertjan.

Frinton wrote on 13 mrt 2006 in microsoft.public.inetserver.asp.general:
[please do not toppost on usenet]

Thanks for the reply

Its not the calculation that is causing me the problem its the storing
of values

That is no problem, you can store them as a string.

In fact, you do the long division also with string parts.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 13 Mar 2006
15:12:09 remote, seen in
Frinton said:
I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it
doesn't get it quite right. Its always somewhere between 10 and and 5000 out
:(

I have a suspition is could be down to one of the number functions I am
using along the way but im not sure.

Others have explained size limits and accuracy. But, at a DOS prompt :

LONGCALC 7,768,489,957,892,578,474,792,094 12,280 div wrt

LONGCALC: www.merlyn.demon.co.uk >= 2005-07-22
compiled with Borland Delphi.
+632,613,188,753,467,302,507

(remainder was 6,134)

LONGCALC handles up to 65520 or 99999999 digits, base 2..16, integers,
programmed in RPN. If you want the answers, you may use it; but it's
not VBS (and not quick). Via sig line 3. VASTCALC is a GUI version.
 
R

Richard Mueller

I find that VBScript can represent 2^48 and 2^49 exactly, but not 2^50.

I see no way to even break up your number into high and low parts, much less
do math with them. For example, if I attempt to represent your large number
as:

x = a * (2^42) + b

I can possibly find a, but not b.

a = IntegerPart(x/(2^42))

where IntegerPart must be coded, since CInt has no chance of working. Then

b = x - a * (2^42)

but VBScript cannot represent a * (2^42) exactly, so b is wrong. I see no
chance of doing long division if I cannot even break up the number as above.
VBScript cannot do any math exactly where any intermediate value is greater
than about 2^48.
 
E

Egbert Nierop \(MVP for IIS\)

Frinton said:
Thanks for the reply

Im getting an overflow when I try this :(

sad.

More sadness :)

The OLEautomation runtime Oleaut32.dll supports 8 byte integers (V_I8), they
should be called 'very long' or so...
But vbscript has been feature-frozen, so it does not match the current OS
when we think about calculation.

You might try vbscript.net that -has- support for 8 byte calculations.
 
J

Jerold Schulman

Is there any documentation on LONGCALC?
What does wrt mean?
I can figure out how to use add, sub, mul, div/mod/srt, pow.
How do I do use the result of 1 calculation in another calculation?


JRS: In article <[email protected]>, dated Mon, 13 Mar 2006
15:12:09 remote, seen in

Others have explained size limits and accuracy. But, at a DOS prompt :

LONGCALC 7,768,489,957,892,578,474,792,094 12,280 div wrt

LONGCALC: www.merlyn.demon.co.uk >= 2005-07-22
compiled with Borland Delphi.
+632,613,188,753,467,302,507

(remainder was 6,134)

LONGCALC handles up to 65520 or 99999999 digits, base 2..16, integers,
programmed in RPN. If you want the answers, you may use it; but it's
not VBS (and not quick). Via sig line 3. VASTCALC is a GUI version.

Jerold Schulman
Windows Server MVP
JSI, Inc.
http://www.jsiinc.com
http://www.jsifaq.com
 
D

Dave Anderson

Richard said:
I find that VBScript can represent 2^48 and 2^49 exactly,
but not 2^50.

That depends on what you mean by "represent". Consider:

p = 2^53

For i=-10 To 10
Response.Write(Represent(p+i) & "<br>")
Next

Function Represent(N)
M = Int(N/1000)
R = N - 1000*M
If (M = 0) Then
Represent = R
Else
Represent = Represent(M) & "," & Right("00" & R,3)
End If
End Function

[Disclaimer: only "works" with positive integers]

I chose 2^53 for a reason. VBScript can distinguish between individual
integers up through 2^53. Above 2^53, numbers are spaced by 2. Above 2^54,
they are spaced by 4. Above 2^55, by 8. I'll let you work out the sequence
above that.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top