439,977 Members | 1,352 Online
Need help? Post your question and get tips & solutions from a community of 439,977 IT Pros & Developers. It's quick & easy.

# Calculations on LARGE numbers

 P: n/a 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 Mar 13 '06 #1
22 Replies

 P: n/a 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 Mar 13 '06 #2

 P: n/a "Frinton" wrote in message news:44******@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. Mar 13 '06 #3

 P: n/a 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. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) Mar 13 '06 #4

 P: n/a 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 "Richard Mueller" wrote in message news:uA**************@TK2MSFTNGP12.phx.gbl... 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 Mar 13 '06 #5

 P: n/a Thanks for the reply Im getting an overflow when I try this :( Fred "Egbert Nierop (MVP for IIS)" wrote in message news:uJ**************@tk2msftngp13.phx.gbl... "Frinton" wrote in message news:44******@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. Mar 13 '06 #6

 P: n/a Thanks for the reply Its not the calculation that is causing me the problem its the storing of values Fred "Evertjan." wrote in message news:Xn********************@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. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) Mar 13 '06 #7

 P: n/a "Frinton" wrote: Thanks for the replyIts not the calculation that is causing me the problem its the storing ofvalues 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) Sl********@bls.gov Mar 13 '06 #8

 P: n/a On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" wrote: Hi,I am trying to do some calculations on large numbers (ie7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do itdoesn'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 amusing along the way but im not sure.Any help or guidance would be greatly appriciated.Thanks in advanceFred 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 Mar 13 '06 #9

 P: n/a "Jerold Schulman" wrote in message news:lv********************************@4ax.com... On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" wrote:Hi,I am trying to do some calculations on large numbers (ie7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do itdoesn't get it quite right. Its always somewhere between 10 and and 5000out:(I have a suspition is could be down to one of the number functions I amusing along the way but im not sure.Any help or guidance would be greatly appriciated.Thanks in advanceFred 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 Mar 13 '06 #10

 P: n/a 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 "Jerold Schulman" wrote in message news:lv********************************@4ax.com... On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" wrote:Hi,I am trying to do some calculations on large numbers (ie7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do itdoesn't get it quite right. Its always somewhere between 10 and and 5000out:(I have a suspition is could be down to one of the number functions I amusing along the way but im not sure.Any help or guidance would be greatly appriciated.Thanks in advanceFred 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 Mar 13 '06 #11

 P: n/a 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:44******@212.67.96.135... 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 "Richard Mueller" wrote in message news:uA**************@TK2MSFTNGP12.phx.gbl... 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 Mar 13 '06 #12

 P: n/a 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. Mar 13 '06 #13

 P: n/a Frinton wrote on 13 mrt 2006 in microsoft.public.inetserver.asp.general: "Evertjan." wrote in message news:Xn********************@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. [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. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) Mar 13 '06 #14

 P: n/a 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 Microsoft MVP Scripting and ADSI Hilltop Lab - http://www.rlmueller.net "Frinton" wrote in message news:44******@212.67.96.135... 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 "Jerold Schulman" wrote in message news:lv********************************@4ax.com... On Mon, 13 Mar 2006 15:12:09 -0000, "Frinton" wrote:Hi,I am trying to do some calculations on large numbers (ie7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do itdoesn't get it quite right. Its always somewhere between 10 and and 5000out:(I have a suspition is could be down to one of the number functions I amusing along the way but im not sure.Any help or guidance would be greatly appriciated.Thanks in advanceFred 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 Mar 13 '06 #15

 P: n/a JRS: In article <44******@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 (ie7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do itdoesn'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 amusing 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. -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. © Web - FAQqish topics, acronyms & links. PAS EXE TXT ZIP via . Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036) Mar 13 '06 #16

 P: n/a 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. -- Richard Microsoft MVP Scripting and ADSI Hilltop Lab - http://www.rlmueller.net "Richard Mueller" wrote in message news:%2****************@TK2MSFTNGP11.phx.gbl... 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:44******@212.67.96.135... 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 "Richard Mueller" wrote in message news:uA**************@TK2MSFTNGP12.phx.gbl... 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 Mar 14 '06 #17

 P: n/a "Frinton" wrote in message news:44******@212.67.96.135... 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. Fred Mar 14 '06 #18

 P: n/a 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 <44******@212.67.96.135>, dated Mon, 13 Mar 200615:12:09 remote, seen in news:microsoft.public.scripting.vbscript,Frinton posted :I am trying to do some calculations on large numbers (ie7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do itdoesn'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 amusing 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'snot 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 Mar 14 '06 #19

 P: n/a 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) & "
") 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. Mar 14 '06 #20

 P: n/a JRS: In article , dated Mon, 13 Mar 2006 17:50:35 remote, seen in news:microsoft.public.scripting.vbscri pt, Richard Mueller posted :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 lessdo math with them. For example, if I attempt to represent your large numberas:x = a * (2^42) + bI can possibly find a, but not b.a = IntegerPart(x/(2^42))where IntegerPart must be coded, since CInt has no chance of working. Thenb = x - a * (2^42)but VBScript cannot represent a * (2^42) exactly, so b is wrong. I see nochance 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 greaterthan about 2^48. Eschew excessive quotation. You have not thought the matter through sufficiently. Clearly, given a need to calculate 123456789 * 987654321, one can break those numbers into their individual digits, and long-multiply them as you should have been taught at school. My longcalc.pas does that, using digits base 2..16. One can also split them as 123 456 789 * 987 654 321, effectively base 1000, to do the job quicker. AIUI, ordinary integers in VBscript are held in IEEE Doubles, so that one could work to base 2^26 with digit- products up to 2^52. Base 1000000 easily fits. The large numbers cannot, of course, be supplied accurately as individual VBS numbers; strings or arrays are needed. There's a trick for speeding multiplication of large numbers; it's in ALGORITHMICS by Brassard & Bratley, ISBN 0-13-023169-X -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 © TP/BP/Delphi/&c., FAQqy topics & links; clpdmFAQ; news:borland.* Guidelines Mar 14 '06 #21

 P: n/a "Dr John Stockton" wrote in message news:Rd**************@merlyn.demon.co.uk... JRS: In article , dated Mon, 13 Mar 2006 17:50:35 remote, seen in news:microsoft.public.scripting.vbscri pt, Richard Mueller posted :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, muchlessdo math with them. For example, if I attempt to represent your largenumberas:x = a * (2^42) + bI can possibly find a, but not b.a = IntegerPart(x/(2^42))where IntegerPart must be coded, since CInt has no chance of working. Thenb = x - a * (2^42)but VBScript cannot represent a * (2^42) exactly, so b is wrong. I see nochance of doing long division if I cannot even break up the number asabove.VBScript cannot do any math exactly where any intermediate value isgreaterthan about 2^48. Eschew excessive quotation. You have not thought the matter through sufficiently. Clearly, given a need to calculate 123456789 * 987654321, one can break those numbers into their individual digits, and long-multiply them as you should have been taught at school. My longcalc.pas does that, using digits base 2..16. One can also split them as 123 456 789 * 987 654 321, effectively base 1000, to do the job quicker. AIUI, ordinary integers in VBscript are held in IEEE Doubles, so that one could work to base 2^26 with digit- products up to 2^52. Base 1000000 easily fits. The large numbers cannot, of course, be supplied accurately as individual VBS numbers; strings or arrays are needed. There's a trick for speeding multiplication of large numbers; it's in ALGORITHMICS by Brassard & Bratley, ISBN 0-13-023169-X -- © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3 Turnpike 4 © TP/BP/Delphi/&c., FAQqy topics & links; clpdmFAQ; news:borland.* Guidelines John, I was saying I couldn't work it out breaking the number up into high and low 2^42 parts. I think you are correct that each "digit" must be 2^26. That is, the number must be broken up into 26 bit parts (or less). In the poster's example, it cannot be done in 2 parts or even 3. The big number would have to be broken up into 4 26-bit parts. Rather than inventing that wheel, I'd suggest your program. -- Richard Microsoft MVP Scripting and ADSI Hilltop Lab - http://www.rlmueller.net Mar 15 '06 #22

 P: n/a If it is a one-time thing you can download the free version of Dolphin SmallTalk which handles numbers of any size. For example you can evaluate 1000 factorial. in a fraction of a second. If you need to program something you can use Java with the BigInteger class. Apr 7 '06 #23

### This discussion thread is closed

Replies have been disabled for this discussion.