Wikipedia-logo.svg For more information, see Exponentiation on Wikipedia.

Solving exponents is the process of multiplying a number (the base) by itself a number (the power) of times. For example:

Many Scratchers have requested a ([]^[]::operators) block for calculating exponents.[1] Such a block does not yet exist in Scratch, but it is included in some Scratch alternatives, such as Snap!.

An example implementation can be found here.

Methods for Calculating Exponents in Scratch

There are multiple workarounds for solving exponents in Scratch.

Repeat Method

Note Note: This method can only be used with whole number powers.
when flag clicked
ask [What is the base number?] and wait
set [1st# v] to (answer)
ask [To what exponent?] and wait
set [2nd# v] to (answer)
set [ans v] to (1)
repeat ([abs v] of (2nd#))
    set [ans v] to ((ans) * (1st#))
end
if <(2nd#) < [0]> then
    set [ans v] to ((1) / (ans)) // a quick workaround for negative powers
end
say (join [The answer is: ] (ans)) for (2) seconds

How it Works

  • (1st#) is the base number. This is the number that will be multiplied by.
  • (2nd#) determines how many times to repeat the multiplication.
  • (ans) is the answer, which is the number 1 multiplied by (1st#), (2nd#) times.

Logarithmic Method

The logarithm method is much faster than the repeat method, but can not be used with negative bases.

[10 ^ v] of ((exponent) * ([log v] of ([abs v] of (base))))

or

[e ^ v] of ((exponent) * ([ln v] of ([abs v] of (base))))

How it Works

  • (base) is the base and (exponent) is the exponent, or the number of times the base is multiplied. The reported value is the answer.
  • The math works because and .

Negative Bases

If the base is negative, the reported solution will be incorrect, as is undefined when . There are only two possible cases where the result is a real number when the base is negative:

  • The exponent is an even number: when is even
  • The exponent is an odd number: when is odd

Here is a revised version of the logarithmic method, accounting for both cases:

set [result v] to ([e ^ v] of ((exponent)*([ln v] of ([abs v] of (base)))))
if <<not <((exponent) mod (2)) = (0)>> and <(0) > (base)>> then
    if <((exponent) mod (2)) = (1)> then
        set [result v] to ((0) - (result))
    else
        set [result v] to [NaN]
    end
end

where the result is stored in (result).

Scratch treats boolean values as bits (0 and 1) when they are used in numerical operations. Here is a shortened version of the logic shown above that compacts the result into a single reporter:

([e ^ v] of (([ln v] of ([abs v] of (base))) * (exponent))) * ((1) - ((2) * ((([floor v] of (exponent)) mod (2)) * <(base) < (0)>)))

where the logarithmic method is multiplied by a sign value.

Note Note: Positive rational exponents, e.g. do work. Thus, this can be used to find N-roots of numbers. Additionally, the script works as expected when the base is equal to 0.

References

  1. illusionist. (6/3/2013). "Could we please have an exponents block? ( ) ^ ( )" topic:2860
Cookies help us deliver our services. By using our services, you agree to our use of cookies.