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.
368 questions
323
votes
19 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,415
  • 2
  • 17
  • 6
306
votes
20 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.
  • 64,223
  • 27
  • 138
  • 202
131
votes
15 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
  • 47,963
  • 46
  • 132
  • 181
125
votes
6 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
  • 32,039
  • 22
  • 142
  • 171
104
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
  • 1,041
  • 2
  • 8
  • 3
103
votes
10 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,467
  • 3
  • 11
  • 22
73
votes
11 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
  • 59,298
  • 38
  • 251
  • 324
63
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
  • 26,657
  • 28
  • 137
  • 252
58
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
  • 2,292
  • 1
  • 19
  • 19
57
votes
5 answers

What is the meaning of number 1e5?

I have seen in some codes that people define a variable and assign values like 1e-8 or 1e5. for example const int MAXN = 1e5 + 123; What are these numbers? I couldn't find any thing on the web...
Kadaj13
  • 1,423
  • 3
  • 17
  • 41
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
  • 769
  • 8
  • 17
42
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
  • 4,462
  • 5
  • 25
  • 35
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,720
  • 2
  • 21
  • 33
27
votes
7 answers

Raise 10 to a power in javascript, are there better ways than this

I have a need to create an integer value to a specific power (that's not the correct term, but basically I need to create 10, 100, 1000, etc.) The "power" will be specified as a function parameter. I came up with a solution but MAN does it feel…
scubasteve
  • 2,718
  • 4
  • 38
  • 49
25
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
  • 57,827
  • 19
  • 139
  • 159
1
2 3
24 25