C++十进制数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14096026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:57:58  来源:igfitidea点击:

C++ decimal data types

c++typesfloating-pointdoubledecimal

提问by fpiro07

Is there a way to use decimal data types such as decimal32, decimal64or decimal128in my C++ programs?

有没有一种方法来使用十进制数据类型,例如decimal32decimal64decimal128在我的C ++程序?

采纳答案by Dietmar Kühl

The classes from the Decimal TRare not implemented for all compilers. Some compilers, e.g., gcc, implement the C Decimal TRand provide the corresponding extensions in C++, too. In the past there was an open source implementation for the C++ Decimal TR available but I failed to locate it. If your compiler doesn't support the decimal types, your best option is probably to create a wrapper for IBM's decNumber library.

Decimal TR中的类并未为所有编译器实现。一些编译器,例如gcc,实现了C Decimal TR并在 C++ 中提供了相应的扩展。过去有一个 C++ Decimal TR 的开源实现可用,但我没有找到它。如果您的编译器不支持十进制类型,那么您最好的选择可能是为 IBM 的decNumber 库创建一个包装器。

To improve the situation in the future of C++, I have created a plan to update the TRand I'm going to turn the current TR into a complete proposal ready for the next C++ committee meeting (in April in Bristol), trying to get it adopted into the C++ standard, possibly into the revision planned for 2014. The implementation I have is part of my regular work and it isn't up to me to decide whether it is can be made available publically although there is some hope that it can be open sourced at some point.

为了改善 C++ 未来的情况,我制定了更新 TR计划,我将把当前的 TR 变成一个完整的提案,为下一次 C++ 委员会会议(4 月在布里斯托尔举行)做好准备,试图让它被纳入 C++ 标准,可能纳入 2014 年计划的修订版。我的实现是我日常工作的一部分,我不能决定它是否可以公开提供,尽管有一些希望可以在某个时候开源。

回答by Piotr

You can use easy to use header-only solution for C++ with templates: https://github.com/vpiotr/decimal_for_cpp

您可以使用易于使用的 C++ 模板解决方案:https: //github.com/vpiotr/decimal_for_cpp

Notice that this is not a *Big*Decimal class; it is limited to 64 bits' worth of "mantissa" digits.

请注意,这不是*Big*Decimal 类;它仅限于 64 位的“尾数”数字。

[taken from link]

[取自链接]

  #include "decimal.h"

  using namespace dec;

  // the following declares currency variable with 2 decimal points
  // initialized with integer value (can be also floating-point)
  decimal<2> value(143125);

  // to use non-decimal constants you need to convert them to decimal
  value = value / decimal_cast<2>(333.0);

  // output values
  cout << "Result is: " << value << endl;
  // this should display something like "429.80"

  // to mix decimals with different precision use decimal_cast
  decimal<6> exchangeRate(12.1234);
  value = decimal_cast<2>(decimal_cast<6>(value) * exchangeRate);

  cout << "Result 2 is: " << value << endl;
  // this should display something like "5210.64"

  cout << "Result 2<6> is: " << decimal_cast<6>(value) << endl;
  // this should display something like "5210.640000"

回答by gbjbaanb

use an int32 or int64, and (manually) shift the decimal point to where you want it to be. If you're measuring dollars, for example, just measure cents instead and display the value differently. simple!

使用 int32 或 int64,并(手动)将小数点移动到您想要的位置。例如,如果您正在测量美元,只需测量美分并以不同方式显示值。简单的!

回答by David Bradley

Boost has cpp_dec_float as well. That's probably the best solution until it's adopted into the standard.

Boost 也有 cpp_dec_float。在它被纳入标准之前,这可能是最好的解决方案。

https://www.boost.org/doc/libs/1_68_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/cpp_dec_float.html

https://www.boost.org/doc/libs/1_68_0/libs/multiprecision/doc/html/boost_multiprecision/tut/floats/cpp_dec_float.html

回答by user1095108

gcc/clang (usually) come with their own floating point decimal implementations, if your distro decides to compile them into whatever gcc/clang version they offer (not the case for some arm distros I tried out). This is why you sometimes need a custom decimal type implementation. Try minefor ideas (tested on i586 all the way to aarch64).

gcc/clang(通常)带有他们自己的浮点十进制实现,如果你的发行版决定将它们编译成他们提供的任何 gcc/clang 版本(不是我尝试过的一些 arm 发行版的情况)。这就是为什么您有时需要自定义十进制类型实现的原因。尝试的想法(在 i586 上一直测试到 aarch64)。