检测从双精度转换为浮点时的精度损失
我正在编写一段代码,其中必须将双精度值转换为浮点值。我正在使用 boost::numeric_cast 进行此转换,这将提醒我任何溢出/下溢。不过,我也有兴趣知道该转换是否会导致一些精度损失。
例如,
double source = 1988.1012;
float dest = numeric_cast<float>(source);
生成值为 1988.1 的 dest
有没有什么方法可以检测这种精度损失/舍入
I am writing a piece of code in which i have to convert from double to float values. I am using boost::numeric_cast to do this conversion which will alert me of any overflow/underflow. However i am also interested in knowing if that conversion resulted in some precision loss or not.
For example
double source = 1988.1012;
float dest = numeric_cast<float>(source);
Produces dest which has value 1988.1
Is there any way available in which i can detect this kind of precision loss/rounding
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将浮点数转换回双精度型,并将该双精度型与原始值进行比较 - 这应该可以给您一个关于是否存在精度损失的公平指示。
You could cast the float back to a double and compare this double to the original - that should give you a fair indication as to whether there was a loss of precision.
因此,
residual
包含您正在寻找的“损失”。Hence,
residual
contains the "loss" you're looking for.请参阅这些文章了解单精度和双精度浮点数。首先,浮点数的指数有 8 位,而双精度数有 11 位。因此,正如您提到的,任何大于 10^127 或小于 10^-126 的大小都将成为溢出。对于浮点数,数字的实际数字有 23 位,而双精度数有 52 位。显然,双精度数比浮点数的精度要高得多。
假设您有一个数字,例如:1.1123。该数字实际上可能不会编码为 1.1123,因为浮点数中的数字实际上用于以分数形式相加。例如,如果尾数中的位是 11001,则该值将由 1(隐式)+ 1 * 1/2 + 1 * 1/4 + 0 * 1/8 + 0 * 1/16 + 1 * 组成1/32 + 0 * (64 + 128 + ...)。因此,除非您可以以精确数字的方式将这些分数相加,否则无法对精确值进行编码。这是很少见的。因此,几乎总是会出现精度损失。
Look at these articles for single precision and double precision floats. First of all, floats have 8 bits for the exponent vs. 11 for a double. So anything bigger than 10^127 or smaller than 10^-126 in magnitude is going to be the overflow as you mentioned. For the float, you have 23 bits for the actual digits of the number, vs 52 bits for the double. So obviously, you have a lot more digits of precision for the double than float.
Say you have a number like: 1.1123. This number may not actually be encoded as 1.1123 because the digits in a floating point number are used to actually add up as fractions. For example, if your bits in the mantissa were 11001, then the value would be formed by 1 (implicit) + 1 * 1/2 + 1 * 1/4 + 0 * 1/8 + 0 * 1/16 + 1 * 1/32 + 0 * (64 + 128 + ...). So the exact value cannot be encoded unless you can add up these fractions in such a way that it's the exact number. This is rare. Therefore, there will almost always be a precision loss.
根据戴夫的回答,你将会有一定程度的精度损失。但是,如果您想专注于对其进行量化并在超过某个数字时引发异常,则必须打开浮点数本身并解析出尾数和尾数。指数,然后进行一些分析以确定是否超出了容忍范围。
但是,好消息是,它通常是标准 IEEE 浮点浮点数。 :-)
You're going to have a certain level of precision loss, as per Dave's answer. If, however, you want to focus on quantifying it and raising an exception when it exceeds a certain number, you will have to open up the floating point number itself and parse out the mantissa & exponent, then do some analysis to determine if you've exceeded your tolerance.
But, the good news, its usually the standard IEEE floating-point float. :-)