Math Module
The Language::Math module provides mathematical constants, functions, and random number generation.
xxml
#import Language::Math;Constants
Math::PI3.14159265358979...
Math::E2.71828182845904...
Math::TAU6.28318530717958...
Basic Functions
| Method | Parameters | Returns | Description |
|---|---|---|---|
| abs | value: Double^ | Double^ | Absolute value |
| ceil | value: Double^ | Double^ | Round up to nearest integer |
| floor | value: Double^ | Double^ | Round down to nearest integer |
| round | value: Double^ | Double^ | Round to nearest integer |
| min | a: Double^, b: Double^ | Double^ | Minimum of two values |
| max | a: Double^, b: Double^ | Double^ | Maximum of two values |
| clamp | val: Double^, min: Double^, max: Double^ | Double^ | Clamp value to range |
xxml
Instantiate Double^ As <x> = Double::Constructor(-5.7);
Instantiate Double^ As <absX> = Math::Math::abs(x); // 5.7
Instantiate Double^ As <ceil> = Math::Math::ceil(x); // -5.0
Instantiate Double^ As <floor> = Math::Math::floor(x); // -6.0
Instantiate Double^ As <round> = Math::Math::round(x); // -6.0
// Clamp value between 0 and 100
Instantiate Double^ As <value> = Double::Constructor(150.0);
Instantiate Double^ As <clamped> = Math::Math::clamp(
value,
Double::Constructor(0.0),
Double::Constructor(100.0)
); // 100.0Power and Roots
| Method | Parameters | Returns | Description |
|---|---|---|---|
| pow | base: Double^, exp: Double^ | Double^ | Raise base to exponent |
| sqrt | value: Double^ | Double^ | Square root |
| cbrt | value: Double^ | Double^ | Cube root |
| exp | value: Double^ | Double^ | e raised to power |
xxml
Instantiate Double^ As <squared> = Math::Math::pow(
Double::Constructor(2.0),
Double::Constructor(10.0)
); // 1024.0
Instantiate Double^ As <root> = Math::Math::sqrt(Double::Constructor(16.0)); // 4.0
Instantiate Double^ As <cubeRoot> = Math::Math::cbrt(Double::Constructor(27.0)); // 3.0Logarithms
| Method | Parameters | Returns | Description |
|---|---|---|---|
| log | value: Double^ | Double^ | Natural logarithm (ln) |
| log10 | value: Double^ | Double^ | Base-10 logarithm |
| log2 | value: Double^ | Double^ | Base-2 logarithm |
Trigonometry
| Method | Parameters | Returns | Description |
|---|---|---|---|
| sin | radians: Double^ | Double^ | Sine |
| cos | radians: Double^ | Double^ | Cosine |
| tan | radians: Double^ | Double^ | Tangent |
| asin | value: Double^ | Double^ | Arc sine |
| acos | value: Double^ | Double^ | Arc cosine |
| atan | value: Double^ | Double^ | Arc tangent |
| atan2 | y: Double^, x: Double^ | Double^ | Two-argument arc tangent |
Hyperbolic Functions
| Method | Parameters | Returns | Description |
|---|---|---|---|
| sinh | value: Double^ | Double^ | Hyperbolic sine |
| cosh | value: Double^ | Double^ | Hyperbolic cosine |
| tanh | value: Double^ | Double^ | Hyperbolic tangent |
Angle Conversion
| Method | Parameters | Returns | Description |
|---|---|---|---|
| toRadians | degrees: Double^ | Double^ | Convert degrees to radians |
| toDegrees | radians: Double^ | Double^ | Convert radians to degrees |
xxml
// Calculate sine of 45 degrees
Instantiate Double^ As <degrees> = Double::Constructor(45.0);
Instantiate Double^ As <radians> = Math::Math::toRadians(degrees);
Instantiate Double^ As <sineValue> = Math::Math::sin(radians);
Run System::Console::printLine(
String::Constructor("sin(45) = ").append(sineValue.toString())
);Random Numbers
| Method | Parameters | Returns | Description |
|---|---|---|---|
| random | max: Integer^ | Integer^ | Random integer in [0, max) |
| randomRange | min: Integer^, max: Integer^ | Integer^ | Random integer in [min, max) |
| seed | value: Integer^ | None | Seed the random generator |
xxml
// Seed with current time for randomness
Run Math::Math::seed(System::Console::getTimeMillis());
// Random number 0-99
Instantiate Integer^ As <r1> = Math::Math::random(Integer::Constructor(100));
// Random number 1-6 (dice roll)
Instantiate Integer^ As <dice> = Math::Math::randomRange(
Integer::Constructor(1),
Integer::Constructor(7)
);
Run System::Console::printLine(
String::Constructor("Dice roll: ").append(dice.toString())
);Note
Seed the random number generator at program start using
Math::Math::seed() for better randomness. Using the current time in milliseconds is a common approach.Complete Example
geometry.xxml
1 #import Language::Core; 2 #import Language::Math; 3 #import Language::System; 4 5 [ Entrypoint 6 { 7 // Calculate circle properties 8 Instantiate Double^ As <radius> = Double::Constructor(5.0); 9 10 Instantiate Double^ As <area> = Math::Math::PI.multiply( 11 Math::Math::pow(radius, Double::Constructor(2.0)) 12 ); 13 14 Instantiate Double^ As <circumference> = Math::Math::TAU.multiply(radius); 15 16 Run System::Console::printLine(String::Constructor("Circle with radius 5:")); 17 Run System::Console::printLine( 18 String::Constructor(" Area: ").append(area.toString()) 19 ); 20 Run System::Console::printLine( 21 String::Constructor(" Circumference: ").append(circumference.toString()) 22 ); 23 24 // Pythagorean theorem 25 Instantiate Double^ As <a> = Double::Constructor(3.0); 26 Instantiate Double^ As <b> = Double::Constructor(4.0); 27 Instantiate Double^ As <c> = Math::Math::sqrt( 28 Math::Math::pow(a, Double::Constructor(2.0)).add( 29 Math::Math::pow(b, Double::Constructor(2.0)) 30 ) 31 ); 32 33 Run System::Console::printLine(String::Constructor("")); 34 Run System::Console::printLine( 35 String::Constructor("Right triangle (3, 4): hypotenuse = ").append(c.toString()) 36 ); 37 38 // Random dice simulation 39 Run Math::Math::seed(System::Console::getTimeMillis()); 40 Run System::Console::printLine(String::Constructor("")); 41 Run System::Console::printLine(String::Constructor("Rolling 5 dice:")); 42 43 Instantiate Integer^ As <sum> = Integer::Constructor(0); 44 For (Instantiate Integer^ As <i> = Integer::Constructor(0); 45 i.lessThan(Integer::Constructor(5)).toBool(); 46 Set i = i.add(Integer::Constructor(1))) 47 { 48 Instantiate Integer^ As <roll> = Math::Math::randomRange( 49 Integer::Constructor(1), 50 Integer::Constructor(7) 51 ); 52 Set sum = sum.add(roll); 53 Run System::Console::printLine( 54 String::Constructor(" Roll ").append(i.add(Integer::Constructor(1)).toString()) 55 .append(String::Constructor(": ")).append(roll.toString()) 56 ); 57 } 58 Run System::Console::printLine( 59 String::Constructor(" Total: ").append(sum.toString()) 60 ); 61 62 Exit(0); 63 } 64 ]