# Math Functions

## Exponential and Logarithmic Functions

Use the functions that follow to compute exponential and logarithmic values. Restricted operations, such as the square root of a negative number, cause a runtime error.

function Exp($n as real) as real

Returns the natural number e raised to the power of a number.

Arguments

$n A number.

Returns

The natural number e raised to the power $n, expressed as a floating-point number. If the result is too large to be represented as a finite floating-point number, this function causes an error.

The examples that follow show how to use the `Exp()`

function with different input values.

// Set $rglobal[0] to Euler’s number raised to the power of 5.5 // (~244.692). $rglobal[0] = Exp(5.5) // The line that follows generates a task error because the result is // too large to express as a real value. $rglobal[1] = Exp(1e308)

To calculate the value of Euler’s number, specify a value of 1 to the `Exp()`

function. Refer to the example that follows.

// Set $rglobal[2] to Euler’s number (~2.71828). $rglobal[2] = Exp(1)

function Sqrt($n as real) as real

Returns the square root of a number.

Arguments

$n A number that is greater than or equal to 0.

Returns

The square root of $n, expressed as a floating-point number.

The examples that follow show how to use the `Sqrt()`

function with different input values.

// Set $rglobal[0] to the square root of 1024 (32.0). $rglobal[0] = Sqrt(1024.0) // Set $rglobal[1] to the square root of 123 (~11.091). $rglobal[1] = Sqrt(123) // The line that follows generates a task error because the input value // is not within the range of acceptable domain values. $rglobal[2] = Sqrt(-2)

function Log($n as real) as real

Returns the natural logarithm (the logarithm to the base e) of a number.

Arguments

$n A number that is greater than 0.

Returns

The natural logarithm (the logarithm to the base e) of $n, expressed as a floating-point number.

The examples that follow show how to use the `Log()`

function with different input values.

// Set $rglobal[0] to e (~2.718). // Set $rglobal[1] to the base-e logarithm of e (1.0). $rglobal[0] = Exp(1) $rglobal[1] = Log($rglobal[0]) // Set $rglobal[2] to the base-e logarithm of 123 (~4.812). $rglobal[2] = Log(123) // The line that follows generates a task error because the input value // is not within the range of acceptable domain values. $rglobal[3] = Log(-0.5)

function Log10($n as real) as real

Returns the common logarithm (the logarithm to the base 10) of a number.

Arguments

$n A number that is greater than 0.

Returns

The common logarithm (the logarithm to the base 10) of $n, expressed as a floating-point number.

The examples that follow show how to use the `Log10()`

function with different input values.

// Set $rglobal[0] to the base-10 logarithm of 100 (2.0). $rglobal[0] = Log10(100) // Set $rglobal[1] to the base-10 logarithm of 123 (~2.090). $rglobal[1] = Log10(123) // The line that follows generates a task error because the input value // is not within the range of acceptable domain values. $rglobal[2] = Log10(-1.23)

function Log2($n as real) as real

Returns the binary logarithm (the logarithm to the base 2) of a number.

Arguments

$n A number that is greater than 0.

Returns

The binary logarithm (the logarithm to the base 2) of $n, expressed as a floating-point number.

The examples that follow show how to use the `Log2()`

function with different input values.

// Set $rglobal[0] to the base-2 logarithm of 1024 (10.0). $rglobal[0] = Log2(1024) // Set $rglobal[1] to the base-2 logarithm of 123 (~6.943). $rglobal[1] = Log2(123) // The line that follows generates a task error because the input value // is not within the range of acceptable domain values. $rglobal[2] = Log2(-1.0)

## Rounding and Remainder Functions

Use the functions that follow to compute rounding and remainder values.

function Abs($n as real) as real

Returns the absolute value of a number.

Arguments

$n A number.

Returns

The absolute value of $n, expressed as a floating-point number. If $n is negative, this function returns -$n. Otherwise, this function returns $n.

The examples that follow show how to use the `Abs()`

function with different input values.

// Set $rglobal[0] to the absolute value of -123 (123.0). $rglobal[0] = Abs(-123) // Set $rglobal[1] to the absolute value of 35.4 (35.4). $rglobal[1] = Abs(35.4)

function Ceil($n as real) as real

Returns the nearest integer that is greater than or equal to a number.

Arguments

$n A number.

Returns

The nearest integer that is greater than or equal to $n, expressed as a floating-point number.

The examples that follow show how to use the `Ceil()`

