(Redirected from Sqrt () (block))

*"() of ()" redirects here. For the Sensing block with two dropdowns used to find Sprite attributes, see () of () (Sensing block).*

() of () | |

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 clicking the down arrow and selecting a new function from the drop-down menu.

## 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" 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.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, it results in `NaN`

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 ([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))

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 e^{3}, 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 10^{6} (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 e^{5} (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 highest 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 next whole number. For example, ceil(3.14) = 4 and ceil(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 ((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.^{[clarify]}

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

([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 sin^{2}θ + cos^{2}θ = 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... |

## Tan of 90 Bug

In mathematics, the tangent of 90° (usually represented as `tan 90°`

) is undefined, as the tangent of 90° resolves to 90 / 0, which is a division by zero and therefore undefined (`NaN`

in computer terminology). However, due to the limitations of Flash, which is what Scratch 2.0 is based on, the tangent of 90° is instead equal to 16331239353195370, which is not undefined. Therefore, the script below causes the sprite to disappear off the edge of the Stage, rather than remaining at 0, 0:

forever go to x:([tan v] of (90)) y:([tan v] of (90)) end