Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Math.pow 1

Status
Not open for further replies.

chals1

Technical User
Sep 3, 2003
73
ES
Why does Math.pow(a,1/n) give 1.0 ? where n is an int

I use it in this sentence to get n-root of a complex number :

temp[k] = new Complejo(Math.pow(a,1/n),(b+k*2*Math.PI)/n);
 
//try this
System.out.println(Math.pow((double)5,1.0d/2));
//Math.pow((double)a,1.0d/n));
//* Please mark this post as helpful if the code is suitable for you
 
What does (double) mean, and 'd' close to 1.0 ?
Do you consider n as int ?
 
public static double pow(double a,double b)
Math.pow requires two double literals as input.
An integer literal is divided by integer literal give a result of integer literal.
int top=11;
int bottom=2;
System.out.println("top/bottom= "+top/bottom);
//--> top/bottom= 5 (the result is trancated)
Look at here for some rules

so Math.pow(5,1/2) gives result of 1.0d because 5 to power of 0 is 1. 1/2 gives 0.
Math.pow((double)a,1.0d/n));
A double literal divided by any literal number(except 0) give double result, so n need not to be casted--> (double)n
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top