Jump to content

Define a class, which implements arithmetic with arbitrary precision


phabion

Recommended Posts

Hi everyone!

I'm reading a book and doing some exercises in this book. One of them is: define a class, which implements arithmetic (+, -, /, *) with arbitrary precision. This is a new type with just some arithmetic operations. With type double we also can do some arithmetic but with a definite precision.

Well, to do arbitrary precision arithmetic in C++, I think we need to develop a class such as 'Big Float'. I find it hard to believe they'd expect a beginner like me to do this as it's really a job for a mathematical specialist.

    #include "stdafx.h"
   #include <iostream>
   #include <string>
   #include <cstdio>
   #include <sstream>
   #include <iomanip>
   using namespace std;

   class Number{
   private:
   string _numbers;
   public:
   Number(){}
   Number(const string& num) : _numbers(num){}
   friend string ConvertToString(double value);
   void print();
   //add operator
   Number operator+(Number rhsNumber);

   };
   Number Number::operator+( Number rhsNumber)
   {
       Number temp;
       temp._numbers =ConvertToString(atof(this->_numbers.c_str())+atof(rhsNumber._numbers.c_str()));
       return temp;
   }
   void Number::print()
   {
       cout<<_numbers<<endl;
   }
   string ConvertToString(double value)
   {
       std::stringstream ss;
       ss << setprecision(15)<<value;
       return ss.str();
   }
   int _tmain(int argc, _TCHAR* argv[])
   {
       Number a="1.21111111111111111111111111111111111112222";
       Number b="2.1111111";
       Number c=a+b;
       c.print();
       system("PAUSE");
       return 0;
   }

The thing is: suppose I'm an user. So I can use a Number object with arbitrary length, after computing the result should be saved with the precision that I've gave the Number object (in this situation the length of result should be saved).

But in my program it depends on setprecision(int n). The user has no choice. and it's a pointless.

Plz help me solve this problem! thanks in advanced!

Share this post


Link to post
Share on other sites

If I understand correctly, the program needs to take numbers with arbitrary precision and output with the same precision?

If that is so, you are on the right track, parsing the number as a string into [val].[frak] (i.e. 2.11 val=2, frak=11) and your precision is setPrecision (frak.Length)

 

Does that help or did I misunderstand the assignment?

Share this post


Link to post
Share on other sites

If I understand correctly, the program needs to take numbers with arbitrary precision and output with the same precision?

If that is so, you are on the right track, parsing the number as a string into [val].[frak] (i.e. 2.11 val=2, frak=11) and your precision is setPrecision (frak.Length)

 

Does that help or did I misunderstand the assignment?

That seems like it would work, since it would set the precision to the length of frak. That said, It's 4:30 in the morning, so I may not be any better off at saying it's correct as you are.

Share this post


Link to post
Share on other sites

If I understand correctly, the program needs to take numbers with arbitrary precision and output with the same precision?

If that is so, you are on the right track, parsing the number as a string into [val].[frak] (i.e. 2.11 val=2, frak=11) and your precision is setPrecision (frak.Length)

 

Does that help or did I misunderstand the assignment?

 

Also, if you store "frak.length"s for all input variables into an array, you can easily figure out the longest one and set that as the precision for the whole system, so you can take functions such as "1.231 + 21.55123" and still come up with the correct precision.

 

Another possibility would be to add both numbers into a double, convert the double to a string have a function to read the string from back to front and cut off every zero until the first non-zero character is reached, and output that.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...