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

Python : overflow error long int too large to convert to float

I had to compute 2 to the power of 8635. I came across this error when i was computing 2^8635. Any suggestion how to solve this in python. Using Decimal module also didn't help. math.exp(2**8635) Traceback (most recent call last): File…
realn
  • 1,732
  • 3
  • 12
  • 20
7
votes
1 answer

Why is 2**100 so much faster than math.pow(2,100)?

When discussing the question Exponentials in python x.**y vs math.pow(x, y), Alfe stated that there would be no good reason for using math.pow instead of the builtin ** operator in python. timeit shows that math.pow is slower than ** in all cases.…
Wolf
  • 9,679
  • 7
  • 62
  • 108
7
votes
1 answer

Calculating Floating Point Powers (PHP/BCMath)

I'm writing a wrapper for the bcmath extension, and bug #10116 regarding bcpow() is particularly annoying -- it casts the $right_operand ($exp) to an (native PHP, not arbitrary length) integer, so when you try to calculate the square root (or any…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
6
votes
4 answers

return value of pow() gets rounded down if assigned to an integer

I am using the pow function in C and storing the return value in an integer type. see the code snippet below: for (i = 0; i < 5; i++){ val = (int)pow(5, i); printf("%d, ", val); } here i, and val are integers and the output is 1, 5, 24,…
ango
  • 829
  • 2
  • 10
  • 23
6
votes
3 answers

Three argument pow for arrays

pow accepts a third argument for modulo pow(x, y, z) that is more efficient computation than x ** y % z. How can you do that with arrays? What I've tried: >>> import numpy as np >>> A = np.array(range(10)) >>> pow(A, 23, 13) TypeError: unsupported…
no step on snek
  • 324
  • 1
  • 15
6
votes
2 answers

pow for Integral Values

I need a version of pow for integers. I have 2 problems that I need to solve with pow: If the result is larger than my integral type I need to clamp to numeric_limits::max() I need to be able to deal with 41.99999 rounding up to 42, rather than…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
6
votes
0 answers

pow() returning wrong result under specific conditions (and an unexpected fix) - Why is all this?

I have been creating a short program for implementing Sieve of Eratosthenes. In he program, I have used int startingPoint which held the value of the current prime number, which had it's multiplications being marked non-prime, starting from its…
MrLumie
  • 247
  • 2
  • 10
6
votes
2 answers

pow() function working without any math libraries

It seems on some software/compilers the pow() function works without any math libraries at all. Only with . But in others it complains. Have math functions been added to the library or to some other place?
YelizavetaYR
  • 1,611
  • 6
  • 21
  • 37
6
votes
1 answer

Raising number to fractional power java

I used the following code: double pow = 3/7; double num = 85; System.out.println(Math.pow(num, pow)); Expected result: 6.71... The output is 1.0 Any idea why?
Greg Peckory
  • 7,700
  • 21
  • 67
  • 114
6
votes
3 answers

java.math.BigInteger pow(exponent) question

I did some tests on pow(exponent) method. Unfortunately, my math skills are not strong enough to handle the following problem. I'm using this code: BigInteger.valueOf(2).pow(var); Results: var | time in ms 2000000 | 11450 2500000 | 12471 3000000…
Jan Kraus
  • 63
  • 1
  • 3
6
votes
2 answers

Math.pow yields different results upon repeated calls

After upgrading to Java 1.8.0_20 our test system reported errors, but the code was not changed. I found out, that Math.pow() called with exactly the same input parameters yields different results upon repreated calls. In Java 1.8.0_11 it behaves as…
Adam
  • 597
  • 6
  • 11
6
votes
2 answers

how can I calc pow of fractions or nth root square in numpy?

I'm trying to calculate the 8th root square of a value or its ^1/8, but numpy is always returning the wrong value temp = 141.18 h2 = temp ** (1/8) h2_ = np.power(temp, (1/8)) my output is always 1.0 . I've tried square command too. I need to use…
marco
  • 915
  • 4
  • 17
  • 35
6
votes
4 answers

Ambiguous call to overloaded function 'pow'

I'm having some problems runnning the following code. I got this: error C2668: 'pow' : ambiguous call to overloaded function. I've tried to manually cast the arguments to the appropiate type using static_cast, however I think I get some pointer…
Stefan
  • 83
  • 1
  • 6
  • 20
6
votes
12 answers

how to get exponents without using the math.pow for java

This is my program // ************************************************************ // PowersOf2.java // // Print out as many powers of 2 as the user requests // // ************************************************************ …
user3552056
  • 61
  • 1
  • 1
  • 2
6
votes
9 answers

Recursive power function: approach

I'm programming for a while now(beginner), and recursive functions are a somewhat abstract concept for me. I would not say I'm stuck, program works fine, I'm just wondering if the function itself could be written without the pow function in the code…
MoSFeT
  • 95
  • 1
  • 1
  • 7