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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Newbie Need Help with Calculation

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I'm new to Java. This may sound like a total newbie question, but how do you do exponentiation in Java? I can't find an operator.

Here's my sample code:


class quad
{
public static void main (String[] args)
{
double x = 4;
double value = ((3 * x^2) - (8*x) + 4);

System.out.println("At X = " + x + "the value is " + value);
}
}



Thanks in advance,
scripter73
Change Your Thinking, Change Your Life.
 
Found the answer to my own question. There is no exponentiation operator for Java. Therefore if you want to perform a square, you have to do something like (x * x) and (x * x * x) for a cube, and so on.

Also, for those who are more advanced, you can use function Math.pow(double x, double p). But beware, Math.pow is an expensive operation.

HTH someone.


Change Your Thinking, Change Your Life.
 
Don't know much about exponents ... never was much good at maths !

But your error was that you were trying to perform arithmetic operator functions on a mix of primitive data types (double and int). You must explicitly cast in order to resolve :
double value = ((3 * ((int)x^2)) - (8*x) + 4);

Ben
 
Hi Sedj,

Thanks for the advice, but when I try that, I get inaccurate results.

I get At X = 4.0 the value is -10.0

Any ideas?

Thanks.
Change Your Thinking, Change Your Life.
 
In the java.math class there is a method called Math.pow(double, double). The first double in the parenthesis is what you are finding the power of, the second double number is the exponent.
 
yeah, so do I :
//where x = 4
eg : System.out.println(((int)x^2)); gives 6

but System.out.println((3 * (int)x^2)); gives 14 ...
so the '^' seems to be performing the calcualtion and then adding on the 2 ??!! I thought the '^' was a logical xor operator ... as I said I never was any good with maths !
It may also have something to do with operator precedence :

I dont know - its too late on a Friday afternoon to be working out java's arithmetic operators !

Operator Precedence

Java follows a well-defined set of rules that govern the order in which operators
will be evaluated in an expression. These rules form an operator precedence or
hierarchy.

Precedence Level Operator Operation Associates

1 + unary plus R to L
- unary minus R to L

2 * multiplication L to R
/ division L to R
% remainder L to R

3 + addition L to R
- substraction L to R
+ string concatenation L to R

4 = assignment R to L
 
Thanks, owls.
I referenced that in my response.

I appreciate it.


Change Your Thinking, Change Your Life.
 
Hi sedj,

Let me research it, and if I find out anything, I'll let you know!

Thanks for your help.
scripter73
Change Your Thinking, Change Your Life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top