Exact decimal datatype for C++?

2020-01-29 06:32发布

PHP has a decimal type, which doesn't have the "inaccuracy" of floats and doubles, so that 2.5 + 2.5 = 5 and not 4.999999999978325 or something like that.

So I wonder if there is such a data type implementation for C or C++?

7条回答
你好瞎i
2楼-- · 2020-01-29 06:38

There will be always some precision. On any computer in any number representation there will be always numbers which can be represented accurately, and other numbers which can't.

  • Computers use a base 2 system. Numbers such as 0.5 (2^-1), 0.125 (2^-3), 0.325 (2^-2 + 2^-3) will be represented accurately (0.1, 0.001, 0.011 for the above cases).

  • In a base 3 system those numbers cannot be represented accurately (half would be 0.111111...), but other numbers can be accurate (e.g. 2/3 would be 0.2)

  • Even in human base 10 system there are numbers which can't be represented accurately, for example 1/3.

  • You can use rational number representation and all the above will be accurate (1/2, 1/3, 3/8 etc.) but there will be always some irrational numbers too. You are also practically limited by the sizes of the integers of this representation.

  • For every non-representable number you can extend the representation to include it explicitly. (e.g. compare rational numbers and a representation a/b + c/d*sqrt(2)), but there will be always more numbers which still cannot be represented accurately. There is a mathematical proof that says so.

So - let me ask you this: what exactly do you need? Maybe precise computation on decimal-based numbers, e.g. in some monetary calculation?

查看更多
Lonely孤独者°
3楼-- · 2020-01-29 06:38

Being a higher level language PHP just cuts off what you call "inaccuracy" but it's certainly there. In C/C++ you can achieve similar effect by casting the result to integer type.

查看更多
Evening l夕情丶
4楼-- · 2020-01-29 06:42

Yes:

There are arbitrary precision libraries for C++.
A good example is The GNU Multiple Precision arithmetic library.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-01-29 06:43

The Boost.Multiprecision library has a decimal based floating point template class called cpp_dec_float, for which you can specify any precision you want.

#include <iostream>
#include <iomanip>
#include <boost/multiprecision/cpp_dec_float.hpp>

int main()
{
    namespace mp = boost::multiprecision;
    // here I'm using a predefined type that stores 100 digits,
    // but you can create custom types very easily with any level
    // of precision you want.
    typedef mp::cpp_dec_float_100 decimal;

    decimal tiny("0.0000000000000000000000000000000000000000000001");
    decimal huge("100000000000000000000000000000000000000000000000");
    decimal a = tiny;         

    while (a != huge)
    {
        std::cout.precision(100);
        std::cout << std::fixed << a << '\n';
        a *= 10;
    }    
}
查看更多
ゆ 、 Hurt°
6楼-- · 2020-01-29 06:49

If you are looking for data type supporting money / currency then try this: https://github.com/vpiotr/decimal_for_cpp

(it's header-only solution)

查看更多
我想做一个坏孩纸
7楼-- · 2020-01-29 06:49

I presume you are talking about the Binary Calculator in PHP. No, there isn't one in the C runtime or STL. But you can write your own if you are so inclined.

Here is a C++ version of BCMath compiled using Facebook's HipHop for PHP: http://fossies.org/dox/facebook-hiphop-php-cf9b612/dir_2abbe3fda61b755422f6c6bae0a5444a.html

查看更多
登录 后发表回答