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.
Why there is no power operator in Java / C++? - Software Engineering Stack Exchange
How does Java calculate Math.pow(a,b) and how do I figure out how to figure out how it does it?
Raising a number to a power in Java - Stack Overflow
How to output a number raised to the power 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++).
Hello,
I'm trying to create a method to calculate exponents through addition and factorial instead of multiplication to compare it to the "usual" Math.pow approach. My problem is that Math.pow only works with doubles and I need bigger numbers to actually see any differences.
^ 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
Another implementation with O(Log(n)) complexity
public static long pow(long base, long exp){
if(exp ==0){
return 1;
}
if(exp ==1){
return base;
}
if(exp % 2 == 0){
long half = pow(base, exp/2);
return half * half;
}else{
long half = pow(base, (exp -1)/2);
return base * half * half;
}
}
Try with recursion:
int pow(int base, int power){
if(power == 0) return 1;
return base * pow(base, --power);
}