BigDecimal 的数量级
我正在尝试测量计算结果(作为双精度)的准确性,与具有已知正确结果的任意精度的 BigDecimal 相比。我想确保它的正确性直至小数点后 x 位。我认为这可以这样做:(
正确结果的数量级)-(差异的数量级)> x
不过,我无法找到一种简单的方法来计算 BigDecimal 的数量级。有什么想法吗?
如果这是衡量准确性的糟糕方法,我愿意接受其他技术。
I am trying to measure the accuracy of a computed result (as a double), compared to a BigDecimal with the known correct result to arbitrary precision. I want to make sure it is correct up to x decimal places. I figure this can be done like so:
(order of magnitude of correct result) - (order of magnitude of difference) > x
I am having trouble finding a simple way to compute the order of magnitude of a BigDecimal though. Any ideas?
If this is a bad way to measure accuracy, I would be open to other techniques.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要检查正确的小数位数,您只需要差异的数量级,而不是正确结果的数量级。因此,我想您需要将
double
计算结果转换为BigDecimal
,减去精确结果,然后转换回double
并取以 10 为底的对数。或者,如果您只需要检查结果是否精确到小数点后 x 位,则只需检查差异是否大于 0.5 * 10^(-x),或者等效地:
实际上这可能不对取决于你的意思通过“正确到小数点后x位”以及你需要有多严格。您可以说 0.15001 和 0.24999 等于 1 位小数(均四舍五入为 0.2),但 0.19999 和 0.25001 并不等于,尽管差异较小。如果你这样做,我认为你只需要明确地将两个数字四舍五入到小数点后x位,然后进行比较。
To check the number of correct decimal places you just want the order of magnitude of the difference, not the order of magnitude of the correct result. So I suppose you need to convert your
double
computed result to aBigDecimal
, subtract the precise result, then convert back to adouble
and take the logarithm in base 10.Or if you just need to check whether the result is accurate to x decimal places then just check if the difference is greater than 0.5 * 10^(-x), or equivalently:
Actually that probably isn't quit right depending on exactly what you mean by "correct to x decimal places" and how rigorous you need to be. You could say that 0.15001 and 0.24999 are equal to 1 decimal place (both round to 0.2) but that 0.19999 and 0.25001 are not even though the difference is smaller. If you go that way I think you just have to explicitly round both numbers to x decimal places and then compare.
由于您似乎只对粗略估计感兴趣:您应该比较两个值的 log_10,这会告诉您错误的数量级...您可以通过查看 BigInteger 的长度来获得 BigInteger 的 log_10 的良好近似值其十进制表示形式
toString(10).length()
Since you seem to be interested only in a rough estimate: you should compare the log_10 of both values, which tells you the order of magnitude of you error... You can get a good approximation of log_10 of a BigInteger by looking at the length of its decimal representation
toString(10).length()