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
22
votes
4 answers

Does pow() work for int data type in C?

I was simply writing a program to calculate the power of an integer. But the output was not as expected. It worked for all the integer numbers except for the power of 5. My code is: #include #include int main(void) { int a,b; …
basanta
  • 363
  • 1
  • 3
  • 8
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
  • 116,683
  • 26
  • 284
  • 387
20
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é …
17
votes
2 answers

Element-wise power of scipy.sparse matrix

How do I raise a scipy.sparse matrix to a power, element-wise? numpy.power should, according to its manual, do this, but it fails on sparse matrices: >>> X <1353x32100 sparse matrix of type '' with 144875 stored…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
17
votes
3 answers

Efficient way to compute p^q (exponentiation), where q is an integer

What is an efficient way to compute pq, where q is an integer?
q0987
  • 34,938
  • 69
  • 242
  • 387
17
votes
6 answers

Modular Exponentiation for high numbers in C++

So I've been working recently on an implementation of the Miller-Rabin primality test. I am limiting it to a scope of all 32-bit numbers, because this is a just-for-fun project that I am doing to familiarize myself with c++, and I don't want to…
Axel Magnuson
  • 1,192
  • 1
  • 10
  • 26
17
votes
3 answers

Integer exponentiation in OCaml

Is there a function for integer exponentiation in OCaml? ** is only for floats. Although it seems to be mostly accurate, isn't there a possibility of precision errors, something like 2. ** 3. = 8. returning false sometimes? Is there a library…
user2258552
  • 824
  • 1
  • 11
  • 25
13
votes
4 answers

Javascript Date getTime() code snippet with mysterious additional characters

What is that funky code after the "+" at the end of the second line doing? It is probably made to be hard to understand, since I suspect it's one of the ways they…
Mattis
  • 5,026
  • 3
  • 34
  • 51
12
votes
4 answers

Why is exponentiation applied right to left?

I am reading an Intro to Python textbook and came across this line: Operators on the same row have equal precedence and are applied left to right, except for exponentiation, which is applied right to left. I understand most of this, but I do not…
elitecheese1337
  • 149
  • 2
  • 13
12
votes
4 answers

What is the Prolog operator `^` ("caret")?

What is the Prolog operator ^ ? Looking at The Prolog Built-in Directive op gives a list of the built-in operators. I see ** is exponentiation /\ is or but what is ^ ? Each of the three current answers are of value and I learned something: …
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
12
votes
5 answers

Fastest modular exponentiation in JavaScript

My problem is to compute (g^x) mod p quickly in JavaScript, where ^ is exponentiation, mod is the modulo operation. All inputs are nonnegative integers, x has about 256 bits, and p is a prime number of 2048 bits, and g may have up to 2048 bits. Most…
pts
  • 80,836
  • 20
  • 110
  • 183
11
votes
2 answers

How do I raise X to the power of Y in Powershell?

I (apparently wrongly) assumed there would be a built-in power operator or function in Powershell, but I it seems there is not, or is there?
skeetastax
  • 1,016
  • 8
  • 18
11
votes
5 answers

Using exponentiation **0.5 less efficient than math.sqrt?

A quote from "Python Programming: An Introduction to Computer Science" We could have taken the square root using exponentiation **. Using math.sqrt is somewhat more efficient. "Somewhat", but to what extent, and how?
Robert
  • 2,222
  • 2
  • 21
  • 36
11
votes
2 answers

Implementing addition using multiplication

I've been familiar with the famous question of implementing multiplication using addition, or exponentiation using multiplication, using algorithms of looping or bit-shifting and adding shifted bit groups combos. Now, I wondered if there is any way…
Uriel
  • 15,579
  • 6
  • 25
  • 46
9
votes
5 answers

first n digits of an exponentiation

How do i determine the first n digits of an exponentiation (ab). eg: for a = 12, b = 13 & n = 4, the first 4 digits are 1069.
josh
  • 13,793
  • 12
  • 49
  • 58
1
2
3
24 25