C ++有效数字 [英] C++ significant figures

查看:137
本文介绍了C ++有效数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何进行涉及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.

谢谢!

推荐答案

http://www.cppreference.com/wiki/numeric/c/startrel =nofollow> math.h中的良好数学库

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.

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

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

来源

也可在打印时通常人们使用_snprintf或printf,你可以格式化这些双精度,浮动到你想要的精度:

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);

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

这说明你需要一个总共
字段的8个字符,在8
字符内,最后2个将保存
的小数部分。

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);

_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.

这篇关于C ++有效数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