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
446
votes
5 answers

How is Math.Pow() implemented in .NET Framework?

I was looking for an efficient approach for calculating ab (say a = 2 and b = 50). To start things up, I decided to take a look at the implementation of Math.Pow() function. But in .NET Reflector, all I found was…
Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
141
votes
20 answers

How to get the Power of some Integer in Swift language?

I'm learning swift recently, but I have a basic problem that can't find an answer I want to get something like var a:Int = 3 var b:Int = 3 println( pow(a,b) ) // 27 but the pow function can work with double number only, it doesn't work with…
林鼎棋
  • 1,995
  • 2
  • 16
  • 25
141
votes
11 answers

Why isn't `int pow(int base, int exponent)` in the standard C++ libraries?

I feel like I must just be unable to find it. Is there any reason that the C++ pow function does not implement the "power" function for anything except floats and doubles? I know the implementation is trivial, I just feel like I'm doing work that…
Dan O
  • 4,323
  • 5
  • 29
  • 44
90
votes
7 answers

Exponentials in python: x**y vs math.pow(x, y)

Which one is more efficient using math.pow or the ** operator? When should I use one over the other? So far I know that x**y can return an int or a float if you use a decimal the function pow will return a float import math print( math.pow(10, 2)…
user3084006
  • 5,344
  • 11
  • 32
  • 41
90
votes
9 answers

Why is Math.pow(0, 0) === 1?

We all know that 00 is indeterminate. But, javascript says that: Math.pow(0, 0) === 1 // true and C++ says the same thing: pow(0, 0) == 1 // true WHY? I know that: >Math.pow(0.001, 0.001) 0.9931160484209338 But why does Math.pow(0, 0) throw no…
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
81
votes
6 answers

Does Java have an exponential operator?

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…
user3362992
  • 843
  • 1
  • 6
  • 4
52
votes
2 answers

Why is pow(int, int) so slow?

I've been working on a few project Euler exercises to improve my knowledge of C++. I've written the following function: int a = 0,b = 0,c = 0; for (a = 1; a <= SUMTOTAL; a++) { for (b = a+1; b <= SUMTOTAL-a; b++) { c =…
Fang
  • 2,199
  • 4
  • 23
  • 44
50
votes
9 answers

Math.Pow vs multiply operator (performance)

Anyone knows if multiply operator is faster than using the Math.Pow method? Like: n * n * n vs Math.Pow ( n, 3 )
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
42
votes
4 answers

pow() seems to be out by one here

What's going on here: #include #include int main(void) { printf("17^12 = %lf\n", pow(17, 12)); printf("17^13 = %lf\n", pow(17, 13)); printf("17^14 = %lf\n", pow(17, 14)); } I get this output: 17^12 =…
jsj
  • 9,019
  • 17
  • 58
  • 103
37
votes
3 answers

How to: pow(real, real) in x86

I'm looking for the implementation of pow(real, real) in x86 Assembly. Also I'd like to understand how the algorithm works.
Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69
37
votes
4 answers

Math.pow yields different result depending on java version

I'm running the following code on a JDK Version 1.7.0_60: System.out.println(Math.pow(1.5476348320352065, (0.3333333333333333))); The result is: 1.1567055833133086 I'm running exactly the same code on a JDK Version 1.7.0. The result is:…
Damnum
  • 1,839
  • 16
  • 35
30
votes
9 answers

BigInteger.pow(BigInteger)?

I'm playing with numbers in Java, and want to see how big a number I can make. It is my understanding that BigInteger can hold a number of infinite size, so long as my computer has enough Memory to hold such a number, correct? My problem is that…
PeterW
  • 309
  • 1
  • 3
  • 3
29
votes
3 answers

How to do a fractional power on BigDecimal in Java?

In my little project, I need to do something like Math.pow(7777.66, 5555.44) only with VERY big numbers. I came across a few solutions: Use double - but the numbers are too big Use BigDecimal.pow but no support for fractional Use the…
Eugene Marin
  • 1,706
  • 3
  • 23
  • 35
26
votes
4 answers

Replacing extrordinarily slow pow() function

We have a CFD solver and while running a simulation, it was found to run extraordinarily slow on some machines but not others. Using Intel VTune, it was found the following line was the problem (in Fortran): RHOV= RHO_INF*((1.0_wp -…
tpg2114
  • 14,112
  • 6
  • 42
  • 57
22
votes
3 answers

calculate mod using pow function python

So, If i would like to calculate the value of 6^8 mod 5 using the pow function, what should I put in a line?? In the assumption that You don't need to import it first I know that pow is used like pow (x, y) = pow (6, 8) = 6^8 and My guess is…
mark T
  • 243
  • 1
  • 2
  • 5
1
2 3
46 47