473,804 Members | 3,250 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 6378
"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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 #11
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
Hey, it happens :-) It's one of our top resources. Anyway, back to the
*original* topic. Please.

Sep 12 '05 #13
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<typ ename std::vector< S >::size_type>
( stack.size() - start )
<
static_cast<typ ename 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::rad ix( r, result, power, power.size()-1, false );
} else {
DO_NOT_USE::rad ix( r, result, power, power.size()-1, true );
DO_NOT_USE::rad ix( 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

"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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
Jon Slaughter wrote:

"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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
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
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

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

"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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

"Protoman" <Pr**********@g mail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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

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
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10327
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...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.