Reputation: 48
I am working on a research project on java in which some tough calculations have to be done. However I am done with most part but stuck at a point.. I have to calculate the following :
(2.1-2.3) raised to power 0.3.
But I get the answer NaN
.. I have tried store result with both float
and double
variables, however it shows same result. Strangely when I did the same with a calculator , it showed result -0.430512
I can not figure out how to make it work with java
Code for same is:
Math.pow((provider1[k][a][m]-provider1[k][j][m]),prior[k][m]);.
When the values of above are 2.1
,2.3
and 0.3
- NaN
is the output.
However when the values are 2.1
, 2.3
and 3
, it gets the correct value (-0.08
). Can someone please help how to do this.
Upvotes: 0
Views: 737
Reputation: 1138
I hope this helps you or someone else in the future:
The link below uses the example below to explain the mathematics behind the complex number solutions you get when you raise a negative base to a fractional number and how abs() can help you. When you try to do with with standard Math.* functions it doesn't know how to handle the complex portion of the solution. Using Java as an example, to make a calculation with a negative base and a positive fractional exponent, normally one could use:
double d = Math.pow(base,exp);
However, with a negative base and fractional exponent this results in NaN because complex solutions exist.
If the complex portion of a solution doesn't matter to you (like it didn't in my case), you can use this approach.
The equation used in this example is a parameter "Phi" in the Yamada and Gunn modification to the Rackett equation:
phi = (1-T/Tc)^(2/7) - (1-Tr/Tc)^(2/7)
double calculatePhi(double T, double Tr, double Tc){
double l = T/Tc;
double m = Tr/Tc;
double n = 2/7.0;
double q = (1-l); //possibly negative
double r = (1-m); //possibly negative
q = abs(q);
double numa = Math.exp(n * Math.log(q));
r = abs(r)
double numb = Math.exp(n * Math.log(r));
double phi = numa - numb;
return phi;
}
See this link for mathematical proof:
Upvotes: 0
Reputation: 178263
According to the Math.pow
Javadocs:
- If the first argument is finite and less than zero
- if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument
- if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument
- if the second argument is finite and not an integer, then the result is NaN.
(emphasis mine)
That is because raising a negative number to a non-integral power yields a complex number. One case is not being able to take the square root of a number: (-1)^0.5
. The result doesn't exist in real numbers, and it inspired the imaginary number i
which in math is defined to be the square root of -1
. In general, raising a negative number to a non-integral power yields a complex number, which is the sum of a real number and an imaginary number.
Upvotes: 4
Reputation: 308763
Looks like a complex number to me.
My HP 15C scientific calculator tells me that (-0.2)^0.3 is an error.
Time for you to buy a new calculator.
Upvotes: 0