Tur:Üsleri Çözme

Solving exponents is the process of multiplying a number by itself multiple times. For example:

23 = 2 * 2 * 2 = 8

Many people want a block like this to make exponents easier to program: ( ^ )//category=operators

Using the Method in Scratch
There are multiple ways to do exponents in Scratch. Here are two:

Repeat Method
This script will be able to solve exponents in Scratch; however, it cannot calculate decimals:

when flag clicked ask [What is the base number?] and wait set [1st# v] to (answer) ask [To what power?] 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]> set [ans v] to ((1) / (ans) // a quick workaround for negative powers end say (join [The answer is: ] (ans)) for (2) secs

How it Works

 * The Variable "1st#" is the base number. This is the number that will be multiplied by.
 * The Variable "2nd#" determines how many times to repeat the multiplication.
 * The variable "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. However, it will cause an error if the base is not a positive number. ([10 ^ v] of ((power) * ([log v] of (base)))) or ([e ^ v] of ((power) * ([ln v] of (base))))

How it works

 * The variable "base" is the base and the variable "power" is the power, or the number of times the base is multiplied. The reported value is the answer.
 * The math works because n * log m = log mn and 10log m n = mn.

Special Cases
These are all errors, so it is advised to have cases for them:
 * The base is 0: Since log 0 is undefined, have an if-else that gives 0 for the answer if the base is 0, as long as the power is positive. The answer 1 should be given for 00 so that it does not give an error then.

set [result v] to ([abs v] of (base) // used to spread out math over two lines set [result v] to ([e^ v] of ((power)*([ln v] of (result)))) if<(base) < [0]> set [result v] to ((1) / (result)) end
 * The base is negative: If one wants to be able to do something such as xp where p is an integer, and they want to allow x to be negative, then the solution with logarithms will not work, because log(-1) is undefined (there is no solution to 10x = -1). Ways to solve this are:
 * Use the loop multiplication method, and even just use loop multiplication / division whenever the power is an integer.
 * Factor out the negative and find the answer for the absolute value of the exponent, then divide 1 by it if the exponent is negative. Also, its a good idea to use an if block for whether p is odd or even ( if p mod 2 = 0 then p is even).

Editing Scratch
A custom block can be made in Squeak which solves exponents. The relevant codes are: ('%n ^ %n' #r #exp:of:) exp:t1 of:t2 ^ t1 raisedTo: t2