473,804 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you write an infinite precision number class?

Hi!!! Protoman here, I need to write an infinite precision number class
b/c I want to compute pi. Give me some sample code. Also, when I run my
program and it computes pi, will my computer freeze b/c it's infinite
precision? Thanks for the help!

Sep 11 '05
37 6377
On 12 Sep 2005 12:59:01 -0700, "Protoman" <Pr**********@g mail.com> wrote:
No, they fired the guy responsible for *that* one; I'm his replacement.


Please, folks, do not feed the troll.

Protoman: You will get better answers in this newsgroup (and actually _learn_
something) if you get serious about doing your _own_ homework, instead of
slinging about your real (chuckle) or imaginary credentials. Who you work for
or what your position happens to be is of little consequence to us.

-dr
Sep 13 '05 #31
OK then... Please help me with my problem!!!! Sorry about being so
rude, but I'm really frustrated right now. Thanks for the future
help!!! And I am not a troll!!!!

Sep 13 '05 #32
Protoman wrote:
OK then... Please help me with my problem!!!!
This thread has already posts where people pointed you in the right
direction as to how you would want to go about coding a high/arbitrary
precission arithmetic class. I suggest you read those posts carefully, and
when you have *more specific* questions, you post those.

Sorry about being so rude, but I'm really frustrated right now.
Yeah, stuff happens.

Thanks for the future help!!!
Experience shows that one can get a lot of help from this news group.
However, you need to follow the rules to actually have a good time in this
part of usenet. In particular, the more specific your problem is, the more
detailed will be the help.

As for the arithmetic class, there is already some advice in this thread
concerning the overall design. I suggest, you put that into some code and
post it for criticism.

And I am not a troll!!!!


Don't walk like a duck, don't talk like a duck; and soon nobody will think
that you are a duck.
Best

Kai-Uwe Bux
Sep 13 '05 #33

"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
OK then... Please help me with my problem!!!! Sorry about being so
rude, but I'm really frustrated right now. Thanks for the future
help!!! And I am not a troll!!!!


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 = "3748392837 4";
string num2 = "2933747282 2";

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



Sep 13 '05 #34
I went ahead and did an example. It's not perfect as you would need to add
some code to error check and to handle strings of different lengths and
such. But it adds to positive integers of arbitrary size rerpesented in the
string. Wouldn't be to hard to modify to actually make it useful... the
other operations are very similar but I think you need to do them.

Jon

#include <stdio.h>

#include <stdlib.h>

#include <string>

#include <iostream>

using namespace std;

char itoc(int n) { return n + 48; }

int ctoi(char c) { return c - 48; }

int main(int argc, char* argv[])

{

string num1 = "293849832" ;

string num2 = "448397571" ;

string result = num2;

// = 742247403

int carry = 0;

for(int index = 0; index < (int)num1.lengt h(); index++)

{

int r = ctoi(num1[num1.length() - index - 1]) + ctoi(num2[num2.length() -
index - 1]);

r += carry;

if (r > 9)

{

r = r % 10;

carry = 1;

}
result[result.length() - index - 1] = itoc(r);

};

cout << result << endl;

return 0;

}
Sep 13 '05 #35

"Jon Slaughter" <Jo***********@ Hotmail.com> wrote in message
I went ahead and did an example. It's not perfect as you would need to add
some code to error check and to handle strings of different lengths and
such. But it adds to positive integers of arbitrary size rerpesented in
the string. Wouldn't be to hard to modify to actually make it useful...
the other operations are very similar but I think you need to do them.

Jon
<snip>
int r = ctoi(num1[num1.length() - index - 1]) + ctoi(num2[num2.length() -
index - 1]);

r += carry;
need to add:

carry = 0; after this line... for got to reset the carry flag ;/

if (r > 9)


(also if there is a carry on the last digit then it won't be added... This
wasn't ment to be a perfect example... just a hopefully working one.)

Sep 13 '05 #36

"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
Yes really; I'm the asst. head of systems programming in the R&D Dept.
We're calculating thruster angles for a prototype ion drive engine.

What complete and utter BS.

Anyone knowledgeable enough to do such calculations would know that you only
need (and want) to use a value for pi which meets the requirements of the
rest of your calculations, as far as precision is concerned. Thruster
angles need to be calculated repeatedly, in extremely short periods of time,
and from what I know, are computed using difference formulas over very short
periods of time, with correctional computations made at longer intervals, to
keep the error values with a specific range. The value for pi is knows to
significant digits _far_ beyond the needs of any calculation that would ever
be made (in _any_ time interval, let alone such short ones). Computing pi
on the fly is a complete waste of time (lterally).

But of course, if you _were_ who you're _claiming_ to be, you'd already know
that.
-Howard
Sep 13 '05 #37
You're a temp working as asst. head of R&D? LOL!!! Seriously, what
company in their right mind would hire a *temp* to be assistand head of
R&D?

Man, this thread has given me the most laughs I've ever had in
comp.lang!

Sep 14 '05 #38

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2405
by: Bruce | last post by:
I have a nice public domain large number class that uses STL vectors and seems to be relatively bug free but is not the fastest thing on the planet and I have a need for speed. Anyone know of any available that are really optimized for speed. All I need for functionality is multiply, divide and modulous operations.
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.