By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,163 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,163 IT Pros & Developers. It's quick & easy.

How do you write an infinite precision number class?

P: n/a
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 #1
Share this Question
Share on Google+
37 Replies


P: n/a
Hi

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


Seriously...
1) you certainly want arbitrary precision, not infinite precision
2) you should definitely do your homework on your own
3) if you get stuck, feel free to ask a more specific question

Good luck
Markus

Sep 11 '05 #2

P: n/a
Protoman wrote:
Hi!!! Protoman here,
Hi, pleased to meet you.

I need to write an infinite precision number class
b/c I want to compute pi.
Non sequitur. If you want to compute pi, all you need is to use an arbitrary
precision library. There are some available, and Google is your friend.

Give me some sample code.
Request denied: be more polite.

Also, when I run my program and it computes pi, will my computer freeze
b/c it's infinite precision?
Yes, an infinite precission calculation will freeze your computer and the
whole universe along with it.

Seriously, if you use a high/arbitrary precision library, it will allow you
to specify the precision you want. Then the program will run and either
print out a highly accurate approximation to pi or it will die on you.
Whether your computer freezes in the process depends on your operating
system and how it handles programs that have an ever growing hunger for
memory.

Thanks for the help!


You are very welcome.
Best

Kai-Uwe Bux
Sep 12 '05 #3

P: n/a
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.

Sep 12 '05 #4

P: n/a

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dg**********@murdoch.acc.Virginia.EDU...
Protoman wrote:
Hi!!! Protoman here,
Hi, pleased to meet you.

I need to write an infinite precision number class
b/c I want to compute pi.


Non sequitur. If you want to compute pi, all you need is to use an
arbitrary
precision library. There are some available, and Google is your friend.

Give me some sample code.


Request denied: be more polite.

Also, when I run my program and it computes pi, will my computer freeze
b/c it's infinite precision?


Yes, an infinite precission calculation will freeze your computer and the
whole universe along with it.


lol
Seriously, if you use a high/arbitrary precision library, it will allow
you
to specify the precision you want. Then the program will run and either
print out a highly accurate approximation to pi or it will die on you.
Whether your computer freezes in the process depends on your operating
system and how it handles programs that have an ever growing hunger for
memory.

Thanks for the help!


You are very welcome.
Best

Kai-Uwe Bux

Sep 12 '05 #5

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.


OMG, you writing code for Nasa?
Sep 12 '05 #6

P: n/a
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.

Sep 12 '05 #7

P: n/a
Protoman wrote:
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].
Use a free library. There are those as well. At least you can use them to do
a quick and dirty implementation of the class to be replaced later. That
way, your colleagues can move on with the rest of the program.

We're writing a program for a NASA sys. that requires us to calc pi to
1,000,000,000+ digits.
a ) Why would NASA need that? The known universe has a diameter on the order
of 10^28m. Thus, an error in an angle in the 50s digit after the decimal
point will project to a distance way less than the diameter of an electron
at the other end of the universe. No real world application has a need for
more than 50 digits of pi.

b) If NASA does not like open source, have them pay for the library.

Don't worry about size; it'll be running on a Cray.
No need for that. There is a digit extraction algorithm for pi. You can
produce 1,000,000,000 digits and more on any modern day PC.

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.


Well, if you really want to code that beast on your own, the best way to
start is probably still to read the relevant chapters in

D.E. Knuth: The art of computer programming Vol 2
(seminumerical algorithms)

Then, start with coding an arbitrary size unsigned int class. A cheap way to
go would be to do something like:

class Cardinal {
std::vector< unsigned long > digit;
public:
...
}; // class Cardinal

later you can use that and build a float class like so:

template < unsigned long Digits = 100 > {
class Real {

bool positive;
Cardinal mantissa;
signed long exponent;
public:
...
}; // Real<>

It does not make sense to discuss the "..." parts unless you settled on
certain algorithms.
Best

Kai-Uwe Bux
Sep 12 '05 #8

P: n/a
OK, but how do I actually *code* it? The precision alogorithm, I mean.

Sep 12 '05 #9

P: n/a
"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
OK, but how do I actually *code* it? The precision alogorithm, I mean.


Please do me a favor. Make sure this rocket that uses this ion drive is not
launched from California. Or let us know when it's launched so I can find
some underground bunker somewhere.
Sep 12 '05 #10

P: n/a
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.

Sep 12 '05 #11

P: n/a
Protoman wrote:
[top-posting, lack of quoting]
We're writing a program for a NASA sys.


What is it about NASA people, and trolling on comp.lang.c++ ?

Sep 12 '05 #12

P: n/a
Hey, it happens :-) It's one of our top resources. Anyway, back to the
*original* topic. Please.

