The Math module provides comprehensive mathematical operations, constants, and utility functions for Soul. It includes basic arithmetic operations, trigonometric functions, logarithmic functions, statistical operations, and random number generation capabilities.
Generates a random number between 0 and 1 (exclusive):
Copy
Ask AI
// Random number between 0 and 1randomNum = Math.random()println(randomNum) // e.g., 0.7234567890123456// Random number between 0 and maxrandomNum = Math.random(10)println(randomNum) // e.g., 7.234567890123456// Random number between min and maxrandomNum = Math.random(5, 15)println(randomNum) // e.g., 12.234567890123456
// Set a specific seed for reproducible resultsMath.seed(12345)random1 = Math.random()random2 = Math.random()// Reset with same seed - will produce same sequenceMath.seed(12345)random3 = Math.random()random4 = Math.random()println(random1 == random3) // trueprintln(random2 == random4) // true// Access current seedcurrentSeed = Math.seedprintln(currentSeed) // 12345
Handle edge cases: Always check for invalid inputs (negative numbers for sqrt, zero for division)
Use appropriate precision: Be aware of floating-point precision limitations
Choose the right function: Use appropriate mathematical functions for your use case
Angle units: Remember that trigonometric functions use radians, not degrees
Random seeding: Set seeds for reproducible random sequences when needed
Copy
Ask AI
// Good - handle edge casessoul safeDivision(a, b) { if (Math.isZero(b)) { return null // or throw error } return a / b}// Good - use constantscircumference = 2 * Math.PI * radius // Use Math.PI instead of 3.14159// Good - convert degrees to radiansangleInRadians = angleInDegrees * Math.PI / 180sineValue = Math.sin(angleInRadians)// Good - set seed for reproducible resultsMath.seed(42)randomSequence = [Math.random(), Math.random(), Math.random()]// Good - clamp values to valid rangesnormalizedValue = Math.clamp(inputValue, 0, 1)
The Math module provides a comprehensive set of mathematical functions and constants for Soul, enabling complex mathematical computations, statistical analysis, and geometric calculations with precision and reliability.