function with different input values.

// Set $rglobal[0] to 32.001 rounded up to the nearest integer as a // floating-point value (33.0). $rglobal[0] = Ceil(32.001) // Set $rglobal[1] to -12.3 rounded up to the nearest integer as a // floating-point value (-12.0). $rglobal[1] = Ceil(-12.3) // The Ceil() function produces floating-point values that are always // integers, so they can be assigned to an integer variable. $iglobal[0] = Ceil($rglobal[0])

function Floor($n as real) as real

Returns the nearest integer that is less than or equal to a number.

Arguments

$n A number.

Returns

The nearest integer that is less than or equal to $n, expressed as a floating-point number.

The examples that follow show how to use the `Floor()`

function with different input values.

// Set $rglobal[0] to 32.999 rounded down to the nearest integer as // a floating-point value (32.0). $rglobal[0] = Floor(32.999) // Set $rglobal[1] to -96.1 rounded down to the nearest integer as a // floating-point value (-97.0). $rglobal[1] = Floor(-96.1) // The Floor() function produces floating-point values that are // always integers, so they can be assigned to an integer variable. $iglobal[0] = Floor($rglobal[0])

function Round($n as real) as real

Returns a number rounded to the nearest integer.

Arguments

$n A number.

Returns

The value of $n rounded to the nearest integer, expressed as a floating-point number. If $n is halfway between two integers, this function rounds $n away from 0.

The examples that follow show how to use the `Round()`

function with different input values.

// Set $rglobal[0] to 19.158 rounded to the nearest integer (19.0). $rglobal[0] = Round(19.158) // Set $rglobal[1] to -24.158 rounded to the nearest integer (-24.0). $rglobal[1] = Round(-24.158) // Set $rglobal[2] to -19.5 rounded to the nearest integer (-20.0). $rglobal[2] = Round(-19.5) // The Round() function produces floating-point values that are // always integers, so they can be assigned to an integer variable. $iglobal[0] = Round($rglobal[0])

function Trunc($n as real) as real

Returns the integer part of a number.

Arguments

$n A number.

Returns

The integer part of $n with the fractional part removed, expressed as a floating-point number with the same sign as $n.

The examples that follow show how to use the `Trunc()`

function with different input values.

// Set $rglobal[0] to 16.361 with its fractional part removed // (16.0). $rglobal[0] = Trunc(16.361) // Set $rglobal[1] to -8.412 with its fractional part removed (-8.0). $rglobal[1] = Trunc(-8.412) // The Trunc() function produces floating-point values that are // always integers, so they can be assigned to an integer variable. $iglobal[0] = Trunc($rglobal[0])

To retrieve the fractional part of a number, use the `Frac()`

function.

function Frac($n as real) as real

Returns the fractional part of a number.

Arguments

$n A number.

Returns

The fractional part of $n with the integer part removed, expressed as a floating-point number with the same sign as $n.

The examples that follow show how to use the `Frac()`

function with different input values.

// Set $rglobal[0] to the fractional portion of 10.967 (0.967). $rglobal[0] = Frac(10.967) // Set $rglobal[1] to the fractional portion of -24.134 (-0.134). $rglobal[1] = Frac(-24.134) // Set $rglobal[2] to the fractional portion of 19.000 (0.0). $rglobal[2] = Frac(19.000

To retrieve the integer part of a number, use the `Trunc()`

function.

## Trigonometric Functions

Use the functions that follow to do trigonometric calculations. Restricted operations, such as the arc cosine of -2, cause a compiler error or runtime error.

The mathematical constant π (pi), the ratio of a circle’s circumference to its diameter, is commonly used in trigonometric functions. You can define the value of π in your program. Refer to the example that follows.

`#define PI 3.14159265358979323846`

function Acos($n as real) as real

Returns the arc cosine of a number.

Arguments

$n A number that is greater than or equal to -1 and less than or equal to 1.

Returns

The arc cosine of $n, expressed as a floating-point number of radians that is greater than or equal to 0 and less than or equal to π.

The examples that follow show how to use the `Acos()`

function with different input values.

// Set $rglobal[0] to the arc cosine of 1.0 (0.0). $rglobal[0] = Acos(1.0) // Set $rglobal[1] to the arc cosine of 0.0 (π/2 or ~1.571). $rglobal[1] = Acos(0.0) // The line that follows generates a task error because the input value // is not within the range of acceptable domain values. $rglobal[2] = Acos(2)

