Solved

# How to represent (-2)^0.325 in Java

Posted on 2005-03-16
Medium Priority
224 Views
According to the documentation on java.lang.Math.pow(double a, double b):

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

In this case, pow(-2,0.325) results in NaN. On my calculator, I get an answer of ~ -1.25. Is there anyway to get java to calculate the correct result of this operation?

0
Question by:AndyBeckwith
• 6
• 3
• 3
• +2

LVL 92

Expert Comment

ID: 13559943
what calculator ??
0

Author Comment

ID: 13560646
Ti-89
0

Author Comment

ID: 13560658
The exact equation I was trying to represent was:

y = -2 ^ (-.025 * x + 2.25) + 5, where x >= 1
0

LVL 86

Expert Comment

ID: 13560684
That will work where (-.025 * x + 2.25) >=1
0

LVL 92

Expert Comment

ID: 13560706
> That will work where (-.025 * x + 2.25) >=1

why???
0

LVL 86

Expert Comment

ID: 13560732
Correction:

That will work where (-.025 * x + 2.25) >=0
0

LVL 86

Expert Comment

ID: 13560825
Forget that - read the docs wrongly
0

LVL 92

Expert Comment

ID: 13560882
Don't think you can, not with the standard classes anyway.
you'll need to find a different exponent implementation that supports it.
0

LVL 19

Assisted Solution

Jim Cakalic earned 300 total points
ID: 13561301
The new Java 5.0 (JDK 1.5) BigDecimal class has an unlimited precision pow method. This is an implementation of a JSR IBM proposed years ago. In case you can't use JDK 1.5 there is an implementation compatible with earlier JVMs available here: http://www2.hursley.ibm.com/decimalj/

Best regards,
Jim Cakalic
0

LVL 13

Expert Comment

ID: 13565919
Hi AndyBeckwith,

>> In this case, pow(-2,0.325) results in NaN. On my calculator, I get an answer of ~ -1.25.
-1.25...  is   - (2 ^ 0.325)    not    (-2) ^ 0.325

(-2) ^ 0.325  =  exp ( 0.325 * log (-2) )
and log(x) for x<0 is not possible without using complex number.
0

LVL 13

Expert Comment

ID: 13566032
0

LVL 13

Expert Comment

ID: 13566183
x ^ n, where x<0 can be represented by a complex number :

log(-1)=exp( i * PI )

x ^ n = (-x)^n * (-1)^n
= (-x)^n * exp( i * n * PI )
0

LVL 13

Expert Comment

ID: 13566278
= (-x)^n  *  cos(n * PI)   +   i * (-x)^n  *  sin(n * PI)

->
(-2)^0.325  ~=  1,2524655 + i * 0,022321
0

LVL 13

Expert Comment

ID: 13566315
>>  log(-1)=exp( i * PI )
in fact :
log(-1) =  i * PI
-1 = exp( i * PI )

0

LVL 13

Accepted Solution

Webstorm earned 450 total points
ID: 13566430
>>  (-2)^0.325  ~=  1,2524655 + i * 0,022321
in fact it's:
(-2)^0.325  ~=  0,6545153 + i * 1,068072
(i forgot to use radians instead of degrees in the calculator for sin & cos)

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula wâ€¦
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the bâ€¦
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and â€¦
###### Suggested Courses
Course of the Month8 days, 10 hours left to enroll