When it's a power of 2 keep in mind that you can use a simple and fast shift expression: 1 << exponent
For example:
22 = 1 << 2 = (int) Math.pow(2, 2)
210 = 1 << 10 = (int) Math.pow(2, 10)
For larger exponents (over 31) use long instead:
232 = 1L << 32 = (long) Math.pow(2, 32)
BTW, in Kotlin you have shl instead of <<:
(Java) 1L << 32 = 1L shl 32 (Kotlin)
When it's a power of 2 keep in mind that you can use a simple and fast shift expression: 1 << exponent
For example:
22 = 1 << 2 = (int) Math.pow(2, 2)
210 = 1 << 10 = (int) Math.pow(2, 10)
For larger exponents (over 31) use long instead:
232 = 1L << 32 = (long) Math.pow(2, 32)
BTW, in Kotlin you have shl instead of <<:
(Java) 1L << 32 = 1L shl 32 (Kotlin)
Integers are only 32 bits. This means that its max value is 2^31 -1. As you see, for very small numbers, you quickly have a result which can't be represented by an integer anymore. That's why Math.pow uses double.
If you want arbitrary integer precision, use BigInteger.pow. But it's of course less efficient.
^ in java does not mean to raise to a power. It means XOR.
You can use java's Math.pow()
And you might want to consider using double instead of int—that is:
double height;
double weight;
Note that 199/100 evaluates to 1.
we can use
Math.pow(2, 4);
this mean 2 to the power 4 (2^4)
answer = 16
Why there is no power operator in Java / C++? - Software Engineering Stack Exchange
java - Multiplication vs ^ operator vs Math.pow() for integer powers - Stack Overflow
Can somebody please help me with Math.pow and specifically raising to the power of.
Do I round up or down in java?
Videos
Generally speaking, the primitive operators in C (and by extension C++) are designed to be implementable by simple hardware in roughly a single instruction. Something like exponentiation often requires software support; so it's not there by default.
Also, it's provided by the standard library of the language in the form of std::pow.
Finally, doing this for integer datatypes wouldn't make much sense, because most even small values for exponentiation blow out the range required for int, that is up to 65,535. Sure, you could do this for doubles and floats but not ints, but why make the language inconsistent for a rarely used feature?
This question is answerable for C++: Stroustrup, "Design and Evolution of C++" discusses this in section 11.6.1, pp. 247-250.
There were general objections to adding a new operator. It would add to the already overcomplicated precedence table. The members of the working group thought it would give only minor convenience over having a function, and they wanted to be able to substitute their own functions sometimes.
There was no good candidate for an operator. ^ is exclusive-or, and ^^ invited confusion because of the relationship between & and | and && and ||. ! was unsuitable since there would be the natural tendency to write != for exponentiation of an existing value, and that was already taken. The best available may have been *^, which apparently nobody really liked.
Stroustrup considered ** again, but it already has a meaning in C: a**p is a times whatever p points to, and char ** c; declares c as a pointer to pointer to char. Introducing ** as a token meaning "declaration of a pointer to pointer to", "times what the next thing points to" (if it's a pointer) or "exponentiation" (if followed by a number) caused precedence problems. a/b**p would have to parse as a/(b**p) if p were a number, but (a/b) * *p if p were a pointer, so this would have to be resolved in the parser.
In other words, it would have been possible, but it would have complicated the precedence table and the parser, and both are already too complicated.
I don't know the story about Java; all I could do would be speculate. As for C, where it started, all C operators are easily translated into assembly code, partly to simplify the compiler and partly to avoid hiding time-consuming functionality in simple operators (the fact that operator+() and others could hide great complexity and performance hits was one of the early complaints about C++).
The ^ operator is not performing exponentiation - it's a bitwise "exclusive OR" (aka "xor").
Using integer math for 100000000 raised to the fourth power will give incorrect results - a 32-bit integer cannot store numbers that large.
Math.pow() will use floating point arithmetic. The answers may not be 100% accurate due to precision issues, but should be capable of representing the required range of results.
To get 100% accurate values for numbers that large, you should use the BigInteger class. However it will not be particularly fast. This is a trade off you have to make when considering accuracy vs performance.
the ^ operator in Java is bitwise exclusive OR and definitaly not similiar to the power function.
Reference
- http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html