Zho:() 数值 ()（运算积木）

The  of  积木 is an 运算类积木 and a 椭圆形积木. The block performs a specified function on a given number and reports the result.

The function can be changed by clicking the down arrow and selecting a new function from the drop-down menu.

用法示例
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 画笔
 * 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

abs
"abs" is an abbreviation for "absolute value." The absolute value of a number is its distance from 0. Another way to describe absolute value is that it makes a number positive if it is negative, it becomes positive, and if it is positive, it stays positive. For example, the absolute value of -3 is +3 and the absolute value of +4 is +4. Another way to write absolute value is |number|. For example, abs(-3) is the same as |-3|.

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!". Scratch 2.0 results in  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 <(letter (1) of (input)) = [-]> then set [input v] to ((0) - (input)) //makes it a positive number so it can be worked with set [output v] to (join [i] ([sqrt v] of (input))) //sets the output to i * the square root of the now positive input else set [output v] to ([sqrt v] of (input))

Note that i is the imaginary unit, defined as √-1.

sin, cos, and tan
sin, cos, and tan are called "trigonometric functions," which is abbreviated "trig functions," or just "trig." 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.



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.

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's 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.

floor
This always rounds the number down to the whole number. For example, floor(1.73) = 1 and floor(-2.74) = -3.

ceiling
This always rounds the number up to the next whole number. For example, ceil(3.14) = 4 and ceil(7.68) = 8.

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 ((num) * (-1)) else set [abs v] to (num) end

or

if <(num) < (0)> then set [abs v] to ((0) - (num)) else set [abs v] to (num)

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, e^x 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.

sqrt
([e^ v] of (([ln v] of (x)) / (2)):: operators)

or

([10^ v] of (([log v] of (x)) * (0.5)):: operators)

or

set [sqrt v] to [0.01] set [a v] to ((sqrt)/(2)) repeat (1000) set [sqrt v] to ((0.5) * ((a) + ((num) / (a)))) end (This type of approximation for the square root of a number uses Newton's method.)

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

([sin v] of ((90) - (angle)))

for cosine. You can also use

([sqrt v] of ((1) - (([cos v] of (angle)) * ([cos v] of (angle)))))

for sine and

([sqrt v] of ((1) - (([sin v] of (angle)) * ([sin v] of (angle)))))

for cosine because sin2θ + cos2θ = 1.

Similarly tangent can rewritten as

(([sin v] of (angle)) / ([cos v] of (angle)))

because tanθ = sinθ / cosθ.