< ()

() / () | |

() / () | |

Category |
Operators |

Type |
Reporter |

Introduced in |
11Oct03 |

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, NaN, or negative infinity. It depends on if the numerator is positive, 0, or negative; respectively. This is sometimes used with other blocks to create new possible changes to a project. If attempted on the 1.4 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

## Workaround

*Main article: List of Block Workarounds*

The block can be replicated with the following code:

delete all of [dividend digits v] delete all 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) 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 (length of [quotient v]) of [quotient v]) = [.]> or <<<(item (length of [quotient v]) of [quotient v]) = [0]> and <(round (quotient)) = (quotient)>> and <[quotient v] contains [.]>>>> delete (length of [quotient v]) of [quotient v] repeat until <<not <(item (1) of [quotient v]) = [0]>> or <(item (2) of [quotient v]) = [.]>> delete (1) 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

### How it Works

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

divided both sides by "a" and take the natural log of both sides to get:

The negative before a log can be rewritten as an exponent in the log, like this:

So:

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 "NaN" 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

() + ()
• () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks... |