Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
I suggest a variant like this:

C#
class Number{
private:
 std::string _numbers;
public:
 Number(){}
 Number(const std::string& num) : _numbers(num){}
 //add operator
 std::string operator+(const std::string& rhsNumber)const{ return _add(rhsNumber);}
 //subtract operator
 std::string operator-(const std::string& rhsNumber)const{return _add(-rhsNumber);}
 //divide operator
 std::string operator/(const std::string& rhsNumber)const{return _divide(rhsNumber);}
 //multiply operator
 std::string operator*(const std::string& rhsNumber)const{return _multiply(rhsNumber);}
 //negation operator
 std::string operator-()const;
private:
 //helper function
 string _add(std::string rhsNumber)const{/*add logic here */}
 string _multiply(std::string rhsNumber)const{/*add logic here */}
 string _divide(std::string rhsNumber)const{/*add logic here */}
};

But what should be inside:
C#
string Number::_add(std::string rhsNumber)const{/*add logic here */}
?

May someone get me started, please!
Thanks in advanced!
Posted

I think the logic may be something like the BigInt types that can be found in other languages. Here[^] are some samples that may help you. I would also assume that the book contains the solution so you may want to read that to see exactly what the author is trying to demonstrate.
 
Share this answer
 
I coded my program like this as you said:
// Task6.cpp : Defines the entry point for the console application.
//
C++
#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);
};
 
Share this answer
 
v4
Comments
Richard MacCutchan 28-Dec-10 5:20am    
Minor edit.
and here is the following code:(I don't know why the window for showing my post doesn't cover all my post)
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.
 
Share this answer
 
v3
Comments
Richard MacCutchan 28-Dec-10 5:26am    
This will never work, since you are converting to float/double which both have fixed precision. Take a look at some of the links to BigInt types that I posted earlier, or this one : http://support.microsoft.com/kb/145889
Do the following in string _add(std::string rhsNumber)

1.use atof/atoi functions to convert rhsNumber,_numbers to float or double/int type.
2. then just add it
3. convert to strnig again (itoa)
4.return it

thats it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900