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

Fastest way to obtain a power of 10

I'll use Java as an example here, as it may be completely language-specific, but I'm generally interested in ways to do this. Of course, the simplest and obvious way is this: Math.pow(10, x) but as far as I know, power is a fairly expensive…
xeruf
  • 2,602
  • 1
  • 25
  • 48
11
votes
5 answers

What is happening here in pow function?

I have seen various answer here that depicts Strange behavior of pow function in C. But I Have something different to ask here. In the below code I have initialized int x = pow(10,2) and int y = pow(10,n) (int n = 2). In first case it when I print…
CuriosGuy
  • 181
  • 1
  • 9
11
votes
3 answers

Why does pow() calculate wrong when Webkit is running?

I have a Qt C++ application where there is a GUI thread in which some floating point calculation happens. It also opens QWebView where there is a flash player with some video. It is obvious that closing of QWebView interfere on new next floating…
Ezee
  • 4,214
  • 1
  • 14
  • 29
11
votes
4 answers

Math.Pow() vs Math.Exp() C# .Net

Can anyone provide an explanation of the difference between using Math.Pow() and Math.Exp() in C# and .net ? Is Exp()just taking a number to the Power using itself as the Exponent?
CraigJSte
  • 912
  • 6
  • 17
  • 33
10
votes
5 answers

Math.Pow is not calculating correctly

I'm having a problem with C#. To be precise with the Math.pow(). If I try to calculate 15^14 then I get "29192926025390624". But if I calculate it with Wolfram Alpha I get "29192926025390625". As you can see this only difference is 1 number. Wolfram…
Olivier_s_j
  • 5,490
  • 24
  • 80
  • 126
10
votes
4 answers

How is pow() calculated in C?

Our professor said that you can't calculate ab if a<0 using pow() because pow() uses natural logarithms to calculate it (ab=eb ln a) and since it's undefined for negative numbers it can't be calculated. I tried it and it works as long as b is an…
Szymon
  • 460
  • 6
  • 17
10
votes
1 answer

How to uninstall pow (node) on mac osx

How to uninstall pow (http://pow.cx/) on mac osx? I frequently encounter the firewall warning of Norton due to an Unix Executable File called "node" It seems to be related to pow. I tried to uninstall this with command "$ curl…
chenghuayang
  • 1,424
  • 1
  • 17
  • 35
10
votes
9 answers

Power function using recursion

I have to write a power method in Java. It receives two ints and it doesn't matter if they are positive or negative numbers. It should have complexity of O(logN). It also must use recursion. My current code gets two numbers but the result I keep…
dEv93
  • 163
  • 1
  • 3
  • 11
10
votes
4 answers

Math.Pow taking an integer value

From http://msdn.microsoft.com/en-us/library/system.math.pow.aspx int value = 2; for (int power = 0; power <= 32; power++) Console.WriteLine("{0}^{1} = {2:N0}", value, power, (long) Math.Pow(value, power)); Math.Pow takes…
Dave Mateer
  • 6,588
  • 15
  • 76
  • 125
9
votes
5 answers

C's pow function refuses to work with variable exponent

Let's say I have the following code snippet: int i; double value; for(i = 0; i < CONSTANT; i++) { value = (double)pow(2, i); } Trying to compile this code yields an "undefined reference to `pow'" error. Including or excluding math.h makes no…
user12163
9
votes
4 answers

pow for SSE types

I do some explicitly vectorised computations using SSE types, such as __m128 (defined in xmmintrin.h etc), but now I need to raise all elements of the vector to some (same) power, i.e. ideally I would want something like __m128 _mm_pow_ps(__m128,…
Walter
  • 44,150
  • 20
  • 113
  • 196
9
votes
5 answers

Find Cube root of a number Using System.Math.Pow() method in C#

While writing a program I came across finding the cube root of a number in one of my functions. when I used the below code, I was getting an incorrect value for the cube root (1 was getting printed for n = 64). public static void cubicPairs(double…
gkb
  • 1,449
  • 2
  • 15
  • 30
9
votes
3 answers

I got different results with pow(10,2) and pow(10,j), j=2;

This one prints 100: int j=2; int i= pow(10,2); printf("%d\n", i); and this one prints 99: int j=2; int i= pow(10,j); printf("%d\n", i); Why?
9
votes
3 answers

pow() cast to integer, unexpected result

I have some problems using an integer cast for the pow() function in the C programming language. The compiler I'm using is the Tiny C Compiler (tcc version 0.9.24) for the Windows platform. When executing the following code, it outputs the…
Jori
  • 1,122
  • 2
  • 18
  • 36
9
votes
5 answers

pow function in C

I write a C code that have power function that is from math.h library. when I compiled my program, I received an error which is " undefined reference to 'pow' function ", I compile my program using gcc compiler (fedora 9). I insert -lm flag to gcc…
hamb
  • 159
  • 1
  • 2
  • 11
1 2
3
46 47