C++ 有效数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4607473/
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-28 15:54:46  来源:igfitidea点击:

C++ significant figures

c++mathroundingsignificant-digits

提问by joshim5

How can I do math involving significant figures in C++? I want this to work correct with measured data from chemistry and physics experiments. An example: 65 / 5 = 10. I would need to get rid of unneeded decimal places and replace some digits with 0s.

如何在 C++ 中进行涉及有效数字的数学运算?我希望这与来自化学和物理实验的测量数据正确工作。一个例子:65 / 5 = 10。我需要去掉不需要的小数位并用 0 替换一些数字。

Thanks!

谢谢!

采纳答案by Ryan Christensen

Well there are good math libraries in math.h

那么math.h 中很好的数学库

Also storing your figures in floats, doubles or long doubles will allow for more precise operations.

还将您的数字存储在浮点数、双精度数或长双精度数中将允许更精确的操作。

Floats offer 7 significant digits while doubles offer 16 significant digits.

浮点数提供 7 位有效数字,而双打提供 16 位有效数字。

source

来源

Also when printing out usually people use _snprintf or printf and you can format those doubles, floats to the precision you want like:

此外,在打印时通常人们使用 _snprintf 或 printf ,您可以格式化这些双精度数,浮动到您想要的精度,例如:

Float Precision

printf("Value %8.2f", floatVariable);

This says you require a total field of 8 characters, within the 8 characters the last 2 will hold the decimal part.

_snprintf(buffer, sizeof(buffer), "Value %.2f", floatVariable);

The example above requests the minimum field width and the last two characters are to hold the decimal part.

浮点精度

printf("值 %8.2f", floatVariable);

这表示您需要总共 8 个字符的字段,在 8 个字符中,最后 2 个将保留小数部分。

_snprintf(buffer, sizeof(buffer), "Value %.2f", floatVariable);

上面的例子要求最小字段宽度,最后两个字符是小数部分。

回答by Rion Williams

This should get you what you need:

这应该可以满足您的需求:

std::cout.precision(x); // x would be the number of significant figures to output

回答by user434565

This may not be the most efficient way, but you can create a custom sig fig data type.

这可能不是最有效的方法,但您可以创建自定义 sig fig 数据类型。

class SigFigFloat
{
  SigFigFloat(vector<short> digits, int decimalIndex, bool negative);
  SigFigFloat operator+(const SigFigFloat &value);
  SigFigFloat operator-(const SigFigFloat &value);
  //etc...


}

It can be a lot of work, but if you implement this right, it can be a really flexible way to represent and do calculations with sig figs.

这可能需要做很多工作,但是如果您正确地实现了这一点,它可以是一种非常灵活的方式来表示和使用 sig figs 进行计算。

回答by Zac Howland

That depends on how you are displaying them. If you are using the printf-family, you set the precision (sprintf(buffer, "%.2f", myfloat)). If you are using ostreams, you call the precision function to set the number of decimal places. If you are looking for the more scientific method of sig figs, you'll have to write a custom function that determines the precision based on the current value of the float.

这取决于您如何显示它们。如果您使用的是 printf 系列,则设置精度 ( sprintf(buffer, "%.2f", myfloat))。如果您使用 ostreams,则调用 precision 函数来设置小数位数。如果您正在寻找更科学的 sig figs 方法,则必须编写一个自定义函数,根据浮点数的当前值确定精度。

回答by Alexandre C.

It is hard because significant figures are a decimal concept, and computers speak binary. You can use decimal number classes (I don't know of any), or use boost::interval, which is the closest to what you certainly want to achieve.

这很难,因为有效数字是十进制概念,而计算机使用二进制。您可以使用十进制数字类(我不知道任何类),或使用boost::interval,这是最接近您想要实现的目标。

回答by Sepehr

here is a quick C++11 solution that worked for me:

这是一个对我有用的快速 C++11 解决方案:

int sig_figs = 3;
double number = 1562.654478;

std::cout << "original number:" << number << std::endl;

number = ([number](int number_of_sig_figs)->double{
    std::stringstream lStream;
    lStream << std::setprecision(number_of_sig_figs) << number;
    return std::stod(lStream.str());
})(sig_figs);

std::cout << "rounded number:" << number << std::endl;