将双精度值四舍五入到(某种程度上)较低精度的好方法是什么? [英] What is a good way to round double-precision values to a (somewhat) lower precision?

查看:186
本文介绍了将双精度值四舍五入到(某种程度上)较低精度的好方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我必须使用第三方函数/算法,该函数将一组 double 精度值作为输入,但是显然对输入数据中很小的变化很敏感.但是对于我的应用程序,对于(几乎)相同的输入,我必须获得相同的结果!特别是我有两个测试输入数组,它们在小数点后第5位之前都是相同的,但仍然得到不同的结果.因此,导致问题"的原因必须是在小数点后第5个位置之后.

My problem is that I have to use a thrid-party function/algorithm which takes an array of double-precision values as input, but apparently can be sensitive to very small changes in the input data. However for my application I have to get identical results for inputs that are (almost) identical! In particular I have two test input arrays which are identical up to the 5-th position after the decimal point and still I get different results. So what causes the "problem" must be after the 5-th position after the decimal point.

现在,我的想法是将输入舍入到稍低的精度,以便从非常相似但<100%相同的输入中获得相同的结果.因此,我正在寻找一种好的/有效的方法来将 double 精度值四舍五入到稍微较低的精度.到目前为止,我正在使用以下代码四舍五入到小数点后的第9个位置:

Now my idea was to round the input to a slightly lower precision in order to get identical results from inputs that are very similar, yet not 100% identical. Therefore I am looking for a good/efficient way to round double-precision values to a slightly lower precision. So far I am using this code to round to the 9-th position after the decimal point:

double x = original_input();
x = double(qRound(x * 1000000000.0)) / 1000000000.0;

这里qRound()是从Qt开始的普通双精度到整数舍入函数.这段代码有效,并且确实通过两个有问题的"测试集解决了我的问题.但是:有没有更有效的方法呢?

Here qRound() is the normal double to integer rounding function from Qt. This code works and it indeed resolved my problem with the two "problematic" test sets. But: Is there a more efficient way to this?

还有什么让我感到困扰:对于-100.0到100.0范围内的输入数据(如我当前的输入数据),四舍五入到小数点后的第9位可能是合理的.但是,例如,对于-0.001到0.001范围内的输入数据,它可能太多(即,精度损失太多).不幸的是,在其他情况下,我不知道输入值在什么范围内...

Also what bothers me: Rounding to the 9-th position after the decimal point might be reasonable for input data that is in the -100.0 to 100.0 range (as is the case with my current input data). But it may be too much (i,e, too much precision loss) for input data in the -0.001 to 0.001 range, for example. Unfortunately I don't know in what range my input values will be in other cases...

毕竟,我认为我需要的是类似执行以下操作的函数:通过适当舍入,将给定的 double 精度值X截断为最多LN小数点后的位置,其中L是 double 精度可以存储(表示)给定值的小数点后的位置数; N是固定的,例如3 .这意味着对于小"值,与大"值相比,我们允许在小数点后保留更多位置.换句话说,我想将64位浮点值四舍五入为(略微)较小的精度(例如60位或56位),然后将其存储回64位double值.

After all, I think what I would need is something like a function which does the following: Cut off, by proper rounding, a given double-precision value X to at most L-N positions after the decimal point, where L is the number of positions after the decimal point that double-precision can store (represent) for the given value; and N is fixed, like 3. It means that for "small" values we would allow more positions after the decimal point than for "large" values. In other words I would like to round the 64-Bit floating-point value to a (somewhat) smaller precision like 60-Bit or 56-Bit and then store it back to a 64-Bit double value.

这对您有意义吗?如果是这样,您能建议一种在C ++中(有效)做到这一点的方法吗?

Does this make sense to you? And if so, can you suggest a way to do this (efficiently) in C++ ???

提前谢谢!

推荐答案

问题不明显.我仍然觉得您正在尝试查看这些值是否在可接受的范围内.您可以检查第二个值是否在某个百分比范围内(例如+/- 0.001%),而不是==

The business scenario is not evident from the question; still I feel you are trying to see the values are within an acceptable range. Rather than ==, you can check if the second value is within a certain % range (say +/- 0.001%)

如果范围百分比不能固定(均值,则根据精度长度而有所不同;例如,对于2个小数位,0.001%可以,但对于4个十进制的0.000001%是必需的),则可以以1/尾数得出.

If the range percentage cannot be fixed (mean, varies based on precision length; say, for 2 decimal places, 0.001 percent is fine but for 4 decimal 0.000001 percent is needed) then, you can arrive it by 1/mantissa.

这篇关于将双精度值四舍五入到(某种程度上)较低精度的好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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