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
6
votes
1 answer

Fastest pow() replacement via modified exp. by squaring when lower powers are already calculated

EDIT: Goal : Generate a ubiquitous method for deriving a custom power function that outperforms the built-in pow(double, uint) by reusing precalculated/cached powers from power calculations on common variables. What's already been done: I've…
Jason R. Mick
  • 5,177
  • 4
  • 40
  • 69
6
votes
2 answers

Is there a substitute for Pow in BigInteger in F#?

I was using the Pow function of the BigInteger class in F# when my compiler told me : This construct is deprecated. This member has been removed to ensure that this type is binary compatible with the .NET 4.0 type…
Peter
  • 47,963
  • 46
  • 132
  • 181
6
votes
4 answers

Strange pow(x, y); behaviour

While doing my homework I noticed something really strange that I just can't figure out why. int x = 5; cout << pow(x, 2); The result is 25. That's fine. But if I write the same program like this: int x = 5; int y = pow(x, 2); cout << y; The…
SoapyCro
  • 175
  • 2
  • 9
6
votes
6 answers

Math.Pow() is broken

And no, this does not (to my understanding) involve integer division or floating-point rounding issues. My exact code is: static void Main(string[] args) { double power = (double)1.0 / (double)7.0; double expBase = -128.0; …
cowlinator
  • 7,195
  • 6
  • 41
  • 61
5
votes
3 answers

Pow() calculates wrong?

I need to use pow in my c++ program and if i call the pow() function this way: long long test = pow(7, e); Where e is an integer value with the value of 23. I always get 821077879 as a result. If i calculate it with the windows calculator i get…
user1004012
  • 87
  • 2
  • 8
5
votes
2 answers

Why is my factorial program working but my almost identical pow program is not working?

Here is my factorial program—this is executing and giving a correct result: #include int main() { int n; printf("enter the no="); scanf("%d", &n); fun(n); printf("%d\n", fun(n)); return 0; } int fun(int n) { …
mayank raj
  • 59
  • 3
5
votes
3 answers

How to raise a column in pandas DataFrame to consecutive powers

Is there a pythonic way to raise a column in DataFrame (xRaw) to consecutive powers? Is there something like xRaw[:,k] = xRaw.pow(k) for k in range(1,6)
Alex
  • 69
  • 6
5
votes
1 answer

PyCUDA: Pow within device code tries to use std::pow, fails

Question more or less says it all. calling a host function("std::pow ") from a __device__/__global__ function("_calc_psd") is not allowed from my understanding, this should be using the cuda pow function instead, but it isn't.
Bolster
  • 7,460
  • 13
  • 61
  • 96
5
votes
1 answer

Is there has a function Math.Pow(A,n) for long type?

I'm testing a small C# program fragment: short min_short = (short)(int)-Math.Pow(2,15); short max_short = (short)(int)(Math.Pow(2, 15) - 1); Console.WriteLine("The min of short is:{0};\tThe max of short is:{1}", min_short,…
Wuying283
  • 99
  • 1
  • 6
5
votes
2 answers

Negative power in modular pow()

How can we use pow with a negative exponent in a modular context? pow(x, y, [z]) If z is present, x and y must be of integer types, and y must be non-negative. >>> pow(11444, -357) 0.0 >>> pow(11444, -357) % 48731 0.0 >>> pow(11444, -357,…
kAldown
  • 610
  • 2
  • 8
  • 27
5
votes
3 answers

Pow precision with unsigned longs

So I am trying to do the pow (x, y). Where x and y are unsigned longs and the result is stored in an unsigned long. This result will be smaller than 2^63 so I should be able to do it. But since it returns a floating point number I get inaccurate…
LifeisHard
  • 125
  • 2
  • 2
  • 10
5
votes
2 answers

Loss of precision when using pow in C++

10^1.64605 = 44.2639330165 However in C++ using pow: double p = pow(10,1.64605) returns 44.2641. Is there a way to increase the precision here? I tried casting both sides to long double but that didn't help either. More interesting…
user3639557
  • 4,791
  • 6
  • 30
  • 55
5
votes
1 answer

Iterative implementation of pow(x, n)

I've found an iterative implementation of pow(x, n) which takes o(log n) time and constant space like below: double pow(double x, int n) { double left = x; double right = 1; if (n<0) return 1/(x*pow(x,-n-1)); // Avoid binary…
eaglesky
  • 730
  • 2
  • 13
  • 28
5
votes
5 answers

Math.pow(0.0, 0.0) returns 1; should be undefined or error ?

Math.pow(0.0, 0.0) in Java returns 1 which is wrong. 0^0 is undefined. The same problem exists also in the windows calculator (I am using Win7). Why is that? Mathematica declares it as an error as well as my Casio scientific calculator, why not java…
5
votes
8 answers

Where is pow function defined and implemented in C?

I read that the pow(double, double) function is defined in "math.h" but I can't find its declaration. Does anybody know where this function declared? And where is it implemented in…
user188276