是否有一个类来表示浮点数? [英] Is there a library class to represent floating point numbers?

查看:163
本文介绍了是否有一个类来表示浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,它使用十进制数字(例如57.65)进行大量操作。因为乘法和除法会迅速削弱它们的准确性,所以我想把数字存储在一个类中,在操作之后保存它们的准确性,而不是依赖float和double。

I am writing an application which does a lot of manipulation with decimal numbers (e.g. 57.65). As multiplications and divisions quickly erode their accuracy, I would like to store the numbers in a class which preserves their accuracy after manipulation, rather than rely on float and double.

I我在说这样的事情:

I am talking about something like this:

class FloatingPointNumber {
private:
    long m_mantissa;
    int m_dps; // decimal points
    // so for example 57.65 would be represented as m_mantissa=5765, m_dps=2
public:
    // Overloaded function for addition
    FloatingPointNumber operator+(FloatingPointNumber n);
    // Other operator overloads follow
}

写这样的类,感觉有点像重新发明的轮子,我相信必须有一些库类在这里做的(虽然这似乎不存在于STL)。

While it is possible for me to write such a class, it feels a bit like reinventing the wheel and I am sure that there must be some library class somewhere which does this (although this does not seem to exist in STL).

有没有人知道这样的图书馆?非常感谢。

Does anybody know of such a library? Many thanks.

推荐答案

这是什么意思?

#include "ttmath/ttmath.h"
#include <iostream>

int main()
{
   // bigdouble consists of 1024*4 bytes for the mantissa
   // and 256*4 bytes for the exponent.
   // This is because my machine is 32-bit!
    typedef ttmath::Big<1024, 256> bigdouble; // <Mantissa, Exponent>

    bigdouble x = 5.05544;
    bigdouble y = "54145454.15484854120248541841854158417";
    bigdouble z = x * y * 0.01;

    std::cout << z;

    return 0;
}

您可以指定机器字 尾数指数
我使用 TTMath 来解决Project Euler拼图,我真的很高兴。

You can specify the number of machine words in the mantissa and the exponent as you like. I have used TTMath to solve Project Euler puzzles, and I am really pleased with it. I think it is relatively stable and the author is very kind if you have questions.


EDIT :: 我还使用了 MAPM a>过去。它代表基数100 中的大浮点数,因此将十进制数转换为基数100是没有问题的,与基数2不同。TTMAT使用基数2来表示大浮点数。它是稳定的,因为2000年作为图书馆页面声称。它已经在许多应用程序中使用,您可以在库页面中看到。它是一个C库与一个不错的C ++包装。

: I have also used MAPM in the past. It represents big floats in base 100, so there would be no problem converting decimal numbers to base 100, unlike base 2. TTMAT uses base 2 to represents big floats. It is stable since 2000 as the library page claims. It has been used in many applications as you can see in the library page. It is a C library with a nice C++ wrapper.

MAPM nextPrime(){

    static MAPM prime = 3;
    MAPM retPrime = prime;

    prime += 2;
    while( isPrime( prime ) == false )
    	prime += 2;

    return retPrime;
}


BTW,如果您对GMP感兴趣,而且您使用的是VS,则可以查看 MPIR 是Windows的GMP端口;)对我来说,我发现TTMath更令人愉快,更容易/更快比所有我尝试,因为库做堆栈分配,而不触及堆反正。基本上它不是一个任意的精度库,你指定编译时的精度如上所示。

BTW, If you are interested in GMP and you are using VS, then you can check the MPIR which is GMP port for Windows ;) for me I find TTMath more than pleasing and easier/faster than all of what I tried because the library does stack allocations without touching the heap in anyway. Basically it is not an arbitrary precision library, you specify the precision at compile-time as shown above.

这篇关于是否有一个类来表示浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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