Cube root of a negative number

后端 未结 4 1993
情深已故
情深已故 2020-12-19 03:12

I\'m trying to find the cube root of a negative number but I get a NaN. Any help?

System.out.println(Math.pow(-8, 1.0 / 3.0));
4条回答
  •  感情败类
    2020-12-19 03:22

    The Java documentation for Math.pow states:

    If the first argument is finite and less than zero [...] [and] if the second argument is finite and not an integer, then the result is NaN.

    You could use Math.cbrt to get the cube root:

    double result = Math.cbrt(-8.0);
    

提交回复
热议问题