Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: KamikazeHighland on Sun 04/03/2012 23:02:48

Title: Calculating, Mostly Addition
Post by: KamikazeHighland on Sun 04/03/2012 23:02:48
I wrote a program to multiply or add any two numbers where neither is negative or has a decimal.  It's the equivalent of pen and paper arithmetic, so it's slow but at GameSpeed 40 it can multiply two 155 digit numbers 100 times in under a minute and is never wrong.

I don't -really- need to use numbers beyond maybe 20 digits each or do it very often, my only concern is that when I script to multiply or add two numbers the answer is never wrong.  Normally ints will be wrong beyond â€"2,147,483,648 to 2,147,483,647 and I cannot trust floats.  I don't need to use negative numbers or decimals at this point.

I've come up with better ways to multiply, subtract and divide, but I'm stuck on addition because carrying is slow and I can't find many resources on how to do that quicker.

I'm mostly just looking for tips or hints at this point.  Maybe someone's already had to do this and come up with ways how?
Title: Re: Calculating, Mostly Addition
Post by: monkey0506 on Thu 08/03/2012 23:15:40
int C;
C++;


Ah, programming jokes. 8)
Title: Re: Calculating, Mostly Addition
Post by: Gilbert on Fri 09/03/2012 02:47:05
Care to elaborate why you need to work with such large numbers in AGS?
Title: Re: Calculating, Mostly Addition
Post by: KamikazeHighland on Fri 09/03/2012 06:06:43
Well, I don't like for math to ever be wrong.  ;)

I've actually finished with all four arithmetic operations for non-negative, non-decimal numbers of any size.

But thank you anyway.
Title: Re: Calculating, Mostly Addition
Post by: Gilbert on Fri 09/03/2012 07:26:05
Care to elaborate why you need to work with such large numbers in AGS?

Is this really needed?
Title: Re: Calculating, Mostly Addition
Post by: KamikazeHighland on Fri 09/03/2012 08:20:43
Necessary?  No.

I don't know c++, and it looks hard to learn.   :-\

I'm finished.
Title: Re: Calculating, Mostly Addition
Post by: Dualnames on Fri 09/03/2012 08:55:01
Again, in case it didn't go through why are you re-inventing the wheel for?
Title: Re: Calculating, Mostly Addition
Post by: Calin Leafshade on Fri 09/03/2012 09:19:37
(32bit) Ints themselves are only accurate to 32-bits but larger numbers can be stored using multiple registers. Theoretically a computer can store a number as large as you like with no errors. Its just far slower because it has to do clever, expensive multiple register arithematic.
Title: Re: Calculating, Mostly Addition
Post by: KamikazeHighland on Fri 09/03/2012 09:40:19
It's not about large numbers, it's about accuracy, and uncertainty.  When you multiply or divide ints or floating point numbers more than once, multiple rounding errors can creep up in no time at all.

Multiple registers, you say?
Title: Re: Calculating, Mostly Addition
Post by: Calin Leafshade on Fri 09/03/2012 11:27:19
dividing ints can introduce rounding errors yes. You would use floats if accuracy was important. The accuracy of a float decreases as the value of the number stored increases but the larger the size of the float the less significance the error will have. Even the vast majority of scientific applications dont require accuracy to more than about 6 significant figures.

Accuracy of integer division can be a problem but if you are concerned about the accuracy of floating point numbers then you really need something far more specialised than AGS.
Title: Re: Calculating, Mostly Addition
Post by: Wyz on Sat 10/03/2012 12:43:12
I'm curious what method you use right now; adding two number of 155 (decimal) digits will boil down to around 30 additions. This is still peanuts for any processor and even for AGS' virtual machine. Adding more digits will cause the processing time to grow linearly. It should by no means be slower than multiplication.