Calculations on LARGE numbers

Discussion in 'ASP General' started by Frinton, Mar 13, 2006.

1. FrintonGuest

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.

Fred

Frinton, Mar 13, 2006

2. Richard MuellerGuest

Fred wrote:

> 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.

--
Richard
Hilltop Lab - http://www.rlmueller.net

Richard Mueller, Mar 13, 2006

3. Egbert Nierop \(MVP for IIS\)Guest

"Frinton" <> wrote in message
news:44158bca@212.67.96.135...
> 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)

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

Egbert Nierop \(MVP for IIS\), Mar 13, 2006
4. Evertjan.Guest

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>

--
Evertjan.
The Netherlands.

Evertjan., Mar 13, 2006
5. FrintonGuest

Is there any work around to this?

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

Thanks

Fred

"Richard Mueller" <> wrote in message
news:...
> Fred wrote:
>
>> 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.
>
> --
> Richard
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
>

Frinton, Mar 13, 2006
6. FrintonGuest

Im getting an overflow when I try this

Fred

"Egbert Nierop (MVP for IIS)" <> wrote in
message news:...
>
> "Frinton" <> wrote in message
> news:44158bca@212.67.96.135...
>> 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)
>
>> I have a suspition is could be down to one of the number functions I am
>> using along the way but im not sure.

>

Frinton, Mar 13, 2006
7. FrintonGuest

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

Fred

"Evertjan." <> wrote in message
news:Xns9785B2358D898eejj99@194.109.133.242...
> 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>
>
>
> --
> Evertjan.
> The Netherlands.

Frinton, Mar 13, 2006
8. Tim SlatteryGuest

"Frinton" <> wrote:

>
>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.

--
Tim Slattery
MS MVP(DTS)

Tim Slattery, Mar 13, 2006
9. Jerold SchulmanGuest

On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" <> wrote:

>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.
>
>
>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

Jerold Schulman, Mar 13, 2006
10. FrintonGuest

"Jerold Schulman" <> wrote in message
news:...
> On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" <>
> wrote:
>
>>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.
>>
>>
>>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

Frinton, Mar 13, 2006
11. FrintonGuest

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

"Jerold Schulman" <> wrote in message
news:...
> On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" <>
> wrote:
>
>>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.
>>
>>
>>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

Frinton, Mar 13, 2006
12. Richard MuellerGuest

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.

--
Richard
Hilltop Lab - http://www.rlmueller.net

"Frinton" <> wrote in message
news:4415a442@212.67.96.135...
>
> Is there any work around to this?
>
> Im storing the values in SQL, is BigInt the largest exact number possible?
>
> Thanks
>
> Fred
>
> "Richard Mueller" <> wrote in message
> news:...
>> Fred wrote:
>>
>>> 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.
>>
>> --
>> Richard
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab - http://www.rlmueller.net
>>

>
>

Richard Mueller, Mar 13, 2006
13. Dave AndersonGuest

Frinton wrote:
> 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.

Dave Anderson, Mar 13, 2006
14. Richard MuellerGuest

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)

--
Richard
Hilltop Lab - http://www.rlmueller.net

"Frinton" <> wrote in message
news:4415b30c@212.67.96.135...
>
> 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
>
>
>
>
>
> "Jerold Schulman" <> wrote in message
> news:...
>> On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" <>
>> wrote:
>>
>>>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.
>>>
>>>
>>>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

>
>

Richard Mueller, Mar 13, 2006
15. Evertjan.Guest

Frinton wrote on 13 mrt 2006 in microsoft.public.inetserver.asp.general:
> "Evertjan." <> wrote in message
> news:Xns9785B2358D898eejj99@194.109.133.242...
>> 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>

[please do not toppost on usenet]

>
> 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.

--
Evertjan.
The Netherlands.

Evertjan., Mar 13, 2006
16. Dr John StocktonGuest

JRS: In article <44158bca@212.67.96.135>, dated Mon, 13 Mar 2006
15:12:09 remote, seen in news:microsoft.public.scripting.vbscript,
Frinton <> posted :

>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.

--
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

Dr John Stockton, Mar 13, 2006
17. Richard MuellerGuest

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

--
Richard
Hilltop Lab - http://www.rlmueller.net

"Richard Mueller" <> wrote in message
news:%...
> 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.
>
> --
> Richard
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
>
> "Frinton" <> wrote in message
> news:4415a442@212.67.96.135...
>>
>> Is there any work around to this?
>>
>> Im storing the values in SQL, is BigInt the largest exact number
>> possible?
>>
>> Thanks
>>
>> Fred
>>
>> "Richard Mueller" <> wrote in
>> message news:...
>>> Fred wrote:
>>>
>>>> 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.
>>>
>>> --
>>> Richard
>>> Microsoft MVP Scripting and ADSI
>>> Hilltop Lab - http://www.rlmueller.net
>>>

>>
>>

>
>

Richard Mueller, Mar 13, 2006
18. Egbert Nierop \(MVP for IIS\)Guest

"Frinton" <> wrote in message
news:4415a490@212.67.96.135...
>
> Im getting an overflow when I try this

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

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

> Fred

Egbert Nierop \(MVP for IIS\), Mar 14, 2006
19. Jerold SchulmanGuest

Re: Re: Calculations on LARGE numbers

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?

On Mon, 13 Mar 2006 23:33:01 +0000, Dr John Stockton <> wrote:

>JRS: In article <44158bca@212.67.96.135>, dated Mon, 13 Mar 2006
>15:12:09 remote, seen in news:microsoft.public.scripting.vbscript,
>Frinton <> posted :
>
>>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.
>

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

Jerold Schulman, Mar 14, 2006
20. Dave AndersonGuest

Richard Mueller wrote:
> 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.

Dave Anderson, Mar 14, 2006