BigDecimal 的数量级

发布于 2024-11-17 04:23:55 字数 209 浏览 1 评论 0原文

我正在尝试测量计算结果(作为双精度)的准确性,与具有已知正确结果的任意精度的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み青杉依旧 2024-11-24 04:23:55

要检查正确的小数位数,您只需要差异的数量级,而不是正确结果的数量级。因此,我想您需要将 double 计算结果转换为 BigDecimal,减去精确结果,然后转换回 double 并取以 10 为底的对数。

或者,如果您只需要检查结果是否精确到小数点后 x 位,则只需检查差异是否大于 0.5 * 10^(-x),或者等效地:

int x = 3; // number of decimal places required
BigDecimal difference = accurateResult.subtract(new BigDecimal(approxResult));
BigDecimal testStat = difference.movePointRight(x).abs();
boolean ok = testStat.compareTo(new BigDecimal(0.5)) <= 0;

实际上这可能不对取决于你的意思通过“正确到小数点后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 a BigDecimal, subtract the precise result, then convert back to a double 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:

int x = 3; // number of decimal places required
BigDecimal difference = accurateResult.subtract(new BigDecimal(approxResult));
BigDecimal testStat = difference.movePointRight(x).abs();
boolean ok = testStat.compareTo(new BigDecimal(0.5)) <= 0;

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.

十年不长 2024-11-24 04:23:55

由于您似乎只对粗略估计感兴趣:您应该比较两个值的 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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文