sigildocs

(sigil math)

(sigil math) - Mathematical Functions

Extended mathematical operations including trigonometry, exponentiation, rounding, and bitwise operations. Basic arithmetic (+, -, *, /, <, >, etc.) remains in (sigil core).

Trigonometry

(import (sigil math))

(sin 0)              ; => 0.0
(cos 0)              ; => 1.0
(atan 1 1)           ; => 0.785... (π/4)

Rounding

(floor 3.7)     ; => 3.0
(ceiling 3.2)   ; => 4.0
(round 3.5)     ; => 4.0
(truncate 3.7)  ; => 3.0

Bitwise Operations

(bitwise-and 12 10)     ; => 8
(bitwise-ior 12 10)     ; => 14
(arithmetic-shift 1 4)  ; => 16

Exports

maxprocedure

Return the maximum of the arguments.

(max 1 2 3)     ; => 3
(max -1 -2 -3)  ; => -1
minprocedure

Return the minimum of the arguments.

(min 1 2 3)     ; => 1
(min -1 -2 -3)  ; => -3
floorprocedure

Round toward negative infinity.

(floor 3.7)   ; => 3.0
(floor -3.7)  ; => -4.0
(floor 3)     ; => 3
ceilingprocedure

Round toward positive infinity.

(ceiling 3.2)   ; => 4.0
(ceiling -3.2)  ; => -3.0
(ceiling 3)     ; => 3
truncateprocedure

Round toward zero.

(truncate 3.7)   ; => 3.0
(truncate -3.7)  ; => -3.0
roundprocedure

Round to nearest integer (half to even).

(round 3.5)   ; => 4.0
(round 2.5)   ; => 2.0  (banker's rounding)
(round 3.2)   ; => 3.0
exptprocedure

Compute base raised to the power of exponent.

(expt 2 10)   ; => 1024
(expt 2 0.5)  ; => 1.414...
(expt 2 -1)   ; => 0.5
sqrtprocedure

Compute the square root.

(sqrt 4)    ; => 2.0
(sqrt 2)    ; => 1.414...
squareprocedure

Compute the square of a number (x * x).

(square 3)    ; => 9
(square -4)   ; => 16
expprocedure

Compute e raised to the given power.

(exp 0)   ; => 1.0
(exp 1)   ; => 2.718...
logprocedure

Compute the natural logarithm, or logarithm with given base.

(log 1)       ; => 0.0
(log 2.718)   ; => ~1.0
(log 8 2)     ; => 3.0  (log base 2 of 8)
sinprocedure

Compute the sine (argument in radians).

(sin 0)              ; => 0.0
(sin (/ 3.14159 2))  ; => ~1.0
cosprocedure

Compute the cosine (argument in radians).

(cos 0)         ; => 1.0
(cos 3.14159)   ; => ~-1.0
tanprocedure

Compute the tangent (argument in radians).

(tan 0)   ; => 0.0
asinprocedure

Compute the arc sine (result in radians).

(asin 0)   ; => 0.0
(asin 1)   ; => ~1.57  (π/2)
acosprocedure

Compute the arc cosine (result in radians).

(acos 1)   ; => 0.0
(acos 0)   ; => ~1.57  (π/2)
atanprocedure

Compute the arc tangent (result in radians).

With one argument, returns atan(x). With two arguments, returns atan(y/x) with correct quadrant.

(atan 1)      ; => ~0.785  (π/4)
(atan 1 1)    ; => ~0.785  (π/4)
(atan 1 -1)   ; => ~2.356  (3π/4)
exact?procedure

Test if a number is exact (integer or exact rational).

(exact? 42)    ; => #t
(exact? 3.14)  ; => #f
inexact?procedure

Test if a number is inexact (floating-point).

(inexact? 3.14)  ; => #t
(inexact? 42)    ; => #f
exactprocedure

Convert a number to exact representation.

(exact 3.0)   ; => 3
(exact 3)     ; => 3
inexactprocedure

Convert a number to inexact (floating-point) representation.

(inexact 3)     ; => 3.0
(inexact 3.14)  ; => 3.14
gcdprocedure

Compute the greatest common divisor.

(gcd 12 8)      ; => 4
(gcd 12 8 6)    ; => 2
(gcd)           ; => 0
lcmprocedure

Compute the least common multiple.

(lcm 3 4)       ; => 12
(lcm 3 4 5)     ; => 60
(lcm)           ; => 1
quotientprocedure

Integer division, truncating toward zero.

(quotient 13 4)   ; => 3
(quotient -13 4)  ; => -3
bitwise-notprocedure

Return the bitwise complement (NOT) of an exact integer.

(bitwise-not 0)    ; => -1
(bitwise-not -1)   ; => 0
(bitwise-not 5)    ; => -6
bitwise-andprocedure

Return the bitwise AND of zero or more exact integers.

With no arguments, returns -1 (all bits set).

(bitwise-and 12 10)     ; => 8
(bitwise-and 255 15 7)  ; => 7
(bitwise-and)           ; => -1
bitwise-iorprocedure

Return the bitwise inclusive OR of zero or more exact integers.

With no arguments, returns 0.

(bitwise-ior 12 10)  ; => 14
(bitwise-ior 1 2 4)  ; => 7
(bitwise-ior)        ; => 0
bitwise-xorprocedure

Return the bitwise exclusive OR (XOR) of zero or more exact integers.

With no arguments, returns 0.

(bitwise-xor 12 10)  ; => 6
(bitwise-xor 1 3 7)  ; => 5
(bitwise-xor)        ; => 0

Shift an exact integer left (positive count) or right (negative count).

(arithmetic-shift 1 4)    ; => 16  (shift left 4)
(arithmetic-shift 16 -2)  ; => 4   (shift right 2)