How do you write an infinite precision number class?

J

Jon Slaughter

Kai-Uwe Bux said:
Well, the OP was aware of that problem. Recall that he specifically asked
for an *infinite precision* class.

Seriously though, there are some irrational numbers that do have finite
representations. E.g., I think algebraic numbers can be manipulated
exactly
on a computer.

Oh, yeah. But I think the methods are indirect in the sense that they are
not working with the decimal representation of the number but group
theoretic properties of algebraic numbers. The fact is that one cannot work
with infinite decimal represented numbers because its impossible. This
doesn't mean there are not symbolic ways to deal with them though.. or even
other means.


Also, there is the notion of a recursive real number
(essentially that is a number whose decimal expansion can be computed by a
recursive function). It turns out that

duh.. sorry. seems I didn't read your entire post ;/ heh.

Jon
 
M

Markus Moll

Hi there
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.

LOL... thanks, you made my day, Mr. Protoman :-D

cheers
Markus
 
H

hacker++

Protoman said:
OK, let me rephrase this:
I'd like to write an arbitary precision number class[I'd like to be
able to specify the precision in digits or bits] because my co. won't
buy any commercial classes[the procurement dept. are cheapskates].
We're writing a program for a NASA sys. that requires us to calc pi to
1,000,000,000+ digits. Don't worry about size; it'll be running on a
Cray. We're also going to use it to replace or supplant the built in
number types. If you can give me some sample code, that'd be great.
Thanks for the help.
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.

Were you involved with the Columbia shuttle too?
 
K

Karl Heinz Buchegger

Protoman said:
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 happened to your Vigenere cipher?
Already finished?
 
J

Jim Langston

Protoman said:
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.

Protoman said:
Because there are strict rules that prohibit me from using my work
email for personal purposes, so I'm using my personal account for this;
it's a problem I'm doing for fun. I'm Dr. Robert Wilson, at Cal State
Long Beach.

Enough said.
 
R

red floyd

Protoman said:
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.


Really? I thought you were a CSU professor!
 
P

Protoman

I'm a temp; I'll be returning to my CSU job as soon as they find a full
time asst. head of R&D; I'm really just a "facillitator" over at NASA;
and can we get back to the original topic. Please.
 
K

Kev

No, they fired the guy responsible for *that* one; I'm his replacement.

The answer to your question is: "an infinite precision number class"

At least, thats how I write it.
 
D

Dave Rahardja

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
 
P

Protoman

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!!!!
 
K

Kai-Uwe Bux

Protoman said:
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
 
J

Jon Slaughter

Protoman said:
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 = "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
 
J

Jon Slaughter

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.length(); 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;

}
 
J

Jon Slaughter

Jon Slaughter said:
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
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.)
 
H

Howard

Protoman said:
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
 
K

kevin.hall

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top