Questions tagged [pow]

pow is a function that exists in various programming languages that usually takes two numbers as input and returns the first number to the power of the second number. DO NOT USE THIS TAG for questions relating to the Rack server, use [rack-pow] instead.

The pow(...) function is a mathematical function commonly featured in most programming language libraries. The pow function represents exponentiation. It takes in two numerical arguments a, b, and returns another numerical value, a to the power of b.

In mathematical notation, we write ab, or if space isn't enough, a^b using the caret. Note that the caret operator may indicate bitwise XOR notation instead. Some languages use the double-asterisk operator ** as an equivalent to pow(a,b).

Usages:

  • C/C++: double pow(double x, double y)
    • Including a header <math.h> is required for C/C++.
    • powf is for floats, powl is for long doubles.
  • Erlang: math:pow(X, Y)
  • Java: Math.pow(double x, double y), returns double
  • JavaScript: Math.pow(number, number) (note all JS numbers are floating-point number type)
  • C#: double Pow(double x, double y) (capital Pow)
  • Python: math.pow(x, y) (Equivalent to x**y, but it converts both arguments to floating-point values unlike **).

Equivalent notations

  • Lua, Mathematica: x ^ y (^ could mean bitwise OR in other languages)
  • Python, Ruby: x ** y
    • Python also have a three-argument pow(x, y, z), which computes x**y modulo z.

Special Cases:

  • Pow(0,0) means zero to the power of zero. Technically, it is undefined, but some implementations return 1, such as Java. Others may return NaN or even incur undefined behavior.
  • Pow(x,1) sometimes returns x regardless of what value x is
  • Pow(x,0) and Pow(x,±Infinity) can result in ±0 or ±Infinity based on mathematical result and the signs of the arguments.
  • Pow(1,±Infinity) can result in NaN.

Related tags:

References:

695 questions
22
votes
4 answers

Does pow() work for int data type in C?

I was simply writing a program to calculate the power of an integer. But the output was not as expected. It worked for all the integer numbers except for the power of 5. My code is: #include #include int main(void) { int a,b; …
basanta
  • 363
  • 1
  • 3
  • 8
19
votes
3 answers

How could I optimize this calculation ? (x^a + y^a +z^a)^(1/a)

As the title shows. I need to do a lot of the calculation like this: re = (x^a + y^a + z^a)^(1/a). where {x, y, z} >= 0. more specific, a is a positive floating point constant, and x, y, z are floating point numbers. The ^ is an exponentiation…
blackball
  • 718
  • 1
  • 6
  • 19
18
votes
5 answers

Strange behaviour of the pow function

While running the following lines of code: int i,a; for(i=0;i<=4;i++) { a=pow(10,i); printf("%d\t",a); } I was surprised to see the output, it comes out to be 1 10 99 1000 9999 instead of 1 10 100 1000 10000. What…
Kroor Singh
  • 255
  • 2
  • 7
16
votes
10 answers

Why pow(10,5) = 9,999 in C++

Recently i write a block of code: const int sections = 10; for(int t= 0; t < 5; t++){ int i = pow(sections, 5- t -1); cout << i << endl; } And the result is wrong: 9999 1000 99 10 1 If i using just this code: for(int t = 0; t < 5; t++){ …
Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
16
votes
3 answers

Why is printf not using scientific notation?

I understand that this is a common problem. However I can't find a solid straight answer. 16 ^ 54 = 1.0531229167e+65 (this is the result I want) When I use pow(16,54), I…
Simon.
  • 1,886
  • 5
  • 29
  • 62
14
votes
3 answers

Why Does Math.pow(x,y) Count as a Double?

I'm writing a Java program to calculate how much food it will take to get a monster to a certain level in My Singing Monsters. When I run the program, it says, "cannot convert from double to int". Can someone explain why this is? Here's the…
Jason Chen
  • 312
  • 1
  • 4
  • 12
14
votes
4 answers

Creating `**` power operator for Scala?

I quite like the ** syntax for pow, available in many languages (such as Python). Is it possible to introduce this into Scala, without modifying the Scala 'base' code? My attempt at an Int only one: import scala.math.pow implicit class PowerInt(i:…
A T
  • 13,008
  • 21
  • 97
  • 158
13
votes
6 answers

Wrong result by Java Math.pow

If you try to run the following code public class Main { public static void main(String[] args) { long a = (long)Math.pow(13, 15); System.out.println(a + " " + a%13); } } You will get "51185893014090752 8" The correct value of…
Maxim Mayers
  • 185
  • 2
  • 6
13
votes
1 answer

What is the equivalent for bigint.pow(a) in Go?

In my use case, I would like to know how the following Java code would be implemented in Go BigInteger base = new BigInteger("16"); int exponent = 1; BigInteger a = base.pow(exponent); //16^1 = 16 I am able to import the math/big package and create…
Dany
  • 2,692
  • 7
  • 44
  • 67
12
votes
2 answers

Where are the inaccuracies in math.sqrt() and math.pow() coming from for large numbers?

If you take a number, take its square root, drop the decimal, and then raise it to the second power, the result should always be less than or equal to the original number. This seems to hold true in python until you try it on 99999999999999975425…
Evan
  • 1,348
  • 2
  • 10
  • 20
12
votes
5 answers

How to force pow(float, int) to return float

The overloaded function float pow(float base, int iexp ) was removed in C++11 and now pow returns a double. In my program, I am computing lots of these (in single precision) and I am interested in the most efficient way how to do it. Is there some…
Michal
  • 671
  • 3
  • 9
  • 22
12
votes
12 answers

finding cube root in C++?

Strange things happen when i try to find the cube root of a number. The following code returns me undefined. In cmd : -1.#IND cout<
ilcredo
  • 785
  • 2
  • 7
  • 17
12
votes
6 answers

Why is Math.pow(-0, -7) === -Infinity?

Is there a rationale for Math.pow(-0, x) evaluating to Infinity for all negative x, except for the odd ones when it's -Infinity? I mean: Math.pow(-0, -6); // Infinity Math.pow(-0, -7); // -Infinity Math.pow(-0, -7.33); //…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
12
votes
1 answer

GCC C++ pow accuracy

So i was in a computing contest and i noticed a weird bug. pow(26,2) would always return 675, and sometimes 674? even though correct answer is 676. These sort of errors also occur with pow(26,3), pow(26,4) etc After some debugging after the contest…
Michael Chen
  • 121
  • 1
  • 4
11
votes
7 answers

Theoretical vs actual time-complexity for algorithm calculating 2^n

I am trying to compute the time-complexity and compare it with the actual computation times. If I am not mistaken, the time-complexity is O(log(n)), but looking at the actual computation times it looks more like O(n) or even O(nlog(n)). What could…
Filip
  • 759
  • 4
  • 17
1
2
3
46 47