< ()
| () / () | |
() / () | |
| Category | Operators |
| Type | Reporter |
| Introduced in | 11Oct03 (0.x) |
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. Instead of this block, the () Mod () block should be used to find the remainder.
Dividing by 0 returns Infinity, NaN, or -Infinity depending on whether the numerator is positive, 0, or negative, respectively. If attempted on the 1.4 offline editor, it will give a Script Error and stops the script.
Example Uses
The block is mainly used for calculations where division is needed.
Some common uses for the () / () block include:
- Calculator scripts
if <(operation) = [division]> then set [answer v] to ((input1) / (input2)) end
- Mathematical formulas
set [area v] to (((base) * (height)) / (2)) //area of a triangle
- 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)))
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
This workaround works due to logarithm rules. 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.
See Also
|
() + () • () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks...
|