?
Solved

How to represent (-2)^0.325 in Java

Posted on 2005-03-16
15
Medium Priority
?
218 Views
Last Modified: 2010-03-31
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?

Thanks for any help you can offer...
0
Comment
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
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13559943
what calculator ??
0
 

Author Comment

by:AndyBeckwith
ID: 13560646
Ti-89
0
 

Author Comment

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

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

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

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

Expert Comment

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

why???
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13560732
Correction:

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

Expert Comment

by:CEHJ
ID: 13560825
Forget that - read the docs wrongly
0
 
LVL 92

Expert Comment

by:objects
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

by:Jim Cakalic
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

by:Webstorm
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

by:Webstorm
ID: 13566032
0
 
LVL 13

Expert Comment

by:Webstorm
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

by:Webstorm
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

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

0
 
LVL 13

Accepted Solution

by:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question