Sep 12 '05 #13

P: n/a
Protoman wrote:
OK, but how do I actually *code* it? The precision alogorithm, I mean.


Before you do the algorithms, settle on a representation. I suggested to use
a std::vector< unsigned long >. For the sake of this discussion, let us go
with that for a while. The idea is that the vector represents a large
number in base 2^32 (I assume for the sake of this discussion that unsigned
long has 32 bits, which may or may not be true for your platform).

Now, you need to implement basic arithmetic for that:

addition,
subtraction,
multiplication,
division
Addition is kind of easy, just start with the least significant digits and
add them. The result is the least significant digit of the sum. If this is
smaller than one of the input digits, a carry occurred. Now move one to the
next digit. Iterate.

Subtraction is essentially the same.

For multiplication, there are choices. You can probably do a straight
forward implementation of the multiplication algorithm that you learned in
elementary school. But that will be slow. Faster methods involve smart
tricks and serious mathematics (including modular arithmetic and discrete
Fourier transform).

Division can be reduced to multiplication. This is tricky but explained in
TAOCP.
Something not to be forgotten: radix conversion in case you want to output
your results. Here is a piece of code that tries to minimize the use of
divisions. It performs reasonably well on something like 1000 digits, but
is very slow compared to libraries available:

namespace DO_NOT_USE {

template < typename T, typename S >
void radix ( T const & num,
std::vector< S > & stack,
std::vector< T > const & power,
typename std::vector< T >::size_type const & power_index,
bool do_fill )
{
if ( power_index == 0 ) {
// one digit only:
stack.push_back( S( num ) );
} else {
typename std::vector< S >::size_type start = stack.size();
typename std::vector< T >::size_type index = power_index - 1;
T q = num / power[ index ];
T r = num - ( q * power[ index ] );
if ( q == T(0) ) {
radix( r, stack, power, index, false );
} else {
radix( r, stack, power, index, true );
radix( q, stack, power, index, false );
}
if ( do_fill ) {
while ( static_cast<typename std::vector< S >::size_type>
( stack.size() - start )
<
static_cast<typename std::vector< S >::size_type>
( 1 << power_index ) ) {
stack.push_back( S(0) );
}
}
}
}

} // namespace DO_NOT_USE

template < typename T, typename S >
std::vector< S > radix ( T const & num, S const & base ) {
std::vector< T > power;
std::vector< S > result;

T current_power = T(base);
T q = num / current_power;
power.push_back( current_power );
while ( current_power < q ) {
q /= current_power;
current_power *= current_power;
power.push_back( current_power );
}
T r = num - ( q*current_power );
if ( q == T(0) ) {
DO_NOT_USE::radix( r, result, power, power.size()-1, false );
} else {
DO_NOT_USE::radix( r, result, power, power.size()-1, true );
DO_NOT_USE::radix( q, result, power, power.size()-1, false );
}
return( result );
}

Here T is supposed to be a high-precision cardinal type and S is some other
arithmetic type (like unsigned short). We assume that T can be constructed
from S and converted to S for values in the range of S.
Finally, floating point arithmetic can be reduced to arithmetic of
cardinals.
Also, let me re-iterate that you should use a library. This kind of code is
very time consuming to write, hard to get right, difficult to test, and it
is close to impossible to outperform state of the art implementations.
Best

Kai-Uwe Bux
Sep 12 '05 #14

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.


Don't take this the wrong way, but why would Nasa invest so much money in
creating this thing is the people who code it cannot "think" for themselfs?
You don't sound as if you know alot about math(or programming) because if
you did you would know that pi is a irrational number and irrational numbers
have no finite representation(by definition)... hence you cannot represent
pi exactly in a computer... you would also know from a engineering point of
view that rarely is pi needed to more than 15 decimal places... and I'm sure
that 1M places is just a tad overkill.

It just seems that your programming experience is a little low for Nasa and
I'd definately not want you working on anything that is going to cost the
american people 100's of millions of dollars. You just don't sound like you
have what it takes to be "part" of Nasa... and if thats the kinda people(No
offense though) that nasa is highering then no wonder it's having so many
problems.

Anyways, there are plenty of archives with pi calculated to more than enough
decimal places then you or nasa will ever need.

But just incase you need 1.24 trillion decimal places of pi I'm sure you can
use your NASA creditials to get a copy of them with no problem.

