有没有 C++相当于Java的BigDecimal?
我正在寻找一个可以进行十进制浮点运算的 C++ 类。浏览 http://speleotrove.com/decimal/ 可以找到人们编写的各种类的链接并且没有维护。通过深入研究 decNumber++ 的内容,我发现了一些电子邮件,表明 GCC 最终将支持此功能。 (正式名称为 ISO/IEC TR 24733)
我正在寻找可以用作 float 或 double 的直接替代品,其他人在自己的项目中使用的东西。希望开源。
谢谢!
编辑:我应该指出,我试图用它来代表价格。所以我需要精确的小数,而不是巨大的小数。
I'm looking for a C++ class that can do decimal floating point arithmetic. Looking through http://speleotrove.com/decimal/ there are links to all sorts of classes that people have written and not maintained. Digging through the decNumber++ stuff led me to some emails showing that GCC will eventually support this functionality. (Formally known as ISO/IEC TR 24733)
I'm looking for something I can use as a drop-in replacement for float or double, something that other people are using in their own projects. Hopefully open source.
Thanks!
EDIT: I should point out that I'm trying to use this to represent prices. So I need EXACT decimals, not HUGE decimals.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
存在一个名为 GMP(GNU 多精度库) 的巨大库,它支持此功能,并且还具有 C++ 绑定,不过说实话C++ 接口有点不稳定且过时。
文档中的一个示例,以下创建了一个名为
f
的浮点数,其精度至少为 500 位:There exists a huge library called GMP (GNU multiple precision library) which supports this and also has C++ bindings, though to be honest the C++ interface is a bit wonky and outdated.
An example from the documentation, the following creates a float called
f
with at least 500 bits of precision:随你挑选。那里有很多这样的人。例如,您可以查阅维基百科上的列表。
Take your pick. There are a bunch of them out there. For instance, you can consult the list on Wikipedia.
这个问题有点老了,但对于其他有相同需求的人来说:Boost.multi precision 可能就是您正在寻找的。
http://www .boost.org/doc/libs/1_57_0/libs/multi precision/doc/html/boost_multi precision/tut/floats/cpp_dec_float.html
这是一个任意精度库,可以处理基于 10 的小数。
The question is a bit old, but for other people that have the same need : Boost.multiprecision is probably what you're looking for.
http://www.boost.org/doc/libs/1_57_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/cpp_dec_float.html
It's an arbitrary precision library that can handle 10-based decimals.
如果您需要使用巨大的十进制值进行运算,我建议您使用 http://gmplib.org/ 库。我在 C 和 C++ 中经常使用它。
If you need to do operations with HUGE decimal values I would suggest you to use http://gmplib.org/ library. I've used it a lot with C and C++.
使用 GMP 并将所有内容存储为美分。如果您知道您不会传递 2^32 美分(42949673 万美元),请使用 32 位无符号 int(或使用 64 位无符号 int)并保持简单。
Use GMP and store everything as cents. If you know that you won't pass 2^32 cents (42.949673 million dollars) use a 32 bit unsigned int (or use a 64 bit unsigned int) and keep it simple.
我可能来得太晚了,但是 128 位小数可以吗?
这些已经被 C++ 接受,并且至少从 gcc-4.5 开始就有了它们(我们现在开始 4.9:
注意,decimal32 的大小等于 float,decimal64 的大小等于大多数 double。因此,decimal128 相当大。从 < a href="http://en.wikipedia.org/wiki/Decimal128_floating-point_format" rel="nofollow">Wikipedia:Decimal128 支持 34 位十进制有效位数,指数范围为 −6143 到 +6144,即±0.000000000000000000000000000000000×10−6143到±9.99999999999999999999999999999999×106144。(相当于±00000000000000000000000000 00000000×10−6176 到 ±999999999999999999999999999999999×106111。)
mpfr 库是任意精度二进制浮点 - 不是任意精度十进制。
I may be too late for this but would 128bit decimals work?
These have been accepted into C++ and at least gcc has them since gcc-4.5 (we're starting 4.9 now:
Note there is decimal32 equal in size to float, decimal64 equal in size to most double. So decimal128 is pretty big. From Wikipedia: Decimal128 supports 34 decimal digits of significand and an exponent range of −6143 to +6144, i.e. ±0.000000000000000000000000000000000×10−6143 to ±9.999999999999999999999999999999999×106144. (Equivalently, ±0000000000000000000000000000000000×10−6176 to ±9999999999999999999999999999999999×106111.)
The mpfr library is arbitrary precision binary floating point - not arbitrary precision decimal. There is a difference.
这是 BCMath PHP 到 C++ 的实现。有两种版本,一种用于 Qt,另一种仅使用 STL。
来源:https://github.com/DesarrollosCuado/BCMath-for-Cpp
结果:
Here is an implementation of BCMath PHP to C++. There are two versions, one for Qt and the other for use only STL.
Source: https://github.com/DesarrollosCuado/BCMath-for-Cpp
Result:
您可以使用 BigDecimal 的下一个 cpp 实现:
https://github.com/Sam-bit/ BigDecimal-CPP
谨致问候。
you can use the next cpp implementation of BigDecimal:
https://github.com/Sam-bit/BigDecimal-CPP
Best Regards.
也许“MAPM,C 语言的便携式任意精度数学库”就是您正在寻找的。它还包括 C++ 包装器:
http://www.tc.umn.edu/ ~ringx004/mapm-main.html
Probably "MAPM, A Portable Arbitrary Precision Math Library in C" is what you are looking for. It also includes C++ Wrappers:
http://www.tc.umn.edu/~ringx004/mapm-main.html