View Full Version : BIG numbers...
krycek
11-18-2002, 12:39 AM
I'm doing a few sums at the moment which require an EXACT number to be stored. Now, this number could potentially be bigger than the max allowable by the computer... so, how do I still process it?
Second, if I have three integers, 1234, 0056, 0367 and I want to combine them so that they make one number: 123400560367, what is the best way to do it? Is it to convert each to a string, and then combine the strings, and then convert back into a number?
And what if that number is more than the allowable maximum... how do I still do sums with it... you see where I am going with this.
::] krycek [::
glenngv
11-18-2002, 01:29 AM
in your example regarding combining numbers
1234, 0056, 0367
if the variable that hold each number is of type integer, the leading zeroes would be chopped off, so the desired ouput of 123400560367 would not be achieved.
how come you would have such a datatype with leading zeroes? Maybe it's a user input or you manipulate it as string and add those zeroes? By then, you wouldn't have to convert it as integer since you would just append each number. Does that make sense? :)
Anyway, if all variables are really of type integer, you'd combine it this way:
strNum = "" + iNum1 + iNum2 + iNum3;
then to convert to integer again:
iNum = new Number(strNum);
and you can find the properties and methods for Number object below:
http://www.devguru.com/Technologies/ecmascript/quickref/number.html
krycek
11-18-2002, 02:03 AM
...how would I perform a calculation with such a number? For instance, an integer 4000 digits long, minus another integer 3000 digits long... how could I do that?
::] krycek [::
beetle
11-18-2002, 02:31 AM
Originally posted by krycek
...how would I perform a calculation with such a number? For instance, an integer 4000 digits long, minus another integer 3000 digits long... how could I do that?
::] krycek [:: Not gonna happen. Any Number type variable greater than 1e+308 returns 'Infinity'. Try it, floating-point literals will take exponential notation...
alert(1E308 + " | " + 2E308);
Youre asking for no less than 1e+2999, far above the limit :D
What sort of calculations are you performing that need 3000+ significant digits of precision? Javascript shouldn't be the programming language of choice for the aspiring astrophysicist, you know :p
EDIT: The last legit math problem I had to solve (in high school) invloving numbers this big was to caclulate this: Assuming the earth is a perfect sphere (we were given a radius) by how many cubic centimeters would the earth's volume decrease if the surface area was decreased by only 1 square meter. The answer was surprisingly large, and required LOTS of precision to be accurate (I don't remember the exact number of significant digits....)
Netscape exposes Java's BigInteger class through LiveConnect. It should be able to handle anything you'll throw at it. Same thing with BigDecimal:
var myHugeNumber = java.math.BigInteger('some string of arbitrary length of all digits');
NS4+ only.
krycek
11-18-2002, 01:40 PM
...can I use that java big stuff or something similar any other way?
What I would like to do is to create my own function whereby given a string of numbers, it performs the calculation by chopping the string up, doing the business on each bit somehow, and recombining. I know that is how some calculators work, but no source! ...does anyone know how to do that? Is it easy?
...beetle, the reason I need such big numbers is because I am experimenting with a lossless compression algorithm. I am not sure if my technique is even possible, but I am learning a lot about maths on the way... ;)
::] krycek [::
krycek
11-18-2002, 02:56 PM
oh, and beetle...
V = (4/3) * pi * (r^3)
or (4/3) * Math.PI * Math.pow(r, 3) in JS
SA = 4 * pi * (r^2)
or 4 * Math.PI * Math.pow(r, 2) in JS
...so V as an expression of SA could be...
(4/3) * pi * ((sqrt(SA/(4*pi)))^3)
so in order to produce the difference in volume if the surface area reduces by m^2...
(4/3)*pi*((sqrt((4*pi*(r^2))/(4*pi)))^3) - (4/3)*pi*((sqrt(((4*pi*(r^2))-m^2)/(4*pi)))^3)
the answer will be in m^3 (cubic metres) so we have to change that to cubic centimetres... to do that, we multiply the answer by 1000^3 (1000 cm in 1 m so 1000^2 in one sq m and 1000^3 in one cube m).
((4/3)*pi*((sqrt((4*pi*(r^2))/(4*pi)))^3) - (4/3)*pi*((sqrt(((4*pi*(r^2))-m^2)/(4*pi)))^3)) * (1000^3)
...so to solve your problem, we just have to specify 1 for m, and give the formula the radius of the Earth in metres. To do this, I have used the MFE (Multigen Flat Earth) value, which is an average of the equatorial and polar radii etc. giving a value that would be approximately accurate if the Earth was a perfect sphere.
The value is:
6366707.02m
So, feeding that into the equation gives:
((4/3)*pi*((sqrt((4*pi*(6366707.02^2))/(4*pi)))^3) - (4/3)*pi*((sqrt(((4*pi*(6366707.02^2))-1^2)/(4*pi)))^3)) * (1000^3)
...well, you were right about it being surprisingly big...
3183353509999998.43762499012504242102806904620517566481385691162728269784725628070513412598313643277 1531989766300364574948917269407672438053288134615591317718136255139218082513252479041451492934320746 5431796889056547490794777958504055214891490149417477612734501143189446132709256134438908019054116426 0908305137787891643019797866815133978166198442091900934420616508291962807193625112069359446032416107 1179241767326930559165823797781503105655476261025097925271760031243812777082537891753232282684218655 4335606203697
I got that answer by using 512 bits in processing. If I stick to 32 bits I get:
3183353509999998.4376249134512622
so essentially the number is 3183353509999998.437625 (after that they start do diverge according to accuracy method)
that's a lot of cubic centimetres!
3183353.5 cubic metres, which I have worked out is approximately the same capacity as the Millennium Dome in London.
OK well I did it, beetle - took me about 15 mins to type this out... did I get it right? :D
::] krycek [::
beetle
11-18-2002, 05:30 PM
Originally posted by krycek
OK well I did it, beetle - took me about 15 mins to type this out... did I get it right? :DBeats the crap outta me! I did that like 7-8 years ago. I knew you'd be tempted to solve it! :D
krycek
11-18-2002, 05:42 PM
Originally posted by beetle
Beats the crap outta me! I did that like 7-8 years ago. I knew you'd be tempted to solve it! :D
lol! ;) well, I can never resist a challenge...
...plus I was genuinely intrigued as to how much difference just one metre would make... and surprised at the result, but when I think about it, it must be right.
:)
::] krycek [::
RadarBob
11-18-2002, 08:23 PM
well, I can never resist a challenge...
How much wood could a woodchuck chuck if a woodchuck could chuck wood?
Answer in cords, please.
krycek
11-18-2002, 08:43 PM
Originally posted by RadarBob
How much wood could a woodchuck chuck if a woodchuck could chuck wood?
Answer in cords, please.
'n' cords ;)
...very funny, btw :D
::] krycek [::
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.