Solved

How to represent (-2)^0.325 in Java

Posted on 2005-03-16
Medium Priority
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a clâ€¦
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how â€¦
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Booleâ€¦
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month10 days, 11 hours left to enroll