(Added break between two templates) |
(rewrote part mentioning snap and removed unnecesary and distracting inline scratch block.) |
||

(9 intermediate revisions by 4 users not shown) | |||

Line 1: | Line 1: | ||

− | '''Solving [[Wikipedia:Exponentiation|exponents]]''' is the process of multiplying a number by itself | + | '''Solving [[Wikipedia:Exponentiation|exponents]]''' is the process of multiplying a number (the ''base'') by itself a number (the ''power'') of times. For example: |

'''2<sup>3</sup> = 2 * 2 * 2 = 8''' | '''2<sup>3</sup> = 2 * 2 * 2 = 8''' | ||

− | Many Scratchers have requested a block | + | Many Scratchers have requested a block for calculating exponents.<ref>http://scratch.mit.edu/discuss/topic/15646/</ref> Such a block does not yet exist in scratch, but it is included in some [[Alternatives_to_Scratch|scratch alternatives]], such as [[Snap! (programming language)|Snap!]]. |

− | == | + | ==Methods for Calculating Exponents in Scratch== |

− | There are multiple | + | There are multiple workarounds for solving exponents in Scratch. |

=== Repeat Method === | === Repeat Method === | ||

− | {{note| | + | {{note|This method can only be used with whole number powers.}} |

− | |||

<scratchblocks> | <scratchblocks> | ||

Line 34: | Line 33: | ||

=== Logarithmic Method === | === Logarithmic Method === | ||

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

<scratchblocks> | <scratchblocks> | ||

− | ([10 ^ v] of ((power) * ([log v] of ([abs v] of (base))))) | + | (round ([10 ^ v] of ((power) * ([log v] of ([abs v] of (base)))))) |

</scratchblocks> | </scratchblocks> | ||

or | or | ||

<scratchblocks> | <scratchblocks> | ||

− | ([e ^ v] of ((power) * ([ln v] of ([abs v] of (base))))) | + | (round ([e ^ v] of ((power) * ([ln v] of ([abs v] of (base)))))) |

</scratchblocks> | </scratchblocks> | ||

− | ==== How it | + | ==== 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 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 m<sup>n</sup> and 10<sup>log m<sup>n</sup></sup> = m<sup>n</sup>. | * The math works because n * log m = log m<sup>n</sup> and 10<sup>log m<sup>n</sup></sup> = m<sup>n</sup>. | ||

====Special Cases==== | ====Special Cases==== | ||

− | These are | + | These are some cases that the above method will fail to calculate correctly: |

* 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 0<sup>0</sup> so that it does not give an error then. | * 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 0<sup>0</sup> so that it does not give an error then. | ||

Line 66: | Line 65: | ||

==Editing Scratch== | ==Editing Scratch== | ||

{{obsolete feature|cat=no}} | {{obsolete feature|cat=no}} | ||

− | |||

{{see also|Blockmaking FAQs}} | {{see also|Blockmaking FAQs}} | ||

Line 81: | Line 79: | ||

==References== | ==References== | ||

− | |||

<references /> | <references /> | ||

## Latest revision as of 18:27, 2 March 2020

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

**2 ^{3} = 2 * 2 * 2 = 8**

Many Scratchers have requested a 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!.

## Contents

## Methods for Calculating Exponents in Scratch

There are multiple workarounds for solving exponents in Scratch.

### Repeat Method

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 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]> then 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, but can not be used with negative bases.

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

or

(round ([e ^ v] of ((power) * ([ln v] of ([abs 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 m
^{n}and 10^{log mn}= m^{n}.

#### Special Cases

These are some cases that the above method will fail to calculate correctly:

- 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 0^{0}so that it does not give an error then.

- The base is negative: If one wants to be able to do something such as x
^{p}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 10^{x}= -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, it's a good idea to use an if block for whether p is odd or even ( if p mod 2 = 0 then p is even).

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)))::operators) if<(base) < [0]> then set [result v] to ((1) / (result)) end

Note: | Positive rational exponents, e.g. 10^{½} do work. Thus, this can be used to find N-roots of numbers. |

## Editing Scratch

This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective. |

*See also: Blockmaking FAQs*

Scratch can be modified to add a block which solves exponents. This is simplest to do in Scratch 1.4 and previous versions. The block specification is:

```
('%n ^ %n' r exp:of: - -)
```

and the method is:

```
exp: b of: n
^ b raisedTo: n
```