Math Module

The Language::Math module provides mathematical constants, functions, and random number generation.

xxml
#import Language::Math;

Constants

Math::PI

3.14159265358979...

Math::E

2.71828182845904...

Math::TAU

6.28318530717958...

Basic Functions

MethodParametersReturnsDescription
absvalue: Double^Double^Absolute value
ceilvalue: Double^Double^Round up to nearest integer
floorvalue: Double^Double^Round down to nearest integer
roundvalue: Double^Double^Round to nearest integer
mina: Double^, b: Double^Double^Minimum of two values
maxa: Double^, b: Double^Double^Maximum of two values
clampval: 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.0

Power and Roots

MethodParametersReturnsDescription
powbase: Double^, exp: Double^Double^Raise base to exponent
sqrtvalue: Double^Double^Square root
cbrtvalue: Double^Double^Cube root
expvalue: 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.0

Logarithms

MethodParametersReturnsDescription
logvalue: Double^Double^Natural logarithm (ln)
log10value: Double^Double^Base-10 logarithm
log2value: Double^Double^Base-2 logarithm

Trigonometry

MethodParametersReturnsDescription
sinradians: Double^Double^Sine
cosradians: Double^Double^Cosine
tanradians: Double^Double^Tangent
asinvalue: Double^Double^Arc sine
acosvalue: Double^Double^Arc cosine
atanvalue: Double^Double^Arc tangent
atan2y: Double^, x: Double^Double^Two-argument arc tangent

Hyperbolic Functions

MethodParametersReturnsDescription
sinhvalue: Double^Double^Hyperbolic sine
coshvalue: Double^Double^Hyperbolic cosine
tanhvalue: Double^Double^Hyperbolic tangent

Angle Conversion

MethodParametersReturnsDescription
toRadiansdegrees: Double^Double^Convert degrees to radians
toDegreesradians: 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

MethodParametersReturnsDescription
randommax: Integer^Integer^Random integer in [0, max)
randomRangemin: Integer^, max: Integer^Integer^Random integer in [min, max)
seedvalue: Integer^NoneSeed 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 usingMath::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]

See Also