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
8
votes
2 answers

Strange behaviour of gcc and math.h?

I've been trying to build some code that uses math functions (e.g. pow). math.h is included, and the flag -lm is used during the build. When compilation is called like this (-lm flag at the begining of the command), it failed, saying that there is…
jtextori
  • 239
  • 2
  • 7
8
votes
1 answer

What's difference between Math.pow(9, 18) and 9^18

when I use Math.pow(9, 18) =150094635296999136 when I use web Calculator 9^18 = 150094635296999121 (http://web2.0calc.com/) when I use Google calculator 9^18 = 1.50094635 × 10^17 why it is different ?
JavaUser
  • 81
  • 2
8
votes
3 answers

Best practice with Math.Pow

I'm working on a n image processing library which extends OpenCV, HALCON, ... . The library must be with .NET Framework 3.5 and since my experiences with .NET are limited I would like to ask some questions regarding the performance. I have…
S. Richter
  • 269
  • 1
  • 3
  • 11
8
votes
1 answer

Unresolved reference: pow in Eclipse using Kotlin

I'm trying to write something small in Kotlin and I'm having problems with finding the second power of a Double number. According to this, Double should implement a pow function receiving another Double, but when I try using this method I get…
Neo
  • 3,534
  • 2
  • 20
  • 32
8
votes
3 answers

Fast floating-point power of 2 on x86_64

Is there a fast way to take 2.0 to some floating-point degree x? I mean something faster than pow(2.0, x) and preferrably what vectorizes well with AVX2. The counterpart for integers is 1<
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
8
votes
1 answer

C99: what is the recomended way to handle exceptions raised by `pow()` (overflow or complex number)

executing double result = pow(base, exponent); with arbitrary base and exponent may result in an attempt to compute a value too big or complex. For example with base=-2, exponent=.5 (square root of -2) Should I just check if result==NAN or…
Paolo
  • 15,233
  • 27
  • 70
  • 91
8
votes
4 answers

pow(NAN) is very slow

What is the reason for the catastrophic performance of pow() for NaN values? As far as I can work out, NaNs should not have an impact on performance if the floating-point math is done with SSE instead of the x87 FPU. This seems to be true for…
dasdingonesin
  • 1,347
  • 1
  • 10
  • 16
8
votes
5 answers

Java - Faster alternative to Math.pow() and Math.sqrt()

My program uses Math.pow() to compute a relatively large double number to the power of 2. Later on I need to find the square root of a very large double number. The problem is, I have to do this over a 100,000 times and it is taking really long. Is…
M9A
  • 3,168
  • 14
  • 51
  • 79
7
votes
2 answers

Why am I seeing a one-off error with Math.pow(11, 16)?

I have to compute 11^16 for a project at my Uni. Somehow Math.pow(11,16) computes a solution exactly 1 less than WolframAlpha or my other computation method. My code is: public class Test { public static void main(String args[]) { long…
pezubi
  • 73
  • 3
7
votes
4 answers

Numerical accuracy of pow(a/b,x) vs pow(b/a,-x)

Is there a difference in accuracy between pow(a/b,x) and pow(b/a,-x)? If there is, does raising a number less than 1 to a positive power or a number greater than 1 to a negative power produce more accurate result? Edit: Let's assume x86_64 processor…
SU3
  • 5,064
  • 3
  • 35
  • 66
7
votes
4 answers

How to use pow() in Swift 3 and get an Int

I have the following code: let lootBase = Int(pow((Decimal(level + 1)), 3) * 2) let lootMod = Int(pow(Decimal(level), 2) * 2) value = Int(arc4random_uniform(UInt32(lootBase)) + lootMod) I need value to be an Int. The error I get when I try to…
zeeple
  • 5,509
  • 12
  • 43
  • 71
7
votes
0 answers

AVX512 log2 or pow instructions

I need a AVX512 double pow(double, int n) function (I need it for a binomial distribution calculation which needs to be exact). In particular I would like this for Knights Landing which has AVX512ER. One way to get this is x^n =…
Z boson
  • 32,619
  • 11
  • 123
  • 226
7
votes
6 answers

negative pow in python

I have this problem >>> import math >>> math.pow(-1.07,1.3) Traceback (most recent call last): File "", line 1, in ValueError: math domain error any suggestion ?
JuanPablo
  • 23,792
  • 39
  • 118
  • 164
7
votes
4 answers

Modulo % with big number- Infinity error - Javascript

Is there are trick to get the modulo of big numbers in Javascript. I am getting infinity with modulo(7, 16971, 25777) 7^16971mod25777=NaN function modulo (n, p, m){ var x = Math.pow(n, p); var y = m; var z = x%y; alert(x); return z; }
user6937251
7
votes
4 answers

Poor performance of Java's Math.pow(x, 2) when x = 0

Background Having noticed that the execution of a java program I am working on was slower than expected, I decided to tinker with the area of code which I thought may be causing the issue - a call to Math.pow(x, 2) from within a for loop. Contrary…
Hungry
  • 1,645
  • 1
  • 16
  • 26