Math Functions
Common Pseudocode provides a variety of built-in mathematical functions for common operations.
Basic Math Functions
Section titled “Basic Math Functions”abs - Absolute Value
Section titled “abs - Absolute Value”Returns the absolute value of a number.
Syntax:
result = abs of numberresult = abs(number)Example:
absolute = abs of -5 // 5positive = abs(-10) // 10round - Round to Nearest Integer
Section titled “round - Round to Nearest Integer”Rounds a number to the nearest integer.
Syntax:
result = round numberresult = round(number)Example:
rounded = round 3.7 // 4rounded = round(3.2) // 3floor - Round Down
Section titled “floor - Round Down”Rounds a number down to the nearest integer.
Syntax:
result = floor numberresult = floor(number)Example:
roundedDown = floor 3.7 // 3roundedDown = floor(3.9) // 3ceil - Round Up
Section titled “ceil - Round Up”Rounds a number up to the nearest integer.
Syntax:
result = ceil numberresult = ceil(number)Example:
roundedUp = ceil 3.2 // 4roundedUp = ceil(3.1) // 4sqrt - Square Root
Section titled “sqrt - Square Root”Returns the square root of a number.
Syntax:
result = sqrt of numberresult = sqrt(number)Example:
squareRoot = sqrt of 16 // 4squareRoot = sqrt(25) // 5Min/Max Functions
Section titled “Min/Max Functions”max - Maximum Value
Section titled “max - Maximum Value”Returns the largest value from the given numbers.
Syntax:
result = max(num1, num2, ...)result = run max with num1 and num2 and num3Example:
maximum = max(5, 10, 3) // 10largest = run max with 5 and 10 and 3 // 10min - Minimum Value
Section titled “min - Minimum Value”Returns the smallest value from the given numbers.
Syntax:
result = min(num1, num2, ...)result = run min with num1 and num2 and num3Example:
minimum = min(5, 10, 3) // 3smallest = run min with 5 and 10 and 3 // 3Random Number Functions
Section titled “Random Number Functions”random - Random Float
Section titled “random - Random Float”Generates a random floating-point number between 0 and 1.
Syntax:
result = random()result = run randomExample:
randomNum = random() // e.g., 0.4827randomNum = run random // e.g., 0.7213random integer - Random Integer in Range
Section titled “random integer - Random Integer in Range”Generates a random integer within a specified range (inclusive).
Syntax:
result = random integer from min to maxExample:
randomInt = random integer from 1 to 10 // e.g., 7diceRoll = random integer from 1 to 6 // e.g., 4Trigonometric Functions
Section titled “Trigonometric Functions”Basic Trig Functions
Section titled “Basic Trig Functions”Common Pseudocode supports the six standard trigonometric functions:
Syntax:
result = sin of angleresult = cos of angleresult = tan of angleresult = csc of angleresult = sec of angleresult = cot of angleExample:
sine = sin of 30cosine = cos of 60tangent = tan of 45Inverse Trig Functions
Section titled “Inverse Trig Functions”Inverse trigonometric functions are also available:
Syntax:
result = sin^-1 of valueresult = cos^-1 of valueresult = tan^-1 of valueresult = csc^-1 of valueresult = sec^-1 of valueresult = cot^-1 of valueExample:
arcSine = sin^-1 of 0.5arcCosine = cos^-1 of 0.5arcTangent = tan^-1 of 1Note: For inverse trig functions, use ^ or ** (the to the power of syntax doesn’t work here).
Power and Modulus
Section titled “Power and Modulus”Exponentiation
Section titled “Exponentiation”Raise a number to a power:
Syntax:
result = base ^ exponentresult = base ** exponentresult = base to the power of exponentExample:
power = 2 ^ 3 // 8power = 2 ** 3 // 8power = 2 to the power of 3 // 8Modulus
Section titled “Modulus”Get the remainder after division:
Syntax:
result = number % divisorresult = number modulus divisorresult = remainder after number / divisorExample:
remainder = 10 % 3 // 1remainder = 10 modulus 3 // 1remainder = remainder after 10 / 3 // 1