Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have an exponential operator?

Tags:

java

pow

exponent

Is there an exponential operator in Java?

For example, if a user is prompted to enter two numbers and they enter 3 and 2, the correct answer would be 9.

import java.util.Scanner; public class Exponentiation {      public static double powerOf (double p) {         double pCubed;          pCubed = p*p;         return (pCubed);     }      public static void main (String [] args) {         Scanner in = new Scanner (System.in);          double num = 2.0;         double cube;              System.out.print ("Please put two numbers: ");         num = in.nextInt();          cube = powerOf(num);          System.out.println (cube);     } } 
like image 882
user3362992 Avatar asked Feb 28 '14 01:02

user3362992


People also ask

How do you do exponential operations in Java?

Syntax: public static double pow(double a, double b) Parameter: a : this parameter is the base b : this parameter is the exponent. Return : This method returns ab. Example 1: To show working of java.


1 Answers

There is no operator, but there is a method.

Math.pow(2, 3) // 8.0  Math.pow(3, 2) // 9.0 

FYI, a common mistake is to assume 2 ^ 3 is 2 to the 3rd power. It is not. The caret is a valid operator in Java (and similar languages), but it is binary xor.

like image 134
Paul Draper Avatar answered Sep 22 '22 07:09

Paul Draper