From Test-Scratch-Wiki

*The correct title of this article is***() / ()**. The change of name is due to technical restrictions.

() / () | |

Category |
Operators |

Type |
Reporter |

The **() / ()** block is an Operators block and a Reporter block. The block divides the second value from the first and returns the result.

If the first value is not evenly divisible by the second, the reported value will have decimals. To find the remainder instead, use the () Mod () block.

The numbers can be typed directly into the block, or Reporter blocks can be used instead.

This block can be stacked inside itself — this can be used to fit more numbers in.

Note, dividing by 0 in the online editor will return infinity, 0, or negative infinity. It depends on if the numerator is positive, 0, or negative; respectively. If attempted on an offline editor, it will give a Script Error and stops the script.

## Example Uses

In many projects, numbers must be divided — this block will do the job.

Some common uses for the () / () block are as follows:

- Calculator scripts

if <(operation) = [division]> then set [answer v] to ((input1) / (input2)) end

- Dividing lists of numbers

set [i v] to (1) repeat (length of [list v]) replace item (i) of [list v] with (round ((item (i) of [list v]) / (2)))

- Mathematical formulas

set [area v] to (((base) * (height)) / (2)) //area of a triangle

## Scientific Notation

In Scratch 1.4 and previous versions, it sometimes converts very large numbers into scientific notation to save space. Scientific notation is simply the number in the form a*10^{b}. These can be converted to a normal number by performing any mathematical function on it, such as adding. So if a variable named "number" has a value of 3*10^{3} and you want to display it as a normal number, you can change it by:

((number) + (0))

It will then report "3000".

## Workaround

*Main article: List of Block Workarounds*

The block can be replicated with the following code:

delete (all v) of [dividend digits v] delete (all v) of [quotient v] set [divident dev v] to [0] ask [Divident (x)] and wait set [dividend v] to (answer) ask [Divisor (y)] and wait set [divisor v] to ([abs v] of (answer)) set [no2 neg v] to <(answer) < [0] > if <(dividend) < [0] > then set [count v] to [1] else set [count v] to [0] end set [no1 neg v] to <(dividend) < [0] > repeat (length of (dividend)) change [count v] by (1) if <(letter (count) of (dividend)) = [.]> then set [dividend dec v] to ((length of (dividend)) - (count)) else add (letter (count) of (dividend)) to [dividend digits v] end end repeat ((10) - (dividend dec)) add [0] to [dividend digits v] end if <(round (divisor)) = (divisor)> then set [dec pos v] to [0] else set [count v] to [0] repeat until <(letter (count) of (dividend)) = [.]> change [count v] by (1) end set [dec pos v] to ((length of (divisor)) - (count)) set [dividend v] to ((dividend) * ([10^ v] of (dec pos)::operators)) set [divisor v] to (round ((divisor) * ([10^ v] of (dec pos)::operators))) end set [count v] to [0] set [currently solving v] to [] //That's an empty input, not a space. repeat (length of [dividend digits v]) change [count v] by (1) set [currently solving v] to (join (currently solving) (item (count) of [dividend digits v])) set [times v] to [9] repeat until <((divisor) * (times)) < ((currently solving) + (1))> change [times v] by (-1) add [times v] to [quotient v] set [currently solving v] to ((currently solving) - ((divisor) * (times))) end insert [.] at ((length of [quotient v]) - (8)) of [quotient v] repeat until <not <<(item (last v) of [quotient v]) = [.]> or <<<(item (last v) of [quotient v]) = [0]> and <(round (quotient)) = (quotient)>> and <[quotient v] contains [.]>>>> delete (last v) of [quotient v] repeat until <<not <(item (1 v) of [quotient v]) = [0]>> or <(item (2 v) of [quotient v]) = [.]>> delete (1 v) of [quotient v]

The list "quotient" will contain the quotient.

A more concise workaround is this:

if <(b) < (0)> then set [result v] to (((a) * ([e^ v] of ((-1) * ([ln v] of ((-1) * (b))))::operators)) * (-1)) else set [result v] to ((a) * ([e^ v] of ((-1) * ([ln v] of (b)))::operators)) end

The above script can replace "(a) / (b)", and actually has a few distinct advantages over the original block: 1/0 is reported as "Infinity", instead of reporting an error and stopping the script -1/0 is reported as "-Infinity" 0/0 is reported as "Error!" rather than stopping the script

**How it Works:**

Let's look at the expression in the "else" part of the statement. This is the expression:

result = a * e^ (-1*ln b) = a / b divided both sides by "a" and take the natural log of both sides to get: -1 * ln b = ln (1 / b) or ln(b^-1) The negative before a log can be rewritten as an exponent in the log, like this: a * log b = log (b^a) So: -1 * ln b = ln (b^-1) Because the log of a negative number does not exist, the formula in the "if" part of the statement pretends like "b" is positive and then just flips the answer. Because Scratch reports "ln 0" as -infinity, you can divide by zero using this expression and not get an error.

The following script replicates the block most exactly, as it is a reporter, and returns "Error!" for division by zero:

(((b) * ([e^ v] of ((-1) * ([ln v] of ([abs v] of (b))))::operators)) * ((a) * ([e^ v] of ((-1) * ([ln v] of ([abs v] of (a))))::operators)))

## See Also

() + ()
• () of ()More blocks... |