Calculating, Mostly Addition

Started by KamikazeHighland, Sun 04/03/2012 23:02:48

Previous topic - Next topic

KamikazeHighland

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?

monkey0506

Code: ags
int C;
C++;


Ah, programming jokes. 8)

Gilbert

Care to elaborate why you need to work with such large numbers in AGS?

KamikazeHighland

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.

Gilbert

Care to elaborate why you need to work with such large numbers in AGS?

Is this really needed?

KamikazeHighland

Necessary?  No.

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

I'm finished.

Dualnames

Again, in case it didn't go through why are you re-inventing the wheel for?
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Calin Leafshade

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

KamikazeHighland

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?

Calin Leafshade

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.

Wyz

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.
Life is like an adventure without the pixel hunts.

SMF spam blocked by CleanTalk