Questions tagged [exponentiation]

Anything related to the exponentiation operation, i.e. the process of computing the power of a number, and the corresponding syntax, semantics, constraints and implementation in programming languages.

Exponentiation is the process of raising a number to the nth power, (xn). Mathematically, it is denoted by superscripts, where the superscripted number is the exponent - xy.

However, since many text editors do not support superscripts, an alternative notation is used in code that uses exponentiation - typically a caret (x^y) or two asterisks (x**y). Other programming languages do not support symbolic operators for exponentiation, and rely on the pow function to calculate the exponent.

Please remember that the caret notation ^ may be used for bitwise XOR instead, and some languages does not support a built-in exponentiation operator.

See more

  • : a function that performs exponentiation. Namely, pow(a, b) calculates ab.
  • : do not assume that the ^ operator represents exponentiation. Many languages treat the ^ operator as bitwise-xor instead.
  • : The function ex, base-e exponentiation.
  • : Describes a type of curve produced by an exponent. Here, the the base is constant, and the variable is the exponent - and it grows extremely fast.
  • Wikipedia article for nore information about exponents and their identities.
329 questions
305
votes
18 answers

What does the ^ operator do in Java?

What function does the ^ (caret) operator serve in Java? When I try this: int a = 5^n; ...it gives me: for n = 5, returns 0 for n = 4, returns 1 for n = 6, returns 3 ...so I guess it doesn't perform exponentiation. But what is it then?
joroj
  • 3,225
  • 2
  • 15
  • 6
268
votes
19 answers

The most efficient way to implement an integer based power function pow(int, int)

What is the most efficient way given to raise an integer to the power of another integer in C? // 2^3 pow(2,3) == 8 // 5^5 pow(5,5) == 3125
Doug T.
  • 59,839
  • 22
  • 131
  • 193
116
votes
13 answers

How to do exponentiation in clojure?

How can I do exponentiation in clojure? For now I'm only needing integer exponentiation, but the question goes for fractions too.
Peter
  • 44,153
  • 43
  • 129
  • 175
95
votes
4 answers

Exponentiation in Haskell

Can someone tell me why the Haskell Prelude defines two separate functions for exponentiation (i.e. ^ and **)? I thought the type system was supposed to eliminate this kind of duplication. Prelude> 2^2 4 Prelude> 4**0.5 2.0
skytreebird
  • 951
  • 1
  • 6
  • 3
84
votes
9 answers

Exponentiation operator in Swift

I don't see an exponentiation operator defined in the base arithmetic operators in the Swift language reference. Is there really no predefined integer or float exponentiation operator in the language?
mcgregor94086
  • 1,229
  • 1
  • 10
  • 15
62
votes
2 answers

Exponentiation in Python - should I prefer ** operator instead of math.pow and math.sqrt?

In my field it's very common to square some numbers, operate them together, and take the square root of the result. This is done in pythagorean theorem, and the RMS calculation, for example. In numpy, I have done the following: result =…
heltonbiker
  • 23,225
  • 20
  • 121
  • 212
57
votes
2 answers

Why is -1**2 a syntax error in JavaScript?

Executing it in the browser console it says SyntaxError: Unexpected token **. Trying it in node: > -1**2 ... ... ... ...^C I thought this is an arithmetic expression where ** is the power operator. There is no such issue with other…
psmith
  • 1,742
  • 15
  • 18
54
votes
10 answers

How do you do *integer* exponentiation in C#?

The built-in Math.Pow() function in .NET raises a double base to a double exponent and returns a double result. What's the best way to do the same with integers? Added: It seems that one can just cast Math.Pow() result to (int), but will this always…
Roman Starkov
  • 52,420
  • 33
  • 225
  • 300
54
votes
3 answers

Why does numpy.power return 0 for small exponents while math.pow returns the correct answer?

In [25]: np.power(10,-100) Out[25]: 0 In [26]: math.pow(10,-100) Out[26]: 1e-100 I would expect both the commands to return 1e-100. This is not a precision issue either, since the issue persists even after increasing precision to 500. Is there…
Jishnu
  • 739
  • 7
  • 15
49
votes
4 answers

How to raise a number to a power?

I was trying to raise an integer to a power using the caret operator (^), but I am getting surprising results, e.g.: assert_eq!(2^10, 8); How can I perform exponentiation in Rust?
Matthias Braun
  • 24,493
  • 16
  • 114
  • 144
37
votes
6 answers

Recursive method for x^n optimised for when n is even

I need to write a recursive method using Java called power that takes a double x and an integer n and that returns x^n. Here is what I have so far. public static double power(double x, int n) { if (n == 0) return 1; if (n == 1) …
Omar N
  • 1,700
  • 2
  • 16
  • 28
32
votes
3 answers

Difference between ECMAScript 2016 exponentiation operator and Math.pow()

What is the benefit to using the ECMAScript 2016 exponentiation operator over the current Math.pow()? In other words, besides reducing key strokes, what is the difference between Math.pow(2, 2) => 4 and 2 ** 2 => 4
Matt
  • 3,680
  • 5
  • 21
  • 33
22
votes
1 answer

Why does "^" on a data.frame return a matrix instead of a data.frame like "*" does?

This question is motivated by a bug filed here by Abiel Reinhart on data.table. I noticed that the same happens on data.frame as well. Here's an example: DF <- data.frame(x=1:5, y=6:10) > DF*DF x y 1 1 36 2 4 49 3 9 64 4 16 81 5 25…
Arun
  • 108,644
  • 21
  • 263
  • 366
21
votes
2 answers

Why does exponentiation (e.g., 10^6) take 4 times longer than calculator notation (e.g., 1e6) in R?

Using the scientific notation 10^6 in an R code (as I customarily do) results in a significantly longer computing time than using the calculator representation 1e6: > system.time(for (t in 1:1e7) x=10^6) utilisateur système écoulé …
Xi'an
  • 767
  • 12
  • 39
21
votes
3 answers

Store and work with Big numbers in C

I need help working with very big numbers. According to Windows calc, the exponent 174^55 = 1.6990597648061509725749329578093e+123 How would I store this using C (c99 standard)? int main(){ long long int x = 174^55; //result is 153 …
Martin Konecny
  • 50,691
  • 18
  • 119
  • 145
1
2 3
21 22