From Test-Scratch-Wiki
- This tur is about the Operators block. For the Sensing block with two dropdowns used to find Sprite attributes, see () of () (Sensing block). For the old block, see Abs () (block).
() of () | |
([ v] of ()::operators)
| |
Category | Operators |
Type | Reporter |
Introduced in | 1.2 |
The () of () block is an operators block and a reporter block. The block performs a specified function on a given number and reports the result. The function can be changed by using the drop-down menu.
Note that () of () can in most cases only report an approximation of the sqrt, sin, cos, tan, asin, acos, atan, e^, 10^, ln, or log of its input, because Scratch, like most programming languages, represents all real numbers as rationals. (In particular, Scratch uses floating-point numbers.) Because of this, versions of Scratch before 2.0 would report 16331239353195370 as the result of the tan of 90 instead of NaN or Infinity.
Example Uses
In Scratch, advanced calculators would be tricky to program without the () of () block; it performs many functions that may be tricky to replicate with other blocks.
Some common uses for the () of () block are:
- Calculator scripts
- Performing functions on numbers to create unpredictable values
- Mathematical formulas
- Making patterns with the pen
- Calculating scores for games
- Calculating the distance between points
- Determining side lengths and angle measurements of polygons (especially triangles)
- Working in numerical bases other than decimal
Functions
Abs
- "Abs ()" redirects here. For this block in 1.0 and 1.1, see Abs () (block).
"abs" is an abbreviation for "absolute value." The absolute value of a number is its distance from 0. A simpler way to describe an absolute value is that it makes any number positive. If it is negative, it becomes positive, and if it is positive, it stays the same.
For example, the absolute value of -3 is +3. The absolute value of +4.5 is +4.5. Another way to write absolute value is with a vertical line on either side of the number, like |number|. abs(-3) is the same as |-3|.
The negative absolute value does a similar job as the absolute value but makes any number negative. It is written as -|number| and calculated as ((-1) * ([abs v] of (x)))
.
The absolute value function was originally a block in its own right, before this block was introduced in Scratch 1.2 and it was incorporated into it. It looked like this:
Sqrt
"sqrt" is an abbreviation for "square root." A number that is squared is multiplied by itself. For example, when 2 is squared, the answer is 2×2, which is 4. When a number is square rooted, the answer is the number that was squared to get it. So, the square root of 4 is 2. Similarly, the square root of 2 is about 1.414213562373095 because 1.4142135623730952 (1.414213562373095 × 1.414213562373095) is close to 2.
Scratch does not support imaginary numbers, which are the square roots of negative numbers. Attempting to find the square root of a negative number will create a Script Error in Scratch 1.4, breaking the script it is in and returning "Error!". In Scratch 2.0 and 3.0, it results in NaN
(Not A Number) instead. The following script can be used to detect if a negative number is inserted into a square root and make the result mathematically correct.
if <(input) < [0]> then set [input v] to ((0) - (input)) // It needs to convert number to positive value . This can also be done with the number in its absolute/positive value. set [output v] to (join ([sqrt v] of (input)) [i]) // Sets the output to i * the square root of the now-positive input else set [output v] to ([sqrt v] of (input)) end
Note that i is the imaginary unit, defined as √-1.
sin, cos, and tan
sin, cos, and tan are called "trigonometric functions". They are ratios between the sides of a right triangle. The value put in the block is an angle (in degrees), which is one of the angles in the triangle.
Using this block in Scratch will output the result in degrees. To convert it to radians, multiply the value by 0.01745329251.
sin
"sin" is an abbreviation for "sine." The sine of an angle is the ratio between the length of the side that is opposite (across) the triangle from it and the length of the hypotenuse (the side that is across from the right angle). In the picture above, the sine of angle A is equal to side "opposite" divided by side "hypotenuse".
cos
"cos" is the abbreviation for "cosine." The cosine of an angle is the ratio between the length of the side adjacent (next to) it on the triangle and the length of the hypotenuse. In the picture above, the cosine of angle A is equal to side "adjacent" divided by side "hypotenuse".
tan
"tan" is the abbreviation for "tangent." The tangent of an angle is the ratio between the length of the side adjacent to it and the side opposite of it. In the picture above, the tangent of angle A is equal to side "opposite" divided by side "adjacent".
asin, acos, and atan
asin, acos, and atan are "inverse trigonometric functions." While sin, tan, and cos find the ratios from the angles, asin, acos, and atan find the angles, in degrees, from the ratios. The domains (accepted inputs) of asin and acos go from -1 to 1, whereas atan accepts any number (including Infinity). The range of outputs for the functions go from -90 to 90 degrees.
asin
"asin" is the abbreviation for "arcsine" and is also sometimes written as sin−1. When given the ratio (in decimal form) of the length of the opposite side and hypotenuse of a right triangle, it finds the angle.
acos
"acos" is the abbreviation for "arccosine" and is also sometimes written as cos−1. When given the ratio (in decimal form) of the length of the adjacent side and hypotenuse of a right triangle, it finds the angle.
atan
"atan" is the abbreviation for "arctangent" and is also sometimes written as tan−1. When given the ratio (in decimal form) of the length of the opposite side and adjacent side of a right triangle, it finds the angle.
e^ and 10^
e^ and 10^ are both "exponential functions" or "power functions."
e^
"e" is an abbreviation for "Euler's number", which is about 2.718. With the e^ function, e is multiplied by itself the value number of times. For example, if the value is 3, the answer would be e3, or e×e×e, which is about 20.086.
10^
The 10^ function multiplies 10 times itself the value number of times. For example, if the value was 6, the answer would be 106 (that is 10×10×10×10×10×10), which is 1,000,000.
ln and log
ln and log are "logarithmic functions". They do the exact opposite of what the exponential functions do.
ln
ln is "natural log." It figures out how many times e would have to be multiplied by itself to get the value. For example, if the value was 148.4, the answer would be about 5 because e5 (e×e×e×e×e) is around 148.4.
log
"Log" is short for "logarithm." The log function figures out how many times 10 must be multiplied by itself to get the value. For example, if the value is 100, the answer is 2 because 10×10 is 100.
Rounding
Floor
This always rounds the number down to the greatest whole number less than or equal to the number. For example, floor(1.73) = 1 and floor(-2.74) = -3.
Ceiling
This always rounds the number up to the least whole number greater than or equal to the number. For example, ceiling(3.14) = 4 and ceiling(7.68) = 8.
Workarounds
- Main article: List of Block Workarounds
Floor
([floor v] of ((number) / (by)))
can be replaced with (((number) - ((number) mod (by))) / (by))
Abs
If the abs option is wanted, the following code can be used to replicate the block:
if <(num) < (0)> then set [abs v] to ((0) - (num)) else set [abs v] to (num) end
The following script can also be used as a reporter:
([sqrt v] of ((num) * (num)))
This works because the square root of n * n will be n, and multiplying a negative number by itself will return a positive number.
10^, e^, Log, ln
If the 10^ option is wanted, the following code can be used to replicate the block (though it only works for integer powers):
set [result v] to [1] repeat (number) set [result v] to ((result) * (10)) end
Another workaround is this:
([e^ v] of ((number) * ([ln v] of (10))):: operators)
Indeed, this workaround can be more useful than the block itself, as it grants the ability to calculate any positive number to any power. For instance, ex could be written as:
([10^ v] of ((x) * ([log v] of (2.718281828459045))) :: operators)
To see how this works and its bugs, go here.
Use 0.3679175 for ln and 0.1 for log.Template:Issue
Sqrt
set [result v] to [1] repeat (15) set [result v] to (((result) + ((x) / (result))) / (2)) end
This is Heron's method for finding square roots. Because √x = x1/2, square roots can also be found using the method for solving exponents, ([e^ v] of (([ln v] of (x)) / (2)) :: operators)
or ([10^ v] of (([log v] of (x)) / (2)) :: operators)
.
Sin, Cos, and Tan
There are several workarounds for sine, cosine, and tangent. Here "angle" represents the value and θ is an angle in a mathematical formula.
Trigonometric Identities
There are several ways to rewrite these functions using trigonometric identities.
Sin and cosine are shifted by 90° relative to each other, so they can be written as
([cos v] of ((90) - (angle)))
for sine, and
([sin v] of ((90) - (angle)))
for cosine. You can also use
([sqrt v] of ((1) - (([cos v] of (angle)) * ([cos v] of (angle)))))
or
((0) - ([sqrt v] of ((1) - (([cos v] of (angle)) * ([cos v] of (angle))))))
for sine, depending on its sign and
([sqrt v] of ((1) - (([sin v] of (angle)) * ([sin v] of (angle)))))
or
((0) - ([sqrt v] of ((1) - (([sin v] of (angle)) * ([sin v] of (angle))))))
for cosine, also depending on its sign because sin2 θ + cos2 θ = 1.
Similarly tangent can rewritten as
(([sin v] of (angle)) / ([cos v] of (angle)))
because tan θ = sin θ/cos θ.
() + ()
• () - () • () * () • () / () • Pick Random () to () • () < () • () = () • () > () • () and () • () or () • Not () • Join ()() • Letter () of () • Length of () • () Mod () • Round () • () of ()More blocks... |