Java 无法得到负数的平方根
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19747310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot get square root of negative number
提问by sportsfan72
I am currently trying to get the square root of -1, but in java, Math.sqrt() return NaN for any negative value. How do I get the square root of -1 in Java?
我目前正在尝试获得 -1 的平方根,但在 java 中,Math.sqrt() 为任何负值返回 NaN。如何在Java中获得-1的平方根?
采纳答案by SleepingSpider
You can't get the square root of a negative number. What you could do is to create a Complex number class with methods to carry out complex number arithmetic. You may have a look here for an example: http://introcs.cs.princeton.edu/java/97data/Complex.java.html
你不能得到负数的平方根。您可以做的是创建一个复数类,其中包含执行复数算术的方法。您可以在这里查看示例:http: //introcs.cs.princeton.edu/java/97data/Complex.java.html
回答by MZ4Code
This is a method that will return a string representation of the square root:
这是一个将返回平方根的字符串表示的方法:
static String sqrtImaginaryNum(int i){
String str ="";
double sqrtI = Math.sqrt(Math.abs(i));
if(i<0){
return str+=sqrtI+"i";
}
return str+=sqrtI;
}
回答by Gustavo E. Hennemann
As said before, there aren't real numbers of negative square roots. But there are in imaginary numbers. The GNU Scientific Library can manage this kind of problems, for example, the funcion gsl_complex_sqrt_real. This library were created in C/C++ but you can port it to java.
如前所述,负平方根没有实数。但是有虚数。GNU 科学图书馆可以管理此类问题,例如函数 gsl_complex_sqrt_real。这个库是用 C/C++ 创建的,但你可以将它移植到 java。
Or you can try: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/complex/Complex.html
或者您可以尝试:http: //commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/complex/Complex.html
This is a very simple example using Apache Commons:
这是一个使用 Apache Commons 的非常简单的例子:
Complex number = new Complex(-4);
System.out.println("Square root: " + number.sqrt());
The result is:
结果是:
Square root: (0.0, 2.0)
The meaning is 0.0 in real part and 2.0 in imaginary part. Or just -2i and 2i.
其含义是实部为 0.0,虚部为 2.0。或者只是 -2i 和 2i。
回答by Heeth21
I know this is an old question, but I have an easier way so that the next person coming here would get help.
我知道这是一个老问题,但我有一个更简单的方法,以便下一个人来这里得到帮助。
if(num<0){
num = -num;
ans = Math.pow(num, 0.5);
System.out.println("The result is: " + ans + "i");
}
Input = -16. Output = 4i
输入 = -16。输出 = 4i
回答by Marton Berecz
Here is a one-liner for you:
这是给你的单线:
Since any negative number's square root is a positive number. Just multiply the number with -1 under the square-root in case it's negative:
因为任何负数的平方根都是正数。只需将数字乘以平方根下的 -1,以防它为负数:
var x = -1;
Math.sqrt(x<0 ? x * -1 : x);
回答by Mushini Gopala swamy
you can use this if(x<0){ System.out.println("root of num "+Math.sqrt(x*(-1))+"i");}
你可以用这个 if(x<0){ System.out.println("root of num "+Math.sqrt(x*(-1))+"i");}