Math
Classes
complex
A class representing a complex number, composed of a real part r
and an imaginary part i
.
complex();
complex(const complex&in other);
complex(float r, float i = 0.0);
Arguments (3):
float r: The real part of the complex number.
float i = 0.0: The imaginary part of the complex number. Defaults to 0 if not specified.
Remarks:
The complex class supports basic arithmetic operations (add, subtract, multiply, divide) and can be compared for equality. It also provides access to magnitude (abs()
), and utilities for working with real/imaginary order via ri
and ir
properties.
Example:
void main() {
complex a(2, 3); // 2 + 3i
complex b(1, -1); // 1 - i
complex c = a + b; // (2+1, 3-1) = (3, 2)
alert("result", "real: " + c.r + ", imag: " + c.i);
}
Functions
abs
Returns the absolute value (magnitude) of the complex number.
float complex::abs() const;
Example:
void main() {
complex c(3, 4);
alert("Magnitude =", c.abs());
}
Operators
opAdd
Adds two complex numbers and returns the result.
complex complex::opAdd(const complex&in other) const;
Arguments:
- const complex&in other: the complex number to add.
Returns:
complex: a new complex object containing the value of the addition.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(1, 2);
complex b(3, 4);
complex c = a + b;
alert("a + b =", to_string(c));
}
opAddAssign
Adds another complex number to this one.
complex& complex::opAddAssign(const complex&in other);
Arguments:
- const complex&in other: the complex number to add.
Returns:
complex&: a reference to the complex number being operated on.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(1, 2);
complex b(3, 4);
a += b;
alert("a + b =", to_string(a));
}
opDiv
Divides this complex number by another and returns the result.
complex complex::opDiv(const complex&in other) const;
Arguments:
- const complex&in other: the complex number to divide by.
Returns:
complex: a new complex object containing the value of the division.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(1, 2);
complex b(3, 4);
complex c = a / b;
alert("a / b =", to_string(c));
}
opDivAssign
Divides this complex number by another.
complex& complex::opDivAssign(const complex&in other);
Arguments:
- const complex&in other: the complex number to divide by.
Returns:
complex&: a reference to the complex number being operated on.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(1, 2);
complex b(3, 4);
a /= b;
alert("a / b =", to_string(a));
}
opEquals
Checks if two complex numbers are equal.
bool complex::opEquals(const complex&in other) const;
Arguments:
- const complex&in other: the complex number to compare to.
Returns:
bool: true if the two complex numbers are equal, false otherwise.
Example:
void main() {
complex a(1, 2);
complex b(1, 2);
alert("a equals b?", a == b);
}
opMul
Multiplies two complex numbers and returns the result.
complex complex::opMul(const complex&in other) const;
Arguments:
- const complex&in other: the complex number to multiply with.
Returns:
complex: a new complex object containing the value of the multiplication.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(1, 2);
complex b(3, 4);
complex c = a * b;
alert("a * b =", to_string(c));
}
opMulAssign
Multiplies this complex number by another.
complex& complex::opMulAssign(const complex&in other);
Arguments:
- const complex&in other: the complex number to multiply with.
Returns:
complex&: a reference to the complex number being operated on.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(1, 2);
complex b(3, 4);
a *= b;
alert("a * b =", to_string(a));
}
opSub
Subtracts a complex number from this one and returns the result.
complex complex::opSub(const complex&in other) const;
Arguments:
- const complex&in other: the complex number to subtract.
Returns:
complex: a new complex object containing the value of the subtraction.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(5, 6);
complex b(2, 3);
complex c = a - b;
alert("a - b =", to_string(c));
}
opSubAssign
Subtracts another complex number from this one.
complex& complex::opSubAssign(const complex&in other);
Arguments:
- const complex&in other: the complex number to subtract.
Returns:
complex&: a reference to the complex number being operated on.
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex a(5, 6);
complex b(2, 3);
a -= b;
alert("a - b =", to_string(a));
}
Properties
i
Represents the imaginary component of the complex number.
float complex::i;
Example:
void main() {
complex c(0, 1);
alert("The imaginary part is", c.i);
}
ir
Accesses the complex number in imaginary-real order.
complex complex::ir [property];
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex c(1, 2);
alert("Imaginary-Real =", to_string(c.ir));
}
ri
Accesses the complex number in real-imaginary order.
complex complex::ri;
Example:
// Helper function to print a complex number.
string to_string(const complex&in c) {
return "(" + c.r + ", " + c.i + ")";
}
void main() {
complex c(1, 2);
alert("Real-Imaginary =", to_string(c.ri));
}
vector
A class containing x, y and z coordinates, usually used to represent a 3d point in space.
vector();
vector(const vector& in vec);
vector(float x, float y = 0.0, float z = 0.0);
Arguments (3):
float x: The initial x point or coordinate this vector represents.
float y = 0.0: The initial y point or coordinate this vector represents.
float z = 0.0: The initial z point or coordinate this vector represents, defaulted to 0 because it may not be needed in some 2d applications.
Remarks:
An advantage with vectors is that they contain basic addition and scaling operators.
Example:
void main() {
vector v1(5, 5, 5);
vector v2(10, 10, 0);
vector v3 = v1 + v2;
alert("example", "the new vector is "+v3.x + ", " + v3.y + ", " + v3.z); // will show that the vector is 15, 15, 5.
}
Operators
opAssign
Overloads the = operator, allowing you to assign a vector to another, already existing one.
vector& opAssign(const vector &in other);
Arguments:
- const vector&in other: the vector to assign to.
Example:
void main() {
vector v1;
vector v2(1.2, 2.3, 9.3);
v1 = v2;
alert("The first vector =", "(" + round(v1.x, 2) + ", " + round(v1.y, 2) + ", " + round(v1.z, 2) + ")");
}
Properties
x
Represents the x coordinate of the vector.
float vector::x const;
Example:
void main() {
vector v(2.9);
alert("The x value of the vector is", round(v.x, 2));
}
y
Represents the y coordinate of the vector.
float vector::y const;
Example:
void main() {
vector v(2.9, 19.2);
alert("The y value of the vector is", round(v.y, 2));
}
z
Represents the z coordinate of the vector.
float vector::z const;
Example:
void main() {
vector v(2.9, 19.2, 4.1);
alert("The z value of the vector is", round(v.z, 2));
}
Functions
abs
Returns the absolute value of a value.
double abs(double x);
Arguments:
- double x: The value whose absolute value is to be calculated.
Returns:
double: the absolute value of the given value.
Example:
void main() {
alert("Example", "The absolute value of -5 is " + abs(-5));
}
acos
Returns the arc cosine of a value in radians.
double acos(double x);
Arguments:
- double x: The value whose arc cosine is to be calculated.
Returns:
double: the arc cosine of the given value.
Example:
void main() {
alert("Example", "The arc cosine of 0.5 is " + acos(0.5) + " radians");
}
asin
Returns the arc sine of a value in radians.
double asin(double x);
Arguments:
- double x: The value whose arc sine is to be calculated.
Returns:
double: the arc sine of the given value.
Example:
void main() {
alert("Example", "The arc sine of 0.5 is " + asin(0.5) + " radians");
}
atan
Returns the arc tangent of a value in radians.
double atan(double x);
Arguments:
- double x: The value whose arc tangent is to be calculated.
Returns:
double: the arc tangent of the given value.
Example:
void main() {
alert("Example", "The arc tangent of 1 is " + atan(1) + " radians");
}
atan2
Returns the arc tangent of y/x, where y and x are the coordinates of a point.
double atan2(double y, double x);
Arguments:
double y: The ordinate coordinate.
double x: The abscissa coordinate.
Returns:
double: the arc tangent of y/x.
Example:
void main() {
alert("Example", "The arc tangent of (1, 2) is " + atan2(1, 2) + " radians");
}
ceil
Returns the smallest integer greater than or equal to a value.
double ceil(double x);
Arguments:
- double x: The value whose ceiling is to be calculated.
Returns:
double: the smallest integer greater than or equal to x.
Example:
void main() {
alert("Example", "The ceiling of 3.14 is " + ceil(3.14));
}
cos
Returns the cosine of an angle given in radians.
double cos(double x);
Arguments:
- double x: The angle (in radians) to get the cosine of.
Returns:
double: the cosine of the angle.
Example:
void main() {
alert("Example", "The cosine of 45 is " + cos(45 * 3.14159 / 180) + " radians");
}
cosh
Returns the hyperbolic cosine of a value.
double cosh(double x);
Arguments:
- double x: The value whose hyperbolic cosine is to be calculated.
Returns:
double: the hyperbolic cosine of the given value.
Example:
void main() {
alert("Example", "The hyperbolic cosine of 2 is " + cosh(2));
}
floor
Returns the largest integer less than or equal to a value.
double floor(double x);
Arguments:
- double x: The value whose floor is to be calculated.
Returns:
double: the largest integer less than or equal to x.
Example:
void main() {
alert("Example", "The floor of 3.14 is " + floor(3.14));
}
fraction
Returns the fractional part of a value.
double fraction(double x);
Arguments:
- double x: The value whose fractional part is to be extracted.
Returns:
double: the fractional part of the given value.
Example:
void main() {
alert("Example", "The fractional part of 3.75 is " + fraction(3.75));
}
log
Returns the natural logarithm (base e) of a value.
double log(double x);
Arguments:
- double x: The value whose natural logarithm is to be calculated.
Returns:
double: the natural logarithm of the given value.
Example:
void main() {
alert("Example", "The natural logarithm of 10 is " + log(10));
}
log10
Returns the base 10 logarithm of a value.
double log10(double x);
Arguments:
- double x: The value whose base 10 logarithm is to be calculated.
Returns:
double: the base 10 logarithm of the given value.
Example:
void main() {
alert("Example", "The base 10 logarithm of 100 is " + log10(100));
}
pow
Returns x raised to the power of y.
double pow(double x, double y);
Arguments:
double x: The base.
double y: The exponent.
Returns:
double: x raised to the power of y.
Example:
void main() {
alert("Example", "2 raised to the power of 3 is " + pow(2, 3));
}
round
Returns the value of a number rounded to the nearest integer.
double round(double n, int p);
Arguments:
double n: The number to be rounded.
int p: The number of decimal places to round to.
Returns:
double: The value of the number rounded to the nearest integer.
Example:
void main() {
alert("Example", "Rounding 3.14159 to 2 decimal places gives " + round(3.14159, 2));
}
sin
Returns the sine of an angle given in radians.
double sin(double x);
Arguments:
- double x: The angle (in radians) to get the sine of.
Returns:
double: the sine of the angle.
Example:
void main() {
alert("Example", "The sine of 45 is " + sin(45 * 3.14159 / 180) + " radians");
}
sinh
Returns the hyperbolic sine of a value.
double sinh(double x);
Arguments:
- double x: The value whose hyperbolic sine is to be calculated.
Returns:
double: the hyperbolic sine of the given value.
Example:
void main() {
alert("Example", "The hyperbolic sine of 2 is " + sinh(2));
}
sqrt
Returns the square root of a value.
double sqrt(double x);
Arguments:
- double x: The value whose square root is to be calculated.
Returns:
double: the square root of the given value.
Example:
void main() {
alert("Example", "The square root of 16 is " + sqrt(16));
}
tan
Returns the tangent of an angle given in radians.
double tan(double x);
Arguments:
- double x: The angle (in radians) to get the tangent of.
Returns:
double: the tangent of the angle.
Example:
void main() {
alert("Example", "The tangent of 45 is " + tan(45 * 3.14159 / 180) + " radians");
}
tanh
Returns the hyperbolic tangent of a value.
double tanh(double x);
Arguments:
- double x: The value whose hyperbolic tangent is to be calculated.
Returns:
double: the hyperbolic tangent of the given value.
Example:
void main() {
alert("Example", "The hyperbolic tangent of 2 is " + tanh(2));
}
tinyexpr
Evaluate a mathematical expression using the tinyexpr library.
double tinyexpr(const string&in expression);
Arguments:
- const string&in expression: the expression to evaluate.
Returns:
double: the result of the expression.
Example:
void main() {
string expression = input_box("Expression", "Enter expression to evaluate");
if (expression.is_empty()) exit();
alert("Result", expression + "= " + tinyexpr(expression));
}