CalcPower

CalcPower

CalcPower[ a, b ] returns a to the power b, if it can be calculated exactly.

Usages

CalcPower[ a, b ]

CalcPower[ a, b ] calculates ab in these cases:

  • a and b are integers. A negative exponent gives a fraction.
  • a is a decimal number and b is an integer.
  • a is an integer and b is a fraction or decimal number (a root), but only if the result is an integer.

In all other cases CalcPower is left unevaluated.

Example in out
An integer power. CalcPower[ 5, 2 ] 25
A negative exponent. CalcPower[ 5, -2 ] 1/25
A negative base. CalcPower[ -2, 3 ] -8
Script variables. x := -4
y := 2
r := CalcPower[x, y]
r = 16
A decimal number as base. CalcPower[ 1.5, 3 ] 3.375
A square root that is an integer. CalcPower[ 9, 1/2 ] 3
A cube root of a negative number. CalcPower[ -27, 1/3 ] -3
(-27)2/3 = 9. CalcPower[ -27, 2/3 ] 9
The square root of 2 is not an integer, so it is not calculated. CalcPower[ 2, 1/2 ] CalcPower[2, 1/2]

CalcPower[ list, b ]

If an argument is a list, the power is calculated for each element.

Example in out
Square each element. CalcPower[ {2, 3}, 2 ] {4, 9}
Powers of 2. CalcPower[ 2, {1, 2, 3} ] {2, 4, 8}
Options

FallbackToPower

If set to True, a power that cannot be calculated is returned as an ordinary power instead of staying unevaluated. Default: False.

Syntax Example out
CalcPower[ a, b, FallbackToPower=>True ] CalcPower[ 2, 1/2, FallbackToPower=>True ] 2^(1/2)
Details
  • Fractions as base are not supported: CalcPower[ 2/3, 2 ] is left unevaluated.
  • Roots of negative numbers with an even root are not calculated: CalcPower[ -4, 1/2 ] is left unevaluated.
  • CalcPower[ 2, 0 ] returns 1.
Invalid input Result
CalcPower[ 2, x ] Left unevaluated: the arguments must be numbers.
CalcPower[ 2 ] Left unevaluated: CalcPower takes two arguments.
CalcPower[ 2, 3, Foo=>1 ] Error: Invalid associative argument for CalcPower: Foo
Warning

CalcPower returns 0 for CalcPower[ 0, 0 ] and for CalcPower[ 0, -1 ], although 0-1 is undefined. Make sure the base is not 0 when the exponent can be 0 or negative, for example when the numbers are generated randomly.

See also: CalcTimes, CalcDivision, FactorInt