Numbers

functions and helpers for more advanced math-related operations


Functions

Predicates

Constants


The Numbers module extends Arturo's numerical capabilities beyond basic arithmetic, offering advanced mathematical functions, number theory operations, and trigonometric calculations.

Key Concepts

  • Full support for complex numbers and mathematical constants
  • Advanced mathematical functions (logarithms, exponentials)
  • Comprehensive trigonometric operations
  • Number theory functions (factors, primes)
  • Random number generation

Basic Usage

Mathematical Constants

pi              ; => 3.141592653589793
epsilon         ; => 2.718281828459045
tau             ; => 6.283185307179586

Mathematical Functions

; exponentials and logarithms
exp 1           ; => 2.718281828459045
ln 10           ; => 2.302585092994046
log 100 10      ; => 2.0

; rounding & absolute values
ceil 3.14       ; => 4
floor 3.14      ; => 3
round 3.14      ; => 3.0
abs neg 42      ; => 42

Complex Numbers

z: to :complex [3 4]    ; 3.0+4.0i
print abs z             ; 5.0
print angle z           ; 0.927295218001612

w: to :complex [1 1]    ; 1.0+1.0i
print z * w             ; -1.0+7.0i

Note
Complex numbers are created using to with a block containing the real and imaginary parts.

Number Theory

; prime numbers
prime? 17           ; => true

; factors
factors 12          ; => [1 2 3 4 6 12]
factors.prime 12    ; => [2 2 3]

; GCD and LCM
print gcd [24 36]   ; 12
print lcm [4 6]     ; 12

Common Patterns

Working with Angles

ang: pi/6                       ; 30 degrees

; trigonometric functions
print round.to: 2 sin ang       ; 0.5
print cos ang                   ; 0.866025403784439
print tan ang                   ; 0.577350269189626

; inverse functions
print asin 0.5                  ; 0.523598775598299

Random Number Generation

; random integer in range (inclusive)
print random 1 6    ; dice roll...

; random floating-point numbers
print random 0.0 1.0