I guess you are going to try to prove that pi is finite or something by
using some algorithm to find the last digit?
BTW, I will mention that there is an algorithm to determine the nth digit of
pi without finding the n-1 digits... if you do a little homework on google
you can easily find it.

Jon
Sep 12 '05 #15

P: n/a
Jon Slaughter wrote:

"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.

Don't take this the wrong way, but why would Nasa invest so much money in
creating this thing is the people who code it cannot "think" for
themselfs? You don't sound as if you know alot about math(or programming)
because if you did you would know that pi is a irrational number and
irrational numbers have no finite representation(by definition)...


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

1) recursive numbers support arithmetic operations.
2) pi is a recursive number.
BTW, I will mention that there is an algorithm to determine the nth digit
of pi without finding the n-1 digits... if you do a little homework on
google you can easily find it.


This actually is one way to prove 2).

Best

Kai-Uwe Bux
Sep 12 '05 #16

P: n/a
I'm using the pi thing because NASA does want an arbitrary precision
class, besides, thats what were using it for right now. And I just
write the first draft.

Sep 12 '05 #17

P: n/a
Protoman wrote:
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.


Use a vector for the bits of your number.

By using a vector you don't have to worry about destructors and copy
constructors because the compiler generated versions will work fine.

john
Sep 12 '05 #18

P: n/a

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dg**********@murdoch.acc.Virginia.EDU...
Jon Slaughter wrote:

"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.

Don't take this the wrong way, but why would Nasa invest so much money in
creating this thing is the people who code it cannot "think" for
themselfs? You don't sound as if you know alot about math(or programming)
because if you did you would know that pi is a irrational number and
irrational numbers have no finite representation(by definition)...


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


Not really. In any base that is rational you can't write an irrational as a
finite decimal representation.

This is actually the definition of an irrational.

The irrationals are the completion of the rational field and rational
numbers are defined, atleast one way, as terminating decimals or repeating
decimals.
1) recursive numbers support arithmetic operations.
2) pi is a recursive number.
BTW, I will mention that there is an algorithm to determine the nth digit
of pi without finding the n-1 digits... if you do a little homework on
google you can easily find it.
This actually is one way to prove 2).


Heh, well I don't know what a recursive number is though ;) Maybe one who's
digits can be recursively generated?
Best

Kai-Uwe Bux


Jon
Sep 12 '05 #19

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I'm using the pi thing because NASA does want an arbitrary precision
class, besides, thats what were using it for right now. And I just
write the first draft.


So you are kinda like an intern? If I write the class for you and you get
hired by them will you give me your bonus?

Jon
Sep 12 '05 #20

P: n/a

"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:dg**********@murdoch.acc.Virginia.EDU...
Jon Slaughter wrote:

"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
It's not a missile; it's a space probe. And how do I code the
prescision algorithm and conversion functs; I can figure out how to do
just about everything else but constructor, destructor, and copy
constructor. Thanks.

Don't take this the wrong way, but why would Nasa invest so much money in
creating this thing is the people who code it cannot "think" for
themselfs? You don't sound as if you know alot about math(or programming)
because if you did you would know that pi is a irrational number and
irrational numbers have no finite representation(by definition)...


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
Sep 12 '05 #21

P: n/a
Hi there

Protoman wrote:
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

Sep 12 '05 #22

P: n/a
Protoman wrote:
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?

Sep 12 '05 #23

P: n/a
Protoman wrote:

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?

--
Karl Heinz Buchegger
kb******@gascad.at
Sep 12 '05 #24

P: n/a
"hacker++" <at*************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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.


Were you involved with the Columbia shuttle too?


Be fair, that was a hardware problem, not a software one.
Sep 12 '05 #25

P: n/a
"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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.
"Protoman" <Pr**********@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com... 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.
Sep 12 '05 #26

P: n/a
Protoman wrote:
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!
Sep 12 '05 #27

P: n/a
No, they fired the guy responsible for *that* one; I'm his replacement.

Sep 12 '05 #28

P: n/a
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.

Sep 12 '05 #29

P: n/a
Kev
"Protoman" <Pr**********@gmail.com> wrote in news:1126555141.690533.238160
@g44g2000cwa.googlegroups.com:
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.
Sep 13 '05 #30

P: n/a
On 12 Sep 2005 12:59:01 -0700, "Protoman" <Pr**********@gmail.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

P: n/a
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

P: n/a
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

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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 = "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



Sep 13 '05 #34

P: n/a
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;

}
Sep 13 '05 #35

P: n/a

"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

P: n/a

"Protoman" <Pr**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.