*This article 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 |
05Oct07 (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 the block can in most cases only report an *approximation* of a function given an 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.

Before Scratch 2.0, the block could only calculate 12 different mathematical functions, although the ability for the block to calculate the floor and ceiling of a value was added in v145.

## 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 prior to Scratch 1.2, 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 nonnegative. 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 . is the same as .

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 ()" redirects here. For this block prior to Scratch 1.0, see Sqrt () (block).*

"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.414213562373095^{2} (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 is the imaginary unit, defined as .

### 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 (which is equal to ).

#### 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 . 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 . 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 . 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 , or , 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 (that is ), 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.

Note that because of Scratch's lack of support for imaginary numbers, this block will result in `NaN`

if a number is inputted that is less than 0.

#### 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 () 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 1000, the answer is 3 because () is 1000.

### 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. This can also be written as .

#### 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. This can also be written as ceil(x) or .

### int

This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective. |

"Int" (short for integer) was an option in the block's dropdown which was added in v145 of Scratch 2.0 alpha, alongside the "floor" and "ceiling" options. Selecting this option would truncate decimal portion of the number. Note that this is different from the floor function, as the floor function will always round the number down (whereas the number will be rounded up in the "int" function when negative). This dropdown was later removed in v194 of Scratch 2.0 alpha.

## Workarounds

*Main article: List of Block Workarounds*

### Floor and Ceiling

Floor can be replaced with `round((number)-(0.5))`

, and ceiling can be replaced with `round((number)+(0.5))`

.

### 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

This can be modified to work as a reporter:

`(num)*((1)-(<(num)<(0)>*(2)))`

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

Powers can be replicated by multiplying a number a certain amount of times:

`[10 ^ v] of (number)`

workaround:

set [result v] to [1] repeat (number) set [result v] to ((result) * (10)) end

`[e ^ v] of (number)`

workaround:

set [result v] to [1] repeat (number) set [result v] to ((result) * (2.718281828459045)) end

Note that these workarounds only work with positive integers.

Another workaround is this:

([e ^ v] of ((number) * ([ln v] of (10))))

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, can be written as:

([10 ^ v] of ((x) * ([log v] of (2.718281828459045))))

can be written as:

([10 ^ v] of ((x) * ([log v] of (0.36787944117144233))))

and can be written as:

([10 ^ v] of ((x) * ([log v] of (0.1))))

To see how this works and its bugs, go here.

### Sqrt

Square roots can be found by using Heron's method for finding square roots:

set [result v] to [1] repeat (25) set [result v] to (((result) + ((x) / (result))) / (2)) end

Because , square roots can also be found using the method for solving exponents, `([e ^ v] of (([ln v] of (x)) / (2)))`

or `([10 ^ v] of (([log v] of (x)) / (2)))`

.

### 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 .

Similarly tangent can rewritten as

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

because .

### Asin, Acos, and Atan

Much like standard trigonometric functions, inverse trigonometric functions can be rewritten in terms of other functions.

#### Inverse Trigonometric Identities

Asin and acos can both be rewritten with inverse trigonometric identities.

Asin can be rewritten as

(90)-([acos v] of (length))

and similarly, acos can be rewritten as

(90)-([asin v] of (length))

because .

#### Compositions with standard trigonometric functions

Inverse trigonometric functions can be rewritten with standard trigonometric functions.

Asin can be rewritten as

(((length)*(([acos v] of ([sqrt v] of ((1)-((length)*(length)))))/([abs v] of (length))))+(0)

because .

Similarly, acos can be rewritten as

(((length)*(([asin v] of (([sqrt v] of ((1)-((length)*(length))))-(90)))/([abs v] of (length))))+(90)

because .

Atan can be rewritten as

([asin v] of ((length)/([sqrt v] of ((1)+((length)*(length))))))

because .

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