function Asin($n as real) as real

Returns the arc sine of a number.

Arguments

$n A number that is greater than or equal to -1 and less than or equal to 1.

Returns

The arc sine of $n, expressed as a floating-point number of radians that is greater than or equal to -π/2 and less than or equal to π/2.

The examples that follow show how to use the `Asin()`

function with different input values.

// Set $rglobal[0] to the arc sine of 1.0 (π/2 or ~1.571). $rglobal[0] = Asin(1.0) // Set $rglobal[1] to the arc sine of 0.0 (0.0). $rglobal[1] = Asin(0.0) // The line that follows generates a task error because the input value // is not within the range of acceptable domain values. $rglobal[2] = Asin(-2)

function Atan($n as real) as real

Returns the arc tangent of a number.

Arguments

$n A number.

Returns

The arc tangent of $n, expressed as a floating-point number of radians that is greater than or equal to -π/2 and less than or equal to π/2.

The examples that follow show how to use the `Atan()`

function with different input values.

// Set $rglobal[0] to the arc tangent of 0.0 (0.0). $rglobal[0] = Atan(0.0) // Set $rglobal[1] to the arc tangent of 1.0 (π/4 or ~0.785). $rglobal[1] = Atan(1.0)

function Atan2($y as real, $x as real) as real

Returns the arc tangent of the quotient of two numbers. The signs of the two numbers are used to identify the quadrant of the return value.

Arguments

$y The y-coordinate of a point.

$x The x-coordinate of a point.

Returns

The arc tangent of $y/$x, expressed as a floating-point number of radians that is greater than or equal to -π and less than or equal to π. If $x is 0, this function returns π/2 if $y is positive or -π/2 if $y is negative. If $x is 0 and $y is 0, this function causes an error.

The examples that follow show how to use the `Atan2()`

function with different input values.

// Set $rglobal[0] to the arc tangent when y = 0.0 and x = 5.3 (0.0). $rglobal[0] = Atan2(0.0, 5.3) // Set $rglobal[1] to the arc tangent when y = 1.0 and x = 0.0 (π/2 // or ~1.571). $rglobal[1] = Atan2(1.0, 0.0) // Set $rglobal[2] to the arc tangent when y = 1.0 and x = 1.0 (π/4 // or ~0.785). $rglobal[2] = Atan2(1.0, 1.0) // Set $rglobal[3] to the arc tangent when y = -1.0 and x = 1.0 (-π/4 // or ~-0.785). $rglobal[3] = Atan2(-1.0, 1.0) // The line that follows generates a task error because the input value // is not within the range of acceptable domain values. $rglobal[4] = Atan2(0.0, 0.0)

function Cos($n as real) as real

Returns the cosine of an angle.

Arguments

$n An angle expressed in radians.

Returns

The cosine of $n, expressed as a floating-point number that is greater than or equal to -1 and less than or equal to 1.

The examples that follow show how to use the `Cos()`

function with different input values.

// Define the mathematical constant pi. #define PI 3.14159265358979323846 // Set $rglobal[0] to the cosine of π/4 (Sqrt(2) / 2 or ~0.707). $rglobal[0] = Cos(PI / 4) // Set $rglobal[1] to the cosine of π (-1.0). $rglobal[1] = Cos(PI)

function Sin($n as real) as real

Returns the sine of an angle.

Arguments

$n An angle expressed in radians.

Returns

The sine of $n, expressed as a floating-point number that is greater than or equal to -1 and less than or equal to 1.

The examples that follow show how to use the `Sin()`

function with different input values.

// Define the mathematical constant pi. #define PI 3.14159265358979323846 // Set $rglobal[0] to the sine of π/4 (Sqrt(2) / 2 or ~0.707). $rglobal[0] = Sin(PI / 4) // Set $rglobal[1] to the sine of π (0.0). $rglobal[1] = Sin(PI)

function Tan($n as real) as real

Returns the tangent of an angle.

Arguments

$n An angle expressed in radians.

Returns

The tangent of $n, expressed as a floating-point number.

The examples that follow show how to use the `Tan()`

function with different input values.

// Define the mathematical constant pi. #define PI 3.14159265358979323846 // Set $rglobal[0] to the tangent of π/4 (1.0). $rglobal[0] = Tan(PI / 4) // Set $rglobal[1] to the tangent of π (0.0). $rglobal[1] = Tan(PI)