Skip to content

Math Operations

wraith4081 edited this page Aug 16, 2023 · 2 revisions

Math Operations in WraithScript

In WraithScript, the Math object provides a wide range of mathematical operations and constants. These operations allow you to perform common mathematical calculations conveniently. This documentation page outlines some of the key math operations available in WraithScript.

Table of Contents

Basic Arithmetic

The Math object in WraithScript supports basic arithmetic operations:

  • Addition: result = x + y
  • Subtraction: result = x - y
  • Multiplication: result = x * y
  • Division: result = x / y
  • Modulus (Remainder): result = x % y

Exponents and Roots

You can perform exponentiation and calculate square roots using the Math object:

  • Exponentiation: result = pow(base, exponent)
  • Square Root: result = sqrt(number)

Trigonometric Functions

WraithScript provides various trigonometric functions in the Math object for working with angles:

  • Sine: result = sin(angle)
  • Cosine: result = cos(angle)
  • Tangent: result = tan(angle)
  • Arcsine: result = asin(value)
  • Arccosine: result = acos(value)
  • Arctangent: result = atan(value)

Constants

The Math object also includes several useful mathematical constants:

  • Pi (π): PI
  • Euler's number (e): Euler

Example

let radius = 5;
let circumference = 2 * PI * radius;

let angle = PI / 4;
letsineValue = sin(angle);

print(circumference, sineValue);

Basic Arithmetic

The Math object in WraithScript supports basic arithmetic operations:

  • floor: Rounds a number down to the nearest integer less than or equal to the given number.
var result = floor(5.7); // Result: 5
  • ceil: Rounds a number up to the nearest integer greater than or equal to the given number.
var result = ceil(5.2); // Result: 6
  • round: Rounds a number to the nearest integer.
var result = round(5.5); // Result: 6
  • abs: Returns the absolute value of a number.
var result = abs(-5); // Result: 5

Exponents and Roots

You can perform exponentiation and calculate square roots using the Math object:

  • pow: Raises a base number to an exponent.
var result = pow(2, 3); // Result: 8
  • sqrt: Returns the square root of a number.
var result = sqrt(25); // Result: 5

Trigonometric Functions

WraithScript provides various trigonometric functions in the Math object for working with angles:

  • sin: Returns the sine of a number, where the input is in radians.
var angle = PI / 6;
var result = sin(angle); // Result: 0.5
  • cos: Returns the cosine of a number, where the input is in radians.
var angle = PI / 3;
var result = cos(angle); // Result: 0.5
  • tan: Returns the tangent of a number, where the input is in radians.
var angle = PI / 4;
var result = tan(angle); // Result: 1

All Global Math Functions

markdown Copy code

  • floor: Rounds a number down to the nearest integer less than or equal to the given number.
  • ceil: Rounds a number up to the nearest integer greater than or equal to the given number.
  • round: Rounds a number to the nearest integer, rounding to the nearest even integer in case of a tie.
  • abs: Returns the absolute value of a number, which is the distance of the number from zero.
  • acos: Returns the arc cosine (inverse cosine) of a number, in radians.
  • acosh: Returns the hyperbolic arc cosine of a number.
  • asin: Returns the arc sine (inverse sine) of a number, in radians.
  • asinh: Returns the hyperbolic arc sine of a number.
  • atan: Returns the arc tangent (inverse tangent) of a number, in radians.
  • atanh: Returns the hyperbolic arc tangent of a number.
  • cbrt: Returns the cube root of a number.
  • clz32: Returns the number of leading zero bits in the 32-bit binary representation of a number.
  • cos: Returns the cosine of a number, where the input is in radians.
  • cosh: Returns the hyperbolic cosine of a number.
  • exp: Returns the value of Euler's number raised to the power of a given number.
  • expm1: Returns e^x - 1, where e is Euler's number and x is the input.
  • fround: Rounds a number to the nearest single precision float (32-bit) representation.
  • log: Returns the natural logarithm (base e) of a number.
  • log10: Returns the base 10 logarithm of a number.
  • log1p: Returns the natural logarithm of 1 + x, where x is the input.
  • log2: Returns the base 2 logarithm of a number.
  • hypot: Calculates the square root of the sum of squares of its arguments, used to find the length of the hypotenuse of a right triangle.
  • sign: Returns the sign of a number (1 if positive, -1 if negative, 0 if zero).
  • sin: Returns the sine of a number, where the input is in radians.
  • sinh: Returns the hyperbolic sine of a number.
  • sqrt: Returns the square root of a number.
  • tan: Returns the tangent of a number, where the input is in radians.
  • tanh: Returns the hyperbolic tangent of a number.
  • trunc: Removes the decimal fraction of a number and returns the integer part.
  • po2: Raises 2 to the power of the given exponent.
  • imul: Performs a 32-bit integer multiplication.
  • pow: Raises a base number to an exponent.
  • max: Returns the maximum value among a set of numbers.
  • min: Returns the minimum value among a set of numbers.
  • random: Generates a random floating-point number between 0 (inclusive) and 1 (exclusive).
  • listmax: Returns the maximum value from a list of numbers.
  • listmin: Returns the minimum value from a list of numbers.
  • sum: Calculates the sum of a list of numbers.
  • avg: Calculates the average of a list of numbers.

All Global Math Constants

  • Euler: Euler's number (e)
  • LN10: Natural logarithm of 10
  • LN2: Natural logarithm of 2
  • LOG10E: Base 10 logarithm of Euler's number (e)
  • LOG2E: Base 2 logarithm of Euler's number (e)
  • PI: Pi (π)
  • SQRT1_2: Square root of 1/2
  • SQRT2: Square root of 2