|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "math" for Math
|
|
|
|
System.print("=== Math Module Demo ===\n")
|
|
|
|
System.print("--- Constants ---")
|
|
System.print("pi: %(Math.pi)")
|
|
System.print("e: %(Math.e)")
|
|
System.print("tau: %(Math.tau)")
|
|
System.print("infinity: %(Math.infinity)")
|
|
System.print("nan: %(Math.nan)")
|
|
|
|
System.print("\n--- Trigonometric Functions ---")
|
|
var angle = Math.pi / 4
|
|
System.print("Angle: pi/4 radians (45 degrees)")
|
|
System.print("sin(pi/4): %(Math.sin(angle))")
|
|
System.print("cos(pi/4): %(Math.cos(angle))")
|
|
System.print("tan(pi/4): %(Math.tan(angle))")
|
|
|
|
System.print("\n--- Inverse Trigonometric Functions ---")
|
|
System.print("asin(0.5): %(Math.asin(0.5)) radians")
|
|
System.print("acos(0.5): %(Math.acos(0.5)) radians")
|
|
System.print("atan(1): %(Math.atan(1)) radians")
|
|
System.print("atan2(1, 1): %(Math.atan2(1, 1)) radians")
|
|
|
|
System.print("\n--- Hyperbolic Functions ---")
|
|
System.print("sinh(1): %(Math.sinh(1))")
|
|
System.print("cosh(1): %(Math.cosh(1))")
|
|
System.print("tanh(1): %(Math.tanh(1))")
|
|
|
|
System.print("\n--- Logarithmic Functions ---")
|
|
System.print("log(e): %(Math.log(Math.e))")
|
|
System.print("log10(100): %(Math.log10(100))")
|
|
System.print("log2(8): %(Math.log2(8))")
|
|
System.print("exp(1): %(Math.exp(1))")
|
|
|
|
System.print("\n--- Power and Roots ---")
|
|
System.print("pow(2, 10): %(Math.pow(2, 10))")
|
|
System.print("sqrt(16): %(Math.sqrt(16))")
|
|
System.print("cbrt(27): %(Math.cbrt(27))")
|
|
|
|
System.print("\n--- Rounding Functions ---")
|
|
System.print("ceil(4.3): %(Math.ceil(4.3))")
|
|
System.print("floor(4.7): %(Math.floor(4.7))")
|
|
System.print("round(4.5): %(Math.round(4.5))")
|
|
System.print("abs(-42): %(Math.abs(-42))")
|
|
|
|
System.print("\n--- Min, Max, Clamp ---")
|
|
System.print("min(5, 3): %(Math.min(5, 3))")
|
|
System.print("max(5, 3): %(Math.max(5, 3))")
|
|
System.print("clamp(15, 0, 10): %(Math.clamp(15, 0, 10))")
|
|
System.print("clamp(5, 0, 10): %(Math.clamp(5, 0, 10))")
|
|
System.print("clamp(-5, 0, 10): %(Math.clamp(-5, 0, 10))")
|
|
|
|
System.print("\n--- Sign Function ---")
|
|
System.print("sign(42): %(Math.sign(42))")
|
|
System.print("sign(-42): %(Math.sign(-42))")
|
|
System.print("sign(0): %(Math.sign(0))")
|
|
|
|
System.print("\n--- Linear Interpolation ---")
|
|
System.print("lerp(0, 100, 0.0): %(Math.lerp(0, 100, 0))")
|
|
System.print("lerp(0, 100, 0.5): %(Math.lerp(0, 100, 0.5))")
|
|
System.print("lerp(0, 100, 1.0): %(Math.lerp(0, 100, 1))")
|
|
|
|
System.print("\n--- Angle Conversion ---")
|
|
System.print("degrees(pi): %(Math.degrees(Math.pi))")
|
|
System.print("degrees(pi/2): %(Math.degrees(Math.pi / 2))")
|
|
System.print("radians(180): %(Math.radians(180))")
|
|
System.print("radians(90): %(Math.radians(90))")
|
|
|
|
System.print("\n--- Practical Example: Distance Calculation ---")
|
|
var x1 = 0
|
|
var y1 = 0
|
|
var x2 = 3
|
|
var y2 = 4
|
|
var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2))
|
|
System.print("Distance from (%(x1),%(y1)) to (%(x2),%(y2)): %(distance)")
|