如何测试一个浮点数在C ++中有多少有效数字? [英] How can I test for how many significant figures a float has in C++?

查看:172
本文介绍了如何测试一个浮点数在C ++中有多少有效数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何测试c ++中指定浮点数有多少有效数字?如果我写:

How can I test how many significant figures a specified float has in c++? say if i write:

sigfigs(x);

x是float的值,
将整数值设置为y, sigfigs的数量

x being the value of the float, it would set an integer value to y, the number of sigfigs

我如何以这种方式写一个void函数

how can i write a void function this way

时间,任何答案赞赏

btw mysticial这是要求一个代码来找到一个浮点数量的sig figs,而不是有多少像一个你链接作为副本。-

btw mysticial this is asking for a code to find the amount of sig figs in a float, not how many there are like the one you linked to as a duplicate -.-

推荐答案

您要确定浮点数的精度位数或数在变量中有效数字? C和C ++通常不指定用于 float double 的格式,但如果知道浮点格式中存储和处理的数字,可以确定位的精度。目前,大多数硬件都使用 IEEE 754 格式。查看定义是一个很好的起点。

Are you trying to determine the number of bits of precision in a floating point number or the number of significant figures in a variable? C and C++ do not generally specify the format to be used for float and double, but if you know the floating point format in which the number is stored and processed, you can determine the number of bits of precision. Most hardware these days uses IEEE 754 format. Looking through the definition would be a good place to start.

有效数字是一个完全不同的问题。有效数字的定义包括多少数字实际上有意义的概念,而不是由于浮点表示可用的数字的数量。例如,如果使用12位A / D转换器(以及足够好的模拟设计,所有位都有效)对电压进行采样,则读取的数据将具有12个有效位,并以更高的格式精度不增加有效数字的数量。例如,根据您计划对数据执行的操作,将其存储为16位整数或32位IEEE 754浮点数。在任何一种情况下,您仍然只有12个有效位,即使32位浮点数有24位尾数。

Number of significant figures is an entirely different question. Definition of significant figures includes a notion of how many figures are actually meaningful, as opposed to the number of figures available due to the floating point representation. For example, if you sample a voltage with a 12-bit A/D converter (and good enough analog design that all the bits are significant) then the data that you read will have 12 significant bits, and storing it in a format with higher precision does not increase the number of significant figures. For example, you store it in a 16-bit integer or a 32-bit IEEE 754 floating point number, depending on what you plan to do with the data. In either case you still only have 12 significant bits, even though a 32-bit float has a 24-bit mantissa.

Goldberg的什么计算机科学家现在应该关注浮点算法完全覆盖了问题,如果有效数字和浮点运算。

Goldberg's What Every Computer Scientist Should Now About Floating-Point Arithmetic pretty thoroughly covers the issue if significant figures and floating-point arithmetic.

这篇关于如何测试一个浮点数在C ++中有多少有效数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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