Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
How do you write an infinite precision number class?
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Jon Slaughter, post: 2521630"] Well, have you tried to find some free source code online? There might even be some tutorials out there. It seems more as if you want us to do it than you want to do it yourself. I'm sure if you spend an hour or two searching google you'll probably find exactly what you need. If you just want an inefficient way to do arithmetic on very large numbers(not irrational ones either) then you might look up BCD arithmetic. As far as I can remember, and I might be wrong, that the 8086+ instruction set has instructions to work with BCD numbers directly. The basic idea is to simply get a method to add and subtract very large numbers... usually the multiplication and division will follow from those algorithms. Basicaly the method would consist of storing the numbers a strings with each digit representing a character... '123834739' + '893483729' ----------------- you have those two numbers as strings and to add them you start at the first digit's and add them lets say you have string num1 = "37483928374"; string num2 = "29337472822"; to add them we will do int x = ascii2int(num1[0]) + ascii2int(num2[0]) if (carry = true) x++; so we have 0 <= x <= 19 obviously if x < 10 then we have no problem to add them and the result is stored as the first digit in our result: The problem comes from when we have to carry we have to "propagate" the carry bit: so if (0<= x < 10) { result[0] = int2ascii(x); } else { carry = true; result[0] = int2ascii(x % 10); } and when we work with the second digit we have to add that carry in. the propagation comes if we have something like 99999999 + 9999999999. we get 18 for the first digit and so we have to add the carry in on the next.. which is 19 total so we have to carry again and again and again until we get to the last digit. so we get something like 19999999998(or whatever). (and that leading 1 is actualy the final carry). Anyways, that should get you started with some code. To make your "infinite precision library" I'm assuming you just mean you want to be able to have a field of arbitrary "real" numbers represented on the computer... that means you need to be able to add, subtract, etc... I've given you an outline of C++ psuedo for adding... I think you can handle the rest(and if your not sure about multiplication you should just try do multiply two numbers by hand and see how you do it and you will see that it is similar to adding(but more complex ofcourse)). Jon [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
How do you write an infinite precision number class?
Top