定义一个类,以任意精度实现算术 [英] Define a class, which implements arithmetic with arbitrary precision

查看:79
本文介绍了定义一个类,以任意精度实现算术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我正在读一本书,并在这本书中做一些练习.其中之一是:定义一个类,该类以任意精度实现算术(+,-,/,*).这是一种仅带有一些算术运算的新类型.使用double类型,我们还可以执行一些算术运算,但是精度很高.
好吧,要在C ++中执行任意精度算术,我认为我们需要开发一个诸如"Big Float"之类的类.我发现很难相信他们会希望像我这样的初学者能够做到这一点,因为对于数学专家而言,这确实是一项工作.
我建议这样的变体:

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:

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:

string Number::_add(std::string rhsNumber)const{/*add logic here */}

?

请有人帮我开始吧!

?

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

推荐答案

我认为逻辑可能类似于可以在其他语言中找到的BigInt类型. 此处 [ ^ ]可以帮助您的一些示例.我还假定这本书包含解决方案,所以您可能需要阅读该书,才能准确地看到作者试图演示的内容.
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.


我按照您所说的那样编写程序:
//Task6.cpp:定义控制台应用程序的入口点.
//
I coded my program like this as you said:
// Task6.cpp : Defines the entry point for the console application.
//
#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);
};


,这是以下代码:(我不知道为什么显示我的帖子的窗口不能覆盖我的所有帖子)
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;
}



问题是:假设我是用户.因此,我可以使用具有任意长度的Number对象,在计算结果之后,应以我赋予Number对象的精度保存结果(在这种情况下,应保存结果的长度).
但是在我的程序中,它取决于setprecision(int n).用户别无选择.这毫无意义.



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.


这篇关于定义一个类,以任意精度实现算术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