(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.0Bitwise Operations
(bitwise-and 12 10) ; => 8
(bitwise-ior 12 10) ; => 14
(arithmetic-shift 1 4) ; => 16Exports
maxprocedureReturn the maximum of the arguments.
(max 1 2 3) ; => 3
(max -1 -2 -3) ; => -1minprocedureReturn the minimum of the arguments.
(min 1 2 3) ; => 1
(min -1 -2 -3) ; => -3floorprocedureRound toward negative infinity.
(floor 3.7) ; => 3.0
(floor -3.7) ; => -4.0
(floor 3) ; => 3ceilingprocedureRound toward positive infinity.
(ceiling 3.2) ; => 4.0
(ceiling -3.2) ; => -3.0
(ceiling 3) ; => 3truncateprocedureRound toward zero.
(truncate 3.7) ; => 3.0
(truncate -3.7) ; => -3.0roundprocedureRound to nearest integer (half to even).
(round 3.5) ; => 4.0
(round 2.5) ; => 2.0 (banker's rounding)
(round 3.2) ; => 3.0exptprocedureCompute base raised to the power of exponent.
(expt 2 10) ; => 1024
(expt 2 0.5) ; => 1.414...
(expt 2 -1) ; => 0.5sqrtprocedureCompute the square root.
(sqrt 4) ; => 2.0
(sqrt 2) ; => 1.414...squareprocedureCompute the square of a number (x * x).
(square 3) ; => 9
(square -4) ; => 16expprocedureCompute e raised to the given power.
(exp 0) ; => 1.0
(exp 1) ; => 2.718...logprocedureCompute 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)sinprocedureCompute the sine (argument in radians).
(sin 0) ; => 0.0
(sin (/ 3.14159 2)) ; => ~1.0cosprocedureCompute the cosine (argument in radians).
(cos 0) ; => 1.0
(cos 3.14159) ; => ~-1.0tanprocedureCompute the tangent (argument in radians).
(tan 0) ; => 0.0asinprocedureCompute the arc sine (result in radians).
(asin 0) ; => 0.0
(asin 1) ; => ~1.57 (π/2)acosprocedureCompute the arc cosine (result in radians).
(acos 1) ; => 0.0
(acos 0) ; => ~1.57 (π/2)atanprocedureCompute 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?procedureTest if a number is exact (integer or exact rational).
(exact? 42) ; => #t
(exact? 3.14) ; => #finexact?procedureTest if a number is inexact (floating-point).
(inexact? 3.14) ; => #t
(inexact? 42) ; => #fexactprocedureConvert a number to exact representation.
(exact 3.0) ; => 3
(exact 3) ; => 3inexactprocedureConvert a number to inexact (floating-point) representation.
(inexact 3) ; => 3.0
(inexact 3.14) ; => 3.14gcdprocedureCompute the greatest common divisor.
(gcd 12 8) ; => 4
(gcd 12 8 6) ; => 2
(gcd) ; => 0lcmprocedureCompute the least common multiple.
(lcm 3 4) ; => 12
(lcm 3 4 5) ; => 60
(lcm) ; => 1quotientprocedureInteger division, truncating toward zero.
(quotient 13 4) ; => 3
(quotient -13 4) ; => -3bitwise-notprocedureReturn the bitwise complement (NOT) of an exact integer.
(bitwise-not 0) ; => -1
(bitwise-not -1) ; => 0
(bitwise-not 5) ; => -6bitwise-andprocedureReturn 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) ; => -1bitwise-iorprocedureReturn 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) ; => 0bitwise-xorprocedureReturn 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) ; => 0arithmetic-shiftprocedureShift 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)