C++有效数字

发布于 2024-10-10 11:48:53 字数 106 浏览 8 评论 0 原文

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

谢谢!

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

等往事风中吹 2024-10-17 11:48:53

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

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

This should get you what you need:

std::cout.precision(x); // x would be the number of significant figures to output
檐上三寸雪 2024-10-17 11:48:53

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

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


}

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

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

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.

情释 2024-10-17 11:48:53

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

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.

圈圈圆圆圈圈 2024-10-17 11:48:53

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

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.

笨死的猪 2024-10-17 11:48:53

这是一个对我有用的快速 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;

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

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;
北座城市 2024-10-17 11:48:53

现在 C++20 正式支持 std::format 类,您可以在 C++ 中获得 printf 样式的格式设置:

#include <iostream>
#include <format>
    
int main()
{
    double d = 0.1 + 0.2;
    
    std::cout << std::format("{:0.63f}\n", d);
}

Now that C++20 officially supports the std::format class, you can get printf-style formatting within C++:

#include <iostream>
#include <format>
    
int main()
{
    double d = 0.1 + 0.2;
    
    std::cout << std::format("{:0.63f}\n", d);
}
吾家有女初长成 2024-10-17 11:48:53

那么 math.h 中有 优秀的数学库

还可以将数字存储在浮点数中,双打或长双打将允许更精确的操作。

浮点型提供 7 位有效数字,而双精度型提供 16 位有效数字。

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

浮点精度

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

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

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

上面的例子
请求最小字段宽度和
最后两个字符要保留
小数部分。

Well there are good math libraries in 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.

source

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文