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

Very slow std::pow() for bases very close to 1

I have a numerical code that solves an equation f(x) = 0, in which I have to raise x to a power p. I solve it using a bunch of things, but in the end I have Newton's method. The solution happens to be equal to x = 1, and thus is the cause of my…
4
votes
2 answers

pow() from math.h library - How to Apply using functions

So I'm writing a bit of code that needs to raise a function's return value to a certain power. I recently discovered that using the '^' operator for exponentiation is useless because in C++ it is actually an XOR operator or something like that. Now…
Ram Sidharth
  • 183
  • 4
  • 4
  • 11
4
votes
7 answers

How to raise an int or long to a power in C++

Trivial question: I am just wondering, if one wants to raise an int or a long to the power of another int or long, does (long)std::pow((double)a,(double)b) suffice, or do I need (long)(0.5 + std::pow((double)a,(double)b)) ?
Cookie
  • 12,004
  • 13
  • 54
  • 83
4
votes
2 answers

Python pow() and modulus

The pow() function in python3 provide the values for exponents. >>>pow(2,3) 8 Python3 has support to negative exponents that is can be represented using pow(10,-1). When I calculated pow(4,-1,5), it gave the output 4. >>> pow(4, -1, 5) 4 I…
Joel Deleep
  • 1,308
  • 2
  • 14
  • 36
4
votes
8 answers

Fast way to compute n times 10 raised to the power of minus m

I want to compute 10 raised to the power minus m. In addition to use the math function pow(10, -m), is there any fast and efficient way to do that? What I ask such a simple question to the c++ gurus from SO is that, as you know, just like base 2, 10…
GoldenLee
  • 737
  • 2
  • 13
  • 28
4
votes
1 answer

How can I compute integer exponentiation modulo a constant in C

I want to compute ab mod c where a, b and c are integer values. Is there an efficient way to do this? pow(a, b) % c does not seem to work.
chqrlie
  • 131,814
  • 10
  • 121
  • 189
4
votes
5 answers

What is the workflow of Pow(x,y) function?

I'm going through "sololearn" and udemy courses to try to learn C#. I am doing the challenges but could not figure out how the below code resulted with 32 (as in 32 is the correct answer here and I am trying to find out why). Can someone explain…
Fuss
  • 59
  • 5
4
votes
2 answers

How can I get integer in Math.pow(10, 10000000)

I always get infinity from: let power = Math.pow(2, 10000000); console.log(power); //Infinity So, can I get integer from this? Maybe I don't understand this task https://www.codewars.com/kata/5511b2f550906349a70004e1/train/javascript? Who…
4
votes
5 answers

Am I going crazy or is Math.Pow broken?

I used the base converter from here and changed it to work with ulong values, but when converting large numbers, specifically numbers higher than 16677181699666568 it was returning incorrect values. I started looking into this and discovered that…
Will Calderwood
  • 4,393
  • 3
  • 39
  • 64
4
votes
2 answers

Is better POW() or POWER()?

I must make an exponentiation of a number and I don't know which function to use between POW() and POWER(). Which of the two functions is better? Looking at the MySQL documentation I saw that they are synonymous, but I wanted to understand if there…
4
votes
2 answers

Why does this code using Math.pow print "HELLO WORLD"?

I discovered the following code. I know, it looks less weird/exciting than this one using seemingly random numbers, but it seems to be more complex than this one using bit shifts on a large number: long[] c = {130636800L, -5080148640L, 13802573088L,…
honk
  • 9,137
  • 11
  • 75
  • 83
4
votes
2 answers

Math.pow gives wrong result

I was trying to repeat a character N times, and came across the Math.pow function. But when I use it in the console, the results don't make any sense to me: Math.pow(10,15) - 1 provides the correct result 999999999999999 But why does Math.pow(10,16)…
Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
4
votes
2 answers

How to vectorize the pow function (with negative bases)?

I'm trying to vectorize (SSE/AVX) the pow function. In all implementations that I have found, it simply vectorized using log and exp: pow(x, y) = exp(y * log(x)) It works well for positive x, but this won't work for negative x since the log of a…
Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110
4
votes
1 answer

Why aren't oversized values outputted from pow() coerced the same way if the exponent is a variable?

I came across this unusual bug while working on some bitwise exercises. When the output of pow() was coerced to an unsigned int, the result of pow() called with a variable as the exponent became zero, while the result when the exponent was a literal…
Alex Patch
  • 125
  • 1
  • 9
4
votes
2 answers

Fastest way to compute (n + 1)^j from (n^j)

I need to compute 0^j, 1^j, ..., k^j for some very large k and j (both in the order of a few millions). I am using GMP to handle the big integer numbers (yes, I need integer numbers as I need full precision). Now, I wonder, once I have gone through…
Matteo Monti
  • 8,362
  • 19
  • 68
  • 114