使用Math.pow在Java中对数字进行平方运算会获得精度错误 [英] Squaring Numbers in java using Math.pow getting error of precision

查看:391
本文介绍了使用Math.pow在Java中对数字进行平方运算会获得精度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题要写一个方法来输入LONG类型的数组,该数组存储从0到63(即2 0 到2 )的所有2的幂的值63 )

I have do to a question to write a method to input an array of type LONG that stores the values of all the powers of 2 from 0 to 63 (i.e. 20 to 263)

输出阵列屏幕的内容. 提示:使用Math.pow(x,y)将数字x放入指数y

Output the contects of the array screen. Hint: Math.pow(x,y) is ued to put number x to exponent y

到目前为止,我有类似的东西

so far I have something like

我不断收到错误消息或Math.pow(size,2);需要很长时间,发现两倍. 我尝试过Math.pow(i,2);我收到相同的错误./可能会失去精度,任何帮助:)谢谢

I keep getting error or Math.pow(size,2); required long, found double. I tried Math.pow(i,2); I get same error./ possible loss of precision ,, any help :) thanks

class ws3q2
{
    public static void main(String[]args)
    {

        int ARRAY_SIZE = 5;

        long size = ARRAY_SIZE;

        long[] array = new long[ARRAY_SIZE];

        for(int i = 0; i < ARRAY_SIZE; ++i)
        {
        array[i] = Math.pow(size,2);
        }

    }
}

推荐答案

更好的转变

对于2 x 其中 x ∈ℕ,最好将其写为位移

Better shift

For 2x where x ∈ ℕ, you are better off writing this as a bit shift

1L << x

代替浮点指数

pow(2, x)

在精度和性能方面.实际上,1L << x是如此容易计算,以至于我宁愿编写它而不是array[x],因此您可能会完全避免使用该数组.

both in terms of precision and in terms of performance. In fact, 1L << x is so easy to compute that I'd prefer writing that instead of array[x], so you might perhaps avoid the array altogether.

pow(x, 2)将是 x 2 ,您可以像x*x那样更轻松地进行计算.那么,是2还是平方的幂呢?

pow(x, 2) as you write in your code would be x2 which you could compute more easily as x*x. So which one is it to be, powers of two or squares?

此外,您编写的pow(size,2)使用size并且不依赖i.因此,数组的所有值都将相等.我想那不是你的意思.

Furthermore, you write pow(size,2) which uses size and does not depend on i. So all values of your array would be equal. I guess that is not what you meant.

错误消息的原因是Math.pow的结果是两倍,并且在Java中禁止从doublelong的隐式转换.您必须编写一个显式强制转换,即(long)pow(2, x).但这确实趋于零,因此轻微的数字错误可能会导致错误的结果.您对Math.pow的唯一准确性保证是:

The reason of the error message is the fact that the result of Math.pow is double, and implicit conversion from double to long is forbidden in Java. You'd have to write an explicit cast, i.e. (long)pow(2, x). But that does round towards zero, so a slight numeric error might cause a wrong result. The only exactness guarantee you have for Math.pow is this:

计算结果必须在准确结果的1 ulp之内.

The computed result must be within 1 ulp of the exact result.

您可以在进行转换之前添加0.5,或者使用Math.round而不是强制类型转换,但是如您所见,您必须对浮点数学的内部原理了解很多得到这个正确的.最好坚持使用整数数学,并完全避免使用doubleMath.pow.

You could add 0.5 before doing the conversion, or use Math.round instead of the cast, but as you see, you have to know a lot about the internal workings of floating point math to get this correct. Better to stick to integer math and avoid double and Math.pow altogether.

这篇关于使用Math.pow在Java中对数字进行平方运算会获得精度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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