< ()
() / () | |
() / () | |
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:
set [counter v] to [0] set [dividend v] to [] set [decimal point v] to [0] repeat (length of (a)) change [counter v] by (1) if <(letter (counter) of (a)) = [.]> then set [decimal point 1 v] to ((length of (a)) - (counter)) else if <not <(letter (counter) of (a)) = [-]>> then set [dividend v] to (join(dividend)(letter (counter) of (a))) end end end set [counter v] to [0] set [divisor v] to [0] set [decimal point 2 v] to [0] repeat (length of (b)) change [counter v] by (1) if <(letter (counter) of (b)) = [.]> then set [decimal point 2 v] to ((length of (b)) - (counter)) else if <not <(letter (counter) of (b)) = [-]>> then set [divisor v] to (join(disivor)(letter (counter) of (b))) end end end set [remainder v] to [] set [calculating v] to [] set [conuter v] to [0] repeat (. . .:: grey) // set precision change [counter v] by (1) if <(length of (dividend)) < (counter)> then set [remainder v] to (join(remainder)(0)) else set [remainder v] to (join(remainder)(letter (counter) of (dividend))) end set [greatest factor v] to [0] repeat until <(remainder) < ((divisor) * (greatest factor))> change [greatest factor v] by (1) end if <(remainder) < ((divisor) * (greatest factor))> then change [greatest factor v] by (-1) end set [calculating v] to (join(calculating)(greatest factor)) set [remainder v] to ((remainder) - ((greatest factor) * (divisor))) end set [counter v] to [0] set [quotient v] to [] repeat (length of (calculating)) change [counter v] by (1) set [quotient v] to (join(quotient)(letter(counter) of (calculating))) if <(((length of (dividend)) - (decimal point 1)) + (decimal point 2)) = (length of (quotient))> then set [quotient v] to (join(quotient)[.]) end end if <not<<(a) < [0]> = <(b) < [0]>>> then set [quotient v] to (join[-](quotient)) end
The (quotient)
variable 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.
See Also
() + () • () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks...
|