Skip to content

Math Functions

Common Pseudocode provides a variety of built-in mathematical functions for common operations.

Returns the absolute value of a number.

Syntax:

result = abs of number
result = abs(number)

Example:

absolute = abs of -5 // 5
positive = abs(-10) // 10

Rounds a number to the nearest integer.

Syntax:

result = round number
result = round(number)

Example:

rounded = round 3.7 // 4
rounded = round(3.2) // 3

Rounds a number down to the nearest integer.

Syntax:

result = floor number
result = floor(number)

Example:

roundedDown = floor 3.7 // 3
roundedDown = floor(3.9) // 3

Rounds a number up to the nearest integer.

Syntax:

result = ceil number
result = ceil(number)

Example:

roundedUp = ceil 3.2 // 4
roundedUp = ceil(3.1) // 4

Returns the square root of a number.

Syntax:

result = sqrt of number
result = sqrt(number)

Example:

squareRoot = sqrt of 16 // 4
squareRoot = sqrt(25) // 5

Returns the largest value from the given numbers.

Syntax:

result = max(num1, num2, ...)
result = run max with num1 and num2 and num3

Example:

maximum = max(5, 10, 3) // 10
largest = run max with 5 and 10 and 3 // 10

Returns the smallest value from the given numbers.

Syntax:

result = min(num1, num2, ...)
result = run min with num1 and num2 and num3

Example:

minimum = min(5, 10, 3) // 3
smallest = run min with 5 and 10 and 3 // 3

Generates a random floating-point number between 0 and 1.

Syntax:

result = random()
result = run random

Example:

randomNum = random() // e.g., 0.4827
randomNum = run random // e.g., 0.7213

Generates a random integer within a specified range (inclusive).

Syntax:

result = random integer from min to max

Example:

randomInt = random integer from 1 to 10 // e.g., 7
diceRoll = random integer from 1 to 6 // e.g., 4

Common Pseudocode supports the six standard trigonometric functions:

Syntax:

result = sin of angle
result = cos of angle
result = tan of angle
result = csc of angle
result = sec of angle
result = cot of angle

Example:

sine = sin of 30
cosine = cos of 60
tangent = tan of 45

Inverse trigonometric functions are also available:

Syntax:

result = sin^-1 of value
result = cos^-1 of value
result = tan^-1 of value
result = csc^-1 of value
result = sec^-1 of value
result = cot^-1 of value

Example:

arcSine = sin^-1 of 0.5
arcCosine = cos^-1 of 0.5
arcTangent = tan^-1 of 1

Note: For inverse trig functions, use ^ or ** (the to the power of syntax doesn’t work here).


Raise a number to a power:

Syntax:

result = base ^ exponent
result = base ** exponent
result = base to the power of exponent

Example:

power = 2 ^ 3 // 8
power = 2 ** 3 // 8
power = 2 to the power of 3 // 8

Get the remainder after division:

Syntax:

result = number % divisor
result = number modulus divisor
result = remainder after number / divisor

Example:

remainder = 10 % 3 // 1
remainder = 10 modulus 3 // 1
remainder = remainder after 10 / 3 // 1