19 Mathematics Functions
19.1. Math functions
The math/ functions accept numbers, and selected functions also accept temporal values. Arguments to a function must all have
the same numeric type; mixed integers and floats produce #err <mismatch type>.
math/abs also accepts a duration. Each function describes its result and
errors below. The namespace includes constants for pi, tau, e, and conversion
between degrees and radians.
19.1.1. math/pi
The constant :math/pi is the float 3.141592653589793, the nearest binary64 approximation to pi.
19.1.2. math/tau
The constant :math/tau is the float 6.283185307179586, the nearest binary64 approximation to two times pi.
19.1.3. math/deg2rad
The constant :math/deg2rad is pi divided by 180, as a float. Multiply degrees
by this value to convert them to radians.
19.1.4. math/rad2deg
The constant :math/rad2deg is 180 divided by pi, as a float. Multiply radians
by this value to convert them to degrees.
19.1.5. math/e
The constant :math/e is the float 2.718281828459045, the nearest binary64 approximation to the base of natural logarithms.
19.1.6. math/floor
Function(math/floor n) → float
Returns the greatest integral-valued float less than or equal to n after conversion to a float.
[(math/floor 1.8) (math/floor -1.2) (math/floor 2) (math/floor 0.0) (math/floor 9007199254740993) (math/floor 100000000000000000000.0)]
(math/floor 1.8) (math/floor -1.2) (math/floor 2) (math/floor 0.0) (math/floor 9007199254740993) (math/floor 100000000000000000000.0)
1.0 -2.0 2.0 0.0 9007199254740992.0 100000000000000000000.0
19.1.7. math/ceil
Function(math/ceil n) → float
Returns the least integral-valued float greater than or equal to n after conversion to a float.
[(math/ceil 1.2) (math/ceil -1.8) (math/ceil 2) (math/ceil 0.0) (math/ceil -0.2) (math/ceil 9007199254740993) (math/ceil 100000000000000000000.0)]
(math/ceil 1.2) (math/ceil -1.8) (math/ceil 2) (math/ceil 0.0) (math/ceil -0.2) (math/ceil 9007199254740993) (math/ceil 100000000000000000000.0)
2.0 -1.0 2.0 0.0 0.0 9007199254740992.0 100000000000000000000.0
19.1.8. math/round
Function(math/round n) → integer
Returns the integer nearest to n, rounding halfway cases away from zero.
Integer inputs are returned unchanged.
A float whose rounded value is outside the signed 64-bit integer range produces #err <integer overflow>.
[(math/round 1.4) (math/round 1.5) (math/round 2.5) (math/round -1.5) (math/round -2.5) (math/round -1.6) (math/round 0.5) (math/round -0.5) (math/round 0.49999999999999994) (math/round -0.5000000000000001) (math/round 7) (math/round 9007199254740993) (math/round 9223372036854775807) (math/round -9223372036854775808) (math/round -9223372036854776000.0) (math/round 9223372036854775000.0)]
(math/round 1.4) (math/round 1.5) (math/round 2.5) (math/round -1.5) (math/round -2.5) (math/round -1.6) (math/round 0.5) (math/round -0.5) (math/round 0.49999999999999994) (math/round -0.5000000000000001) (math/round 7) (math/round 9007199254740993) (math/round 9223372036854775807) (math/round -9223372036854775808) (math/round -9223372036854776000.0) (math/round 9223372036854775000.0)
1 2 3 -2 -3 -2 1 -1 0 -1 7 9007199254740993 9223372036854775807 -9223372036854775808 -9223372036854775808 9223372036854774784
19.1.9. math/rint
Function(math/rint n) → float
Returns the integral-valued float nearest to n after conversion to a float, choosing the even integer in halfway cases.
The result does not depend on the host's current rounding mode.
[(math/rint 1.5) (math/rint 2.5) (math/rint -1.5) (math/rint -2.5) (math/rint 0.5) (math/rint -0.5) (math/rint 7) (math/rint 9007199254740993)]
(math/rint 1.5) (math/rint 2.5) (math/rint -1.5) (math/rint -2.5) (math/rint 0.5) (math/rint -0.5) (math/rint 7) (math/rint 9007199254740993)
2.0 2.0 -2.0 -2.0 0.0 0.0 7.0 9007199254740992.0
19.1.10. math/trunc
Function(math/trunc n) → float
Returns n converted to a float with its fractional part discarded, rounding toward zero.
[(math/trunc 1.8) (math/trunc -1.8) (math/trunc 2) (math/trunc -0.2) (math/trunc 9007199254740993)]
(math/trunc 1.8) (math/trunc -1.8) (math/trunc 2) (math/trunc -0.2) (math/trunc 9007199254740993)
1.0 -1.0 2.0 0.0 9007199254740992.0
19.1.11. math/pow
Function(math/pow base exponent) → float
Returns base raised to exponent.
A zero exponent returns 1.0, including when base is zero.
A negative base requires an integral-valued exponent after float conversion.
A zero base with a negative exponent produces #err <float overflow>.
[(math/pow 2 3) (math/pow -2 3) (math/pow 9 0.5) (math/pow 0 0) (math/pow 2 -3)]
(math/pow 2 3) (math/pow -2 3) (math/pow 9 0.5) (math/pow 0 0) (math/pow 2 -3)
8.0 -8.0 #err <mismatch type> 1.0 0.125
19.1.12. math/sqrt
Function(math/sqrt n) → float
Returns the square root of n.
A negative n produces #err <float overflow>.
A zero result has the same sign as n.
[(math/sqrt 0) (math/sqrt 4) (math/sqrt 9.0)]
(math/sqrt 0) (math/sqrt 4) (math/sqrt 9.0)
0.0 2.0 3.0
19.1.13. math/cbrt
Function(math/cbrt n) → float
Returns the real cube root of n, with the same sign as n.
[(math/round (* 1000000000000.0 (math/cbrt -8))) (math/round (* 1000000000000.0 (math/cbrt 0))) (math/round (* 1000000000000.0 (math/cbrt 27)))]
(math/round (* 1000000000000.0 (math/cbrt -8))) (math/round (* 1000000000000.0 (math/cbrt 0))) (math/round (* 1000000000000.0 (math/cbrt 27)))
-2000000000000 0 3000000000000
19.1.14. math/hypot
Function(math/hypot x y) → float
Returns the square root of x*x + y*y without intermediate overflow or underflow.
An out-of-range final result produces #err <float overflow>.
[(math/hypot 3 4) (math/hypot -3 -4) (math/hypot 0 0) (math/hypot 0 1.5)]
(math/hypot 3 4) (math/hypot -3 -4) (math/hypot 0 0) (math/hypot 0 1.5)
5.0 5.0 0.0 #err <mismatch type>
[(math/round (* 1000000000000.0 (/ (math/hypot 1e+308 1e+308) 1e+308))) (math/round (* 1000000000000.0 (/ (math/hypot 1e-308 1e-308) 1e-308)))]
(math/round (* 1000000000000.0 (/ (math/hypot 1e+308 1e+308) 1e+308))) (math/round (* 1000000000000.0 (/ (math/hypot 1e-308 1e-308) 1e-308)))
1414213562373 1414213562373
19.1.15. math/exp
Function(math/exp n) → float
Returns e raised to n.
[(math/round (* 1000000000000.0 (math/exp -1))) (math/round (* 1000000000000.0 (math/exp 0))) (math/round (* 1000000000000.0 (math/exp 1)))]
(math/round (* 1000000000000.0 (math/exp -1))) (math/round (* 1000000000000.0 (math/exp 0))) (math/round (* 1000000000000.0 (math/exp 1)))
367879441171 1000000000000 2718281828459
19.1.16. math/exp2
Function(math/exp2 n) → float
Returns two raised to n.
[(math/exp2 -3) (math/exp2 0) (math/exp2 3)]
(math/exp2 -3) (math/exp2 0) (math/exp2 3)
0.125 1.0 8.0
19.1.17. math/expm1
Function(math/expm1 n) → float
Returns e raised to n minus one, computed without the loss of precision from subtracting one from math/exp's rounded result near zero.
[(math/round (* 1000000000000.0 (math/expm1 -1))) (math/round (* 1000000000000.0 (math/expm1 0))) (math/round (* 1000000000000.0 (math/expm1 1)))]
(math/round (* 1000000000000.0 (math/expm1 -1))) (math/round (* 1000000000000.0 (math/expm1 0))) (math/round (* 1000000000000.0 (math/expm1 1)))
-632120558829 0 1718281828459
19.1.18. math/log
Function(math/log n) → float
(math/log n base) → float
Returns the natural logarithm of n, or its logarithm in the supplied base.
When base is supplied, both numbers must have the same type. The base must be
positive and cannot be one.
A zero or negative n produces #err <float overflow>.
[(math/round (* 1000000000000.0 (math/log 0.5))) (math/round (* 1000000000000.0 (math/log 1))) (math/round (* 1000000000000.0 (math/log 2)))]
(math/round (* 1000000000000.0 (math/log 0.5))) (math/round (* 1000000000000.0 (math/log 1))) (math/round (* 1000000000000.0 (math/log 2)))
-693147180560 0 693147180560
19.1.19. math/log2
Function(math/log2 n) → float
Returns the base-two logarithm of n.
A zero or negative n produces #err <float overflow>.
[(math/log2 0.5) (math/log2 1) (math/log2 2) (math/log2 8)]
(math/log2 0.5) (math/log2 1) (math/log2 2) (math/log2 8)
-1.0 0.0 1.0 3.0
19.1.20. math/log10
Function(math/log10 n) → float
Returns the base-ten logarithm of n.
A zero or negative n produces #err <float overflow>.
[(math/log10 0.1) (math/log10 1) (math/log10 10) (math/log10 100)]
(math/log10 0.1) (math/log10 1) (math/log10 10) (math/log10 100)
-1.0 0.0 1.0 2.0
19.1.21. math/log1p
Function(math/log1p n) → float
Returns the natural logarithm of one plus n, computed without the loss of precision from rounding 1 + n near zero.
An n less than or equal to -1 produces #err <float overflow>.
[(math/round (* 1000000000000.0 (math/log1p -0.5))) (math/round (* 1000000000000.0 (math/log1p 0))) (math/round (* 1000000000000.0 (math/log1p 1)))]
(math/round (* 1000000000000.0 (math/log1p -0.5))) (math/round (* 1000000000000.0 (math/log1p 0))) (math/round (* 1000000000000.0 (math/log1p 1)))
-693147180560 0 693147180560
19.1.22. math/sin
Function(math/sin angle) → float
Returns the sine of angle in radians.
[(math/round (* 1000000000000.0 (math/sin -1))) (math/round (* 1000000000000.0 (math/sin 0))) (math/round (* 1000000000000.0 (math/sin 1)))]
(math/round (* 1000000000000.0 (math/sin -1))) (math/round (* 1000000000000.0 (math/sin 0))) (math/round (* 1000000000000.0 (math/sin 1)))
-841470984808 0 841470984808
19.1.23. math/cos
Function(math/cos angle) → float
Returns the cosine of angle in radians.
[(math/round (* 1000000000000.0 (math/cos -1))) (math/round (* 1000000000000.0 (math/cos 0))) (math/round (* 1000000000000.0 (math/cos 1)))]
(math/round (* 1000000000000.0 (math/cos -1))) (math/round (* 1000000000000.0 (math/cos 0))) (math/round (* 1000000000000.0 (math/cos 1)))
540302305868 1000000000000 540302305868
19.1.24. math/tan
Function(math/tan angle) → float
Returns the tangent of angle in radians.
[(math/round (* 1000000000000.0 (math/tan -1))) (math/round (* 1000000000000.0 (math/tan 0))) (math/round (* 1000000000000.0 (math/tan 1)))]
(math/round (* 1000000000000.0 (math/tan -1))) (math/round (* 1000000000000.0 (math/tan 0))) (math/round (* 1000000000000.0 (math/tan 1)))
-1557407724655 0 1557407724655
19.1.25. math/asin
Function(math/asin n) → float
Returns the arc sine of n in radians, in the range from negative pi/2 to pi/2.
An n outside [-1, 1] produces #err <float overflow>.
[(math/round (* 1000000000000.0 (math/asin -1))) (math/round (* 1000000000000.0 (math/asin 0))) (math/round (* 1000000000000.0 (math/asin 1)))]
(math/round (* 1000000000000.0 (math/asin -1))) (math/round (* 1000000000000.0 (math/asin 0))) (math/round (* 1000000000000.0 (math/asin 1)))
-1570796326795 0 1570796326795
19.1.26. math/acos
Function(math/acos n) → float
Returns the arc cosine of n in radians, in the range from zero to pi.
An n outside [-1, 1] produces #err <float overflow>.
[(math/round (* 1000000000000.0 (math/acos -1))) (math/round (* 1000000000000.0 (math/acos 0))) (math/round (* 1000000000000.0 (math/acos 1)))]
(math/round (* 1000000000000.0 (math/acos -1))) (math/round (* 1000000000000.0 (math/acos 0))) (math/round (* 1000000000000.0 (math/acos 1)))
3141592653590 1570796326795 0
19.1.27. math/atan
Function(math/atan n) → float
Returns the arc tangent of n in radians, in the range from negative pi/2 to pi/2.
[(math/round (* 1000000000000.0 (math/atan -1))) (math/round (* 1000000000000.0 (math/atan 0))) (math/round (* 1000000000000.0 (math/atan 1)))]
(math/round (* 1000000000000.0 (math/atan -1))) (math/round (* 1000000000000.0 (math/atan 0))) (math/round (* 1000000000000.0 (math/atan 1)))
-785398163397 0 785398163397
19.1.28. math/atan2
Function(math/atan2 y x) → float
Returns the angle of the point (x, y) in radians, in the range from negative pi to pi, using both signs to select the quadrant.
The y argument comes first.
For zero y and positive x or positive zero x, returns zero with y's sign.
For zero y and negative x or negative zero x, returns pi with y's sign.
[(math/round (* 1000000000000.0 (math/atan2 0 1))) (math/round (* 1000000000000.0 (math/atan2 1 1))) (math/round (* 1000000000000.0 (math/atan2 1 -1))) (math/round (* 1000000000000.0 (math/atan2 -1 -1))) (math/round (* 1000000000000.0 (math/atan2 -1 1)))]
(math/round (* 1000000000000.0 (math/atan2 0 1))) (math/round (* 1000000000000.0 (math/atan2 1 1))) (math/round (* 1000000000000.0 (math/atan2 1 -1))) (math/round (* 1000000000000.0 (math/atan2 -1 -1))) (math/round (* 1000000000000.0 (math/atan2 -1 1)))
0 785398163397 2356194490192 -2356194490192 -785398163397
[(math/round (* 1000000000000.0 (math/atan2 0.0 (- 0.0)))) (math/round (* 1000000000000.0 (math/atan2 (- 0.0) (- 0.0)))) (math/copy-sign 1.0 (math/atan2 (- 0.0) 0.0))]
(math/round (* 1000000000000.0 (math/atan2 0.0 (- 0.0)))) (math/round (* 1000000000000.0 (math/atan2 (- 0.0) (- 0.0)))) (math/copy-sign 1.0 (math/atan2 (- 0.0) 0.0))
3141592653590 -3141592653590 -1.0
19.1.29. math/sinh
Function(math/sinh n) → float
Returns the hyperbolic sine of n.
[(math/round (* 1000000000000.0 (math/sinh -1))) (math/round (* 1000000000000.0 (math/sinh 0))) (math/round (* 1000000000000.0 (math/sinh 1)))]
(math/round (* 1000000000000.0 (math/sinh -1))) (math/round (* 1000000000000.0 (math/sinh 0))) (math/round (* 1000000000000.0 (math/sinh 1)))
-1175201193644 0 1175201193644
19.1.30. math/cosh
Function(math/cosh n) → float
Returns the hyperbolic cosine of n.
[(math/round (* 1000000000000.0 (math/cosh -1))) (math/round (* 1000000000000.0 (math/cosh 0))) (math/round (* 1000000000000.0 (math/cosh 1)))]
(math/round (* 1000000000000.0 (math/cosh -1))) (math/round (* 1000000000000.0 (math/cosh 0))) (math/round (* 1000000000000.0 (math/cosh 1)))
1543080634815 1000000000000 1543080634815
19.1.31. math/tanh
Function(math/tanh n) → float
Returns the hyperbolic tangent of n.
[(math/round (* 1000000000000.0 (math/tanh -1))) (math/round (* 1000000000000.0 (math/tanh 0))) (math/round (* 1000000000000.0 (math/tanh 1)))]
(math/round (* 1000000000000.0 (math/tanh -1))) (math/round (* 1000000000000.0 (math/tanh 0))) (math/round (* 1000000000000.0 (math/tanh 1)))
-761594155956 0 761594155956
19.1.32. math/asinh
Function(math/asinh n) → float
Returns the inverse hyperbolic sine of n.
[(math/round (* 1000000000000.0 (math/asinh -1))) (math/round (* 1000000000000.0 (math/asinh 0))) (math/round (* 1000000000000.0 (math/asinh 1)))]
(math/round (* 1000000000000.0 (math/asinh -1))) (math/round (* 1000000000000.0 (math/asinh 0))) (math/round (* 1000000000000.0 (math/asinh 1)))
-881373587020 0 881373587020
19.1.33. math/acosh
Function(math/acosh n) → float
Returns the nonnegative inverse hyperbolic cosine of n.
An n less than 1 produces #err <float overflow>.
[(math/round (* 1000000000000.0 (math/acosh 1))) (math/round (* 1000000000000.0 (math/acosh 2))) (math/round (* 1000000000000.0 (math/acosh 3)))]
(math/round (* 1000000000000.0 (math/acosh 1))) (math/round (* 1000000000000.0 (math/acosh 2))) (math/round (* 1000000000000.0 (math/acosh 3)))
0 1316957896925 1762747174039
19.1.34. math/atanh
Function(math/atanh n) → float
Returns the inverse hyperbolic tangent of n.
An n outside the open interval from -1 to 1 produces #err <float overflow>.
[(math/round (* 1000000000000.0 (math/atanh -0.5))) (math/round (* 1000000000000.0 (math/atanh 0))) (math/round (* 1000000000000.0 (math/atanh 0.5)))]
(math/round (* 1000000000000.0 (math/atanh -0.5))) (math/round (* 1000000000000.0 (math/atanh 0))) (math/round (* 1000000000000.0 (math/atanh 0.5)))
-549306144334 0 549306144334
19.1.35. math/signum
Function(math/signum n) → float
Returns -1.0 for negative n, 1.0 for positive n, or zero with n's sign for zero.
[(math/signum -3) (math/signum 0) (math/signum 5.5) (math/signum (- 0.0))]
(math/signum -3) (math/signum 0) (math/signum 5.5) (math/signum (- 0.0))
-1.0 0.0 1.0 0.0
19.1.36. math/copy-sign
Function(math/copy-sign magnitude sign) → float
Returns the absolute value of magnitude with the sign of sign, including the sign of floating-point zero.
[(math/copy-sign 3.5 -2) (math/copy-sign -3.5 2) (math/copy-sign 1.0 (math/copy-sign 0 -1)) (math/copy-sign 2 (- 0.0)) (math/copy-sign -2 0.0)]
(math/copy-sign 3.5 -2) (math/copy-sign -3.5 2) (math/copy-sign 1.0 (math/copy-sign 0 -1)) (math/copy-sign 2 (- 0.0)) (math/copy-sign -2 0.0)
#err <mismatch type> #err <mismatch type> -1.0 #err <mismatch type> #err <mismatch type>
19.1.37. math/floor-div
Function(math/floor-div n d) → integer
Returns the integer quotient of n divided by d, rounded toward negative infinity.
Before division, float arguments are converted to integers by truncating toward zero.
An out-of-range conversion or quotient produces #err <integer overflow>.
A zero divisor after conversion produces #err <division integer zero>.
[(math/floor-div 5 3) (math/floor-div -5 3) (math/floor-div 5 -3) (math/floor-div -5 -3) (math/floor-div 9223372036854775807 1) (math/floor-div -9223372036854775808 1) (math/floor-div 5.9 2.1) (math/floor-div -5.9 2.1)]
(math/floor-div 5 3) (math/floor-div -5 3) (math/floor-div 5 -3) (math/floor-div -5 -3) (math/floor-div 9223372036854775807 1) (math/floor-div -9223372036854775808 1) (math/floor-div 5.9 2.1) (math/floor-div -5.9 2.1)
1 -2 -2 1 9223372036854775807 -9223372036854775808 2 -3
19.1.38. math/floor-mod
Function(math/floor-mod n d) → integer
Returns the integer remainder of n divided by d using a quotient rounded toward negative infinity.
Before division, float arguments are converted to integers by truncating toward zero.
An out-of-range conversion produces #err <integer overflow>.
A zero divisor after conversion produces #err <division integer zero>.
A nonzero result has the divisor's sign and a smaller magnitude.
The quotient need not fit in an integer, so (math/floor-mod -9223372036854775808 -1) returns 0.
[(math/floor-mod 5 3) (math/floor-mod -5 3) (math/floor-mod 5 -3) (math/floor-mod -5 -3) (math/floor-mod -9223372036854775808 -1) (math/floor-mod 9223372036854775807 2) (math/floor-mod 5.9 2.1) (math/floor-mod -5.9 2.1)]
(math/floor-mod 5 3) (math/floor-mod -5 3) (math/floor-mod 5 -3) (math/floor-mod -5 -3) (math/floor-mod -9223372036854775808 -1) (math/floor-mod 9223372036854775807 2) (math/floor-mod 5.9 2.1) (math/floor-mod -5.9 2.1)
2 1 -1 -2 0 1 1 1
19.1.39. math/abs
Function(math/abs value) → value
Returns the absolute value, preserving integer or float kind. The absolute value of the minimum integer overflows and is an error. Durations are also accepted and return a duration; the minimum tick duration likewise overflows. UTC timestamps are rejected.
19.1.40. math/sign
Function(math/sign number) → integer
Returns integer -1, 0, or 1 according to the argument's sign.
19.1.41. math/lerp
Function(math/lerp start end fraction) → float
Returns start + (end - start) * fraction as a float. The fraction is not
clamped, so values outside zero through one extrapolate beyond the endpoints.
(math/lerp 10.0 20.0 0.5) (math/lerp 10.0 20.0 1.5) (math/lerp 10 20 0)
15.0 25.0 10.0
19.1.42. math/inverse-lerp
Function(math/inverse-lerp start end value) → float
Returns the fraction locating value between start and end, without
clamping. Equal endpoints are an error.
(math/inverse-lerp 10 20 15) (math/inverse-lerp 10 20 25) (math/inverse-lerp 10 20 10)
0.5 1.5 0.0
19.1.43. math/remap
Function(math/remap input-start input-end output-start output-end value) → float
Maps value from the input interval to the output interval, without clamping.
Equal input endpoints are an error. Reversed intervals are accepted.
(math/remap 0 10 100 200 5) (math/remap 0 10 100 200 15) (math/remap 10 0 100 200 5)
150.0 250.0 150.0
19.1.44. math/zero?
Function(math/zero? n) → boolean
Returns true if n is zero, otherwise false.
(math/zero? 0) (math/zero? 0.0) (math/zero? 3) (math/zero? -2) (math/zero? 0.5) (math/zero? -0.5) (math/zero? -9223372036854775808) (math/zero? 9223372036854775807)
true true false false false false false false
{boolean (math/zero? false) extra (math/zero? 0 0) list (math/zero? []) missing (math/zero?) null-input (math/zero? null) string (math/zero? "0")}
#cases [{output #err <mismatch type> select "string"} {output #err <mismatch type> select "null-input"} {output #err <mismatch type> select "boolean"} {output #err <mismatch type> select "list"} {output #err <arity mismatch> select "missing"} {output #err <arity mismatch> select "extra"}]
19.1.45. math/odd?
Function(math/odd? n) → boolean
Returns true if integer n is odd, otherwise false.
(math/odd? 1) (math/odd? 2) (math/odd? -3) (math/odd? 0) (math/odd? -2) (math/odd? -9223372036854775808) (math/odd? 9223372036854775807)
true false true false false false true
{boolean (math/odd? true) extra (math/odd? 1 3) float (math/odd? 1.0) fractional (math/odd? 1.5) missing (math/odd?) null-input (math/odd? null) string (math/odd? "1")}
#cases [{output #err <mismatch type> select "float"} {output #err <mismatch type> select "fractional"} {output #err <mismatch type> select "string"} {output #err <mismatch type> select "null-input"} {output #err <mismatch type> select "boolean"} {output #err <arity mismatch> select "missing"} {output #err <arity mismatch> select "extra"}]
19.1.46. math/even?
Function(math/even? n) → boolean
Returns true if integer n is even, otherwise false.
(math/even? 0) (math/even? 2) (math/even? -3) (math/even? 1) (math/even? -2) (math/even? -9223372036854775808) (math/even? 9223372036854775807)
true true false false true true false
{boolean (math/even? false) extra (math/even? 2 4) float (math/even? 2.0) fractional (math/even? 2.5) missing (math/even?) null-input (math/even? null) string (math/even? "2")}
#cases [{output #err <mismatch type> select "float"} {output #err <mismatch type> select "fractional"} {output #err <mismatch type> select "string"} {output #err <mismatch type> select "null-input"} {output #err <mismatch type> select "boolean"} {output #err <arity mismatch> select "missing"} {output #err <arity mismatch> select "extra"}]
19.1.47. math/min
Function(math/min value more*) → value
Returns the least argument. Requires at least one argument, and all arguments must have the same numeric or temporal kind. Integer and temporal comparisons retain full 64-bit precision.
(math/min 3 1 2) (math/min 1.5 0.5) (math/min 9007199254740994 9007199254740993)
1 0.5 9007199254740993
19.1.48. math/max
Function(math/max value more*) → value
Returns the greatest argument. Requires at least one argument, and all arguments must have the same numeric or temporal kind. Integer and temporal comparisons retain full 64-bit precision.
(math/max 3 1 2) (math/max 1.5 0.5) (math/max -9007199254740994 -9007199254740993)
3 1.5 -9007199254740993
19.1.49. math/clamp
Function(math/clamp value low high) → value
Returns value limited to the inclusive interval from low through high.
All three arguments must have the same numeric or temporal kind, and low
must not exceed high.
19.2. Packed math values
Packed math values are immutable arrays of finite binary64 components, the
same numbers as a Float. Each shape is its own value kind. Its kind predicate
is its tag name with ?, such as v3? or m44?, and a schema uses that
predicate. A #v4 value is not a #quat value. DUST, RON, and EDN use these tags:
| Tag | Components | Value |
|---|---|---|
#v2 |
2 | two-dimensional vector |
#v3 |
3 | three-dimensional vector |
#v4 |
4 | four-dimensional vector |
#m22 |
4 | 2 by 2 matrix |
#m33 |
9 | 3 by 3 matrix |
#m44 |
16 | 4 by 4 matrix |
#quat |
4 | quaternion |
#aabb |
6 | minimum and maximum three-dimensional points |
#ray |
6 | origin and direction |
#sphere |
4 | center and radius |
#plane |
4 | normal and distance |
#triangle |
9 | three three-dimensional points |
#frustum |
24 | six planes |
A Float component keeps its exact value, including -0.0, and an Integer
component becomes the nearest Float. No component or operation rounds to
binary32. As for Floats, -0.0 and 0.0 are equal, order together, and hash
together. An operation whose result or intermediate value passes the Float
range produces #err <float overflow>.
19.2.1. Conventions
- Matrix layout
- A matrix is row-major: its tag lists rows, and its
components are stored row after row. A matrix multiplies a column vector,
so
(* m v)ism vand(* a b)appliesbfirst, thena. A translation is in the last column of a#m44. - Coordinate basis
- Space is right-handed and Y-up.
:math/vectors/rightis +X,:math/vectors/upis +Y, and:math/vectors/forwardis −Z, as in OpenGL and glTF cameras. (cljgame is Z-up with +Y forward; convert at that boundary.) - Angle units
- Angles are radians, as for
math/sin. - Quaternions
- A
#quatlistsx y z w: the vector part, then the scalar part.(* q1 q2)is the Hamilton product q1 q2, which appliesq2first andq1second, as matrices do.(* q v)rotates the#v3vby a unit quaternionq, so(* q1 q2 v)is(* q1 (* q2 v)). - Planes
- A
#planewith normalnand distancedholds the pointspwheren · p + d = 0. Its positive side, wheren · p + d > 0, is the inside of a#frustum. - Contact
- Geometry boundary contact and tangency count as intersections.
- Tolerance
- Two computations of one result, such as the scalar and batch kernels, or a kernel and its binary64 reference, agree when each component satisfies |a − b| ≤ 1e-12 · max(1, |a|, |b|): a relative tolerance at magnitudes above one and an absolute one below. Operators and named operations give exact binary64 results of their formulas; they do not round to the tolerance.
(* #m22 [[0 -1] [1 0]] #v2 [1 0]) (* #m44 [[1 0 0 5] [0 1 0 6] [0 0 1 7] [0 0 0 1]] #v4 [1 1 1 1]) (* #m44 [[1 0 0 5] [0 1 0 6] [0 0 1 7] [0 0 0 1]] #v4 [1 1 1 0]) (* #quat [0 0.7071067811865476 0 0.7071067811865476] :math/vectors/forward) (let [yaw #quat [0 0.7071067811865476 0 0.7071067811865476] roll #quat [0 0 0.7071067811865476 0.7071067811865476]] (* yaw roll :math/vectors/right)) (let [yaw #quat [0 0.7071067811865476 0 0.7071067811865476] roll #quat [0 0 0.7071067811865476 0.7071067811865476]] (* roll yaw :math/vectors/right)) (math/intersects? #plane [[0 1 0] -2] #sphere [[0 2 0] 0.5]) (math/intersects? #ray [[0 5 0] [0 -1 0]] #plane [[0 1 0] -2])
(* #m22 [[0.0 -1.0] [1.0 0.0]] #v2 [1.0 0.0]) (* #m44 [[1.0 0.0 0.0 5.0] [0.0 1.0 0.0 6.0] [0.0 0.0 1.0 7.0] [0.0 0.0 0.0 1.0]] #v4 [1.0 1.0 1.0 1.0]) (* #m44 [[1.0 0.0 0.0 5.0] [0.0 1.0 0.0 6.0] [0.0 0.0 1.0 7.0] [0.0 0.0 0.0 1.0]] #v4 [1.0 1.0 1.0 0.0]) (* #quat [0.0 0.7071067811865476 0.0 0.7071067811865476] :math/vectors/forward) (let [yaw #quat [0.0 0.7071067811865476 0.0 0.7071067811865476] roll #quat [0.0 0.0 0.7071067811865476 0.7071067811865476]] (* yaw roll :math/vectors/right)) (let [yaw #quat [0.0 0.7071067811865476 0.0 0.7071067811865476] roll #quat [0.0 0.0 0.7071067811865476 0.7071067811865476]] (* roll yaw :math/vectors/right)) (math/intersects? #plane [[0.0 1.0 0.0] -2.0] #sphere [[0.0 2.0 0.0] 0.5]) (math/intersects? #ray [[0.0 5.0 0.0] [0.0 -1.0 0.0]] #plane [[0.0 1.0 0.0] -2.0])
#v2 [0.0 1.0] #v4 [6.0 7.0 8.0 1.0] #v4 [1.0 1.0 1.0 0.0] #v3 [-1.0000000000000002 0.0 2.220446049250313e-16] #v3 [-4.440892098500626e-16 1.0000000000000004 0.0] #v3 [-4.440892098500626e-16 0.0 -1.0000000000000004] true true
19.2.2. Operators
The arithmetic operators +, -, * and / take packed values. As for
numbers, they reduce left to right, one pair at a time, so
(* 2 3 v) is (* (* 2 3) v). A number operand can be an Integer or a
Float; it becomes a Float.
| Operands | Operators | Result |
|---|---|---|
| two vectors of one width | + - * / |
componentwise vector |
| vector and number, either order | * |
scaled vector |
| vector and number | / |
vector divided by the number |
| two matrices of one size | + - |
componentwise matrix |
| two matrices of one size | * |
matrix product |
| matrix and vector of its size | * |
column vector product |
| two quaternions | * |
Hamilton product |
quaternion and #v3 |
* |
rotated vector |
| one vector or matrix | - |
negation |
Other pairs give #err <mismatch type>: a vector and a number with + or
-, a number divided by a vector, a matrix and a number, quaternions with +
or -, geometry values, and #m44 with #v3. Points and directions
transform differently, so a #m44 transforms a #v3 by name, as with
math/vectors/transform. A zero divisor gives
#err <division float zero>.
(+ #v3 [1 2 3] #v3 [10 20 30] #v3 [100 200 300]) (- #v2 [1 2] #v2 [0.5 0.5]) (- #v3 [1 -2 0]) (* #v3 [1 2 3] 2) (* 0.5 #v3 [1 2 3]) (* 2 3 #v2 [1 1]) (* #v4 [1 2 3 4] #v4 [2 2 2 2]) (/ #v3 [1 2 3] 2) (/ #v3 [1 2 3] #v3 [2 4 8]) (+ #m22 [[1 2] [3 4]] #m22 [[1 1] [1 1]]) (- #m33 [[1 2 3] [4 5 6] [7 8 9]] #m33 [[1 0 0] [0 1 0] [0 0 1]]) (* #m22 [[1 2] [3 4]] #m22 [[5 6] [7 8]]) (* #m22 [[1 2] [3 4]] #v2 [1 1]) (* #m44 [[1 0 0 10] [0 1 0 20] [0 0 1 30] [0 0 0 1]] #v4 [1 2 3 1]) (* #quat [0 0 1 0] #v3 [1 2 3]) (* #quat [1 0 0 0] #quat [0 1 0 0]) (* #quat [0 1 0 0] #quat [1 0 0 0]) (* #quat [0 0 0 1] #quat [0 0 1 0] #v3 [1 0 0]) (* #v3 [1e300 0 0] 1e10)
(+ #v3 [1.0 2.0 3.0] #v3 [10.0 20.0 30.0] #v3 [100.0 200.0 300.0]) (- #v2 [1.0 2.0] #v2 [0.5 0.5]) (- #v3 [1.0 -2.0 0.0]) (* #v3 [1.0 2.0 3.0] 2) (* 0.5 #v3 [1.0 2.0 3.0]) (* 2 3 #v2 [1.0 1.0]) (* #v4 [1.0 2.0 3.0 4.0] #v4 [2.0 2.0 2.0 2.0]) (/ #v3 [1.0 2.0 3.0] 2) (/ #v3 [1.0 2.0 3.0] #v3 [2.0 4.0 8.0]) (+ #m22 [[1.0 2.0] [3.0 4.0]] #m22 [[1.0 1.0] [1.0 1.0]]) (- #m33 [[1.0 2.0 3.0] [4.0 5.0 6.0] [7.0 8.0 9.0]] #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]]) (* #m22 [[1.0 2.0] [3.0 4.0]] #m22 [[5.0 6.0] [7.0 8.0]]) (* #m22 [[1.0 2.0] [3.0 4.0]] #v2 [1.0 1.0]) (* #m44 [[1.0 0.0 0.0 10.0] [0.0 1.0 0.0 20.0] [0.0 0.0 1.0 30.0] [0.0 0.0 0.0 1.0]] #v4 [1.0 2.0 3.0 1.0]) (* #quat [0.0 0.0 1.0 0.0] #v3 [1.0 2.0 3.0]) (* #quat [1.0 0.0 0.0 0.0] #quat [0.0 1.0 0.0 0.0]) (* #quat [0.0 1.0 0.0 0.0] #quat [1.0 0.0 0.0 0.0]) (* #quat [0.0 0.0 0.0 1.0] #quat [0.0 0.0 1.0 0.0] #v3 [1.0 0.0 0.0]) (* #v3 [1e+300 0.0 0.0] 10000000000.0)
#v3 [111.0 222.0 333.0] #v2 [0.5 1.5] #v3 [-1.0 2.0 -0.0] #v3 [2.0 4.0 6.0] #v3 [0.5 1.0 1.5] #v2 [6.0 6.0] #v4 [2.0 4.0 6.0 8.0] #v3 [0.5 1.0 1.5] #v3 [0.5 0.5 0.375] #m22 [[2.0 3.0] [4.0 5.0]] #m33 [[0.0 2.0 3.0] [4.0 4.0 6.0] [7.0 8.0 8.0]] #m22 [[19.0 22.0] [43.0 50.0]] #v2 [3.0 7.0] #v4 [11.0 22.0 33.0 1.0] #v3 [-1.0 -2.0 3.0] #quat [0.0 0.0 1.0 0.0] #quat [0.0 0.0 -1.0 0.0] #v3 [-1.0 0.0 0.0] #err {<float overflow> message "floating-point overflow"}
(+ #v3 [1 2 3] 1) (+ #v2 [1 2] #v3 [1 2 3]) (- 1 #v2 [1 2]) (/ 1 #v2 [1 2]) (/ #v2 [1 2] 0) (/ #v2 [1 2] #v2 [1 0]) (* #m22 [[1 2] [3 4]] 2) (* #m44 [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]] #v3 [1 2 3]) (* #m33 [[1 0 0] [0 1 0] [0 0 1]] #m22 [[1 0] [0 1]]) (+ #quat [0 0 0 1] #quat [0 0 0 1]) (- #quat [0 0 0 1]) (* #aabb [[0 0 0] [1 1 1]] 2) (+ #v2 [1 2] "1") (* #v2 [1e300 1] #v2 [1e300 1]) (* #aabb [[0 0 0] [1 1 1]]) (* #ray [[0 0 0] [1 0 0]]) (* #sphere [[0 0 0] 1]) (* #plane [[0 1 0] 0]) (* #triangle [[0 0 0] [1 0 0] [0 1 0]]) (* #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]])
(+ #v3 [1.0 2.0 3.0] 1) (+ #v2 [1.0 2.0] #v3 [1.0 2.0 3.0]) (- 1 #v2 [1.0 2.0]) (/ 1 #v2 [1.0 2.0]) (/ #v2 [1.0 2.0] 0) (/ #v2 [1.0 2.0] #v2 [1.0 0.0]) (* #m22 [[1.0 2.0] [3.0 4.0]] 2) (* #m44 [[1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]] #v3 [1.0 2.0 3.0]) (* #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]] #m22 [[1.0 0.0] [0.0 1.0]]) (+ #quat [0.0 0.0 0.0 1.0] #quat [0.0 0.0 0.0 1.0]) (- #quat [0.0 0.0 0.0 1.0]) (* #aabb [[0.0 0.0 0.0] [1.0 1.0 1.0]] 2) (+ #v2 [1.0 2.0] "1") (* #v2 [1e+300 1.0] #v2 [1e+300 1.0]) (* #aabb [[0.0 0.0 0.0] [1.0 1.0 1.0]]) (* #ray [[0.0 0.0 0.0] [1.0 0.0 0.0]]) (* #sphere [[0.0 0.0 0.0] 1.0]) (* #plane [[0.0 1.0 0.0] 0.0]) (* #triangle [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]]) (* #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]])
#err {<mismatch type> message "packed + requires two vectors or two matrices of one shape"} #err {<mismatch type> message "packed + requires two vectors or two matrices of one shape"} #err {<mismatch type> message "packed - requires two vectors or two matrices of one shape"} #err {<mismatch type> message "packed / requires a vector and a vector of its width or a number"} #err {<division float zero> message "division by zero"} #err {<division float zero> message "division by zero"} #err {<mismatch type> message "unsupported packed * operands; transform points and directions by name"} #err {<mismatch type> message "unsupported packed * operands; transform points and directions by name"} #err {<mismatch type> message "unsupported packed * operands; transform points and directions by name"} #err {<mismatch type> message "packed + requires two vectors or two matrices of one shape"} #err {<mismatch type> message "unsupported packed - operand"} #err {<mismatch type> message "unsupported packed * operands; transform points and directions by name"} #err {<mismatch type> message "packed + requires two vectors or two matrices of one shape"} #err {<float overflow> message "floating-point overflow"} #err {<mismatch type> message "unsupported packed * operand"} #err {<mismatch type> message "unsupported packed * operand"} #err {<mismatch type> message "unsupported packed * operand"} #err {<mismatch type> message "unsupported packed * operand"} #err {<mismatch type> message "unsupported packed * operand"} #err {<mismatch type> message "unsupported packed * operand"}
19.2.3. Destructuring and lookup
A list binding pattern in let, fn or for selects
a packed value's parts, as its tag writes them:
| Value | Parts |
|---|---|
#v2, #v3, #v4 |
components |
#quat |
x y z w |
#m22, #m33, #m44 |
row vectors |
#aabb |
minimum and maximum #v3 |
#ray |
origin and direction #v3 |
#sphere |
center #v3 and radius |
#plane |
normal #v3 and distance |
#triangle |
three #v3 points |
#frustum |
six #plane values |
A component is a Float. Destructuring reads components directly and makes no
intermediate list. A nested pattern reads the components of a packed part
without making that part: [[x] [_ _ z]] over an AABB makes no #v3. Only a
packed part bound to a name becomes its own value. Short and long patterns
follow the list rules: a missing part binds null, extra parts are ignored,
and a ... rest capture is a list of the remaining parts. A map pattern over
a packed value is #err <mismatch type>.
(let [[x y z] #v3 [1 2 3]] (+ x y z)) (let [[x y z] #v3 [1 2 3]] [x y z]) (let [[x y z w] #quat [0 0 0 1]] [x y z w]) (let [[r0 r1] #m22 [[1 2] [3 4]]] [r0 r1]) (let [[[a b] [c d]] #m22 [[1 2] [3 4]]] [a b c d]) (let [[minimum maximum] #aabb [[0 0 0] [1 2 3]]] [minimum maximum]) (let [[[x] [_ _ z]] #aabb [[7 0 0] [1 2 3]]] [x z]) (let [[origin direction] #ray [[0 0 0] [1 0 0]]] [origin direction]) (let [[center radius] #sphere [[1 2 3] 4]] [center radius]) (let [[normal distance] #plane [[0 1 0] -2]] [normal distance]) (let [[a b c] #triangle [[0 0 0] [1 0 0] [0 1 0]]] c) (let [[near] #frustum [[0 0 1 1] [0 0 -1 1] [1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1]]] near) (let [[x ... more] #v4 [1 2 3 4]] [x more]) (let [[x y z w] #v3 [1 2 3]] [z w]) (let [[x] null] x) ((fn [[x y]] (+ x y)) #v2 [1.5 2]) (for [[x y] [#v2 [1 2] #v2 [3 4]]] (* x y))
(let [[x y z] #v3 [1.0 2.0 3.0]] (+ x y z)) (let [[x y z] #v3 [1.0 2.0 3.0]] [x y z]) (let [[x y z w] #quat [0.0 0.0 0.0 1.0]] [x y z w]) (let [[r0 r1] #m22 [[1.0 2.0] [3.0 4.0]]] [r0 r1]) (let [[[a b] [c d]] #m22 [[1.0 2.0] [3.0 4.0]]] [a b c d]) (let [[minimum maximum] #aabb [[0.0 0.0 0.0] [1.0 2.0 3.0]]] [minimum maximum]) (let [[[x] [_ _ z]] #aabb [[7.0 0.0 0.0] [1.0 2.0 3.0]]] [x z]) (let [[origin direction] #ray [[0.0 0.0 0.0] [1.0 0.0 0.0]]] [origin direction]) (let [[center radius] #sphere [[1.0 2.0 3.0] 4.0]] [center radius]) (let [[normal distance] #plane [[0.0 1.0 0.0] -2.0]] [normal distance]) (let [[a b c] #triangle [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]]] c) (let [[near] #frustum [[0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0] [1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0]]] near) (let [[x ... more] #v4 [1.0 2.0 3.0 4.0]] [x more]) (let [[x y z w] #v3 [1.0 2.0 3.0]] [z w]) (let [[x] null] x) ((fn [[x y]] (+ x y)) #v2 [1.5 2.0]) (for [[x y] [#v2 [1.0 2.0] #v2 [3.0 4.0]]] (* x y))
6.0 [1.0 2.0 3.0] [0.0 0.0 0.0 1.0] [#v2 [1.0 2.0] #v2 [3.0 4.0]] [1.0 2.0 3.0 4.0] [#v3 [0.0 0.0 0.0] #v3 [1.0 2.0 3.0]] [7.0 3.0] [#v3 [0.0 0.0 0.0] #v3 [1.0 0.0 0.0]] [#v3 [1.0 2.0 3.0] 4.0] [#v3 [0.0 1.0 0.0] -2.0] #v3 [0.0 1.0 0.0] #plane [[0.0 0.0 1.0] 1.0] [1.0 [2.0 3.0 4.0]] [3.0 null] null 3.5 [2.0 12.0]
(let [{x a} #v3 [1 2 3]] a)
(let [{x a} #v3 [1.0 2.0 3.0]] a)
#err {<mismatch type> message "map destructuring requires a map or null"}
(let [[x [y]] #v3 [1 2 3]] y)
(let [[x [y]] #v3 [1.0 2.0 3.0]] y)
#err {<mismatch type> message "list destructuring requires a list, a packed math value, or null"}
For a computed index, get, lists/nth and a packed value in
callee position select the same parts. A path reads through nested parts,
and a string swizzles a vector (see
math/vectors/swizzle).
(get #v3 [4 5 6] 1) (get #v3 [4 5 6] -1) (get #v3 [4 5 6] 3) (get #v3 [4 5 6] 3 0.0) (get #m33 [[1 2 3] [4 5 6] [7 8 9]] 1) (get #m33 [[1 2 3] [4 5 6] [7 8 9]] [2 0]) (get #sphere [[1 2 3] 4] 1) (lists/nth #v3 [4 5 6] 2) (let [v #v3 [4 5 6]] (v 0)) (let [i 2] (#v3 [4 5 6] i)) (get #v3 [4 5 6] "zy") (lists/nth #v3 [4 5 6] 3) (get #m22 [[1 2] [3 4]] "x")
(get #v3 [4.0 5.0 6.0] 1) (get #v3 [4.0 5.0 6.0] -1) (get #v3 [4.0 5.0 6.0] 3) (get #v3 [4.0 5.0 6.0] 3 0.0) (get #m33 [[1.0 2.0 3.0] [4.0 5.0 6.0] [7.0 8.0 9.0]] 1) (get #m33 [[1.0 2.0 3.0] [4.0 5.0 6.0] [7.0 8.0 9.0]] [2 0]) (get #sphere [[1.0 2.0 3.0] 4.0] 1) (lists/nth #v3 [4.0 5.0 6.0] 2) (let [v #v3 [4.0 5.0 6.0]] (v 0)) (let [i 2] (#v3 [4.0 5.0 6.0] i)) (get #v3 [4.0 5.0 6.0] "zy") (lists/nth #v3 [4.0 5.0 6.0] 3) (get #m22 [[1.0 2.0] [3.0 4.0]] "x")
5.0 6.0 null 0.0 #v3 [4.0 5.0 6.0] 7.0 4.0 6.0 4.0 6.0 #v2 [6.0 5.0] #err {<invalid value> message "index out of range"} #err {<mismatch type> message "a swizzle requires a #v2, #v3, or #v4 vector"}
19.2.4. Packed vectors
A packed vector holds any number of lanes, also none. A #vf value holds
finite binary64 lanes, the same numbers as a Float. A #vi value holds int64
lanes, the same numbers as an Integer. Use them for long vectors, such as text
embeddings of 384 to 1,536 numbers. The kind predicates vf? and vi? are
their schemas.
| Tag | Lanes | Lane value |
|---|---|---|
#vf |
any count | Float; an Integer lane becomes the nearest Float |
#vi |
any count | Integer; a Float lane is an error |
Each lane uses 8 bytes, in memory and in storage. A #vf of 1,536 lanes uses
12,288 bytes for its lanes. A list of the same 1,536 Floats uses 24,592 bytes
in memory. One cell holds at most 536,870,911 lanes, which is the cell's byte
length limit divided by 8.
count gives the number of lanes. get, lists/nth, a call of
the vector, and a list binding pattern select lanes as for a list. Two vectors
are equal when they have the same tag, the same length and equal lanes. A
-0.0 lane equals 0.0. Vectors of one tag order lane by lane, and a shorter
vector that is a prefix of a longer one comes first. A #vf is never equal to
a #vi or a list.
#vf [0.12 -0.03 0.88] #vf [1 2 3] #vi [1 -2 9007199254740993] #vf [] (count #vf [0.12 -0.03 0.88]) (count #vi []) (= #vf [1.0 -0.0] #vf [1.0 0.0]) (= #vf [1.0 2.0] #vf [1.0 2.0 3.0]) (= #vi [1] #vf [1.0]) (sort [#vf [2.0] #vf [1.0 5.0] #vf [1.0]]) (sort [#vi [3] #vi [-9223372036854775808 1] #vi []]) ((fn [] #vf [0.5 1.5]))
#vf [0.12 -0.03 0.88] #vf [1.0 2.0 3.0] #vi [1 -2 9007199254740993] #vf [] (count #vf [0.12 -0.03 0.88]) (count #vi []) (= #vf [1.0 -0.0] #vf [1.0 0.0]) (= #vf [1.0 2.0] #vf [1.0 2.0 3.0]) (= #vi [1] #vf [1.0]) (sort [#vf [2.0] #vf [1.0 5.0] #vf [1.0]]) (sort [#vi [3] #vi [-9223372036854775808 1] #vi []]) ((fn [] #vf [0.5 1.5]))
#vf [0.12 -0.03 0.88] #vf [1.0 2.0 3.0] #vi [1 -2 9007199254740993] #vf [] 3 0 true false #err {<mismatch type> message "equality requires matching types"} [#vf [1.0] #vf [1.0 5.0] #vf [2.0]] [#vi [] #vi [-9223372036854775808 1] #vi [3]] #vf [0.5 1.5]
(get #vf [0.5 1.5 2.5] 1) (#vi [7 8 9] 2) (lists/nth #vf [0.5 1.5] 0) (get #vi [7 8 9] 5) (get #vf [0.5 1.5] -1) (let [[x y ... rest] #vf [1.0 2.0 3.0 4.0]] [x y rest]) (let [[a b c] #vi [1 2]] [a b c])
1.5 9 0.5 null 1.5 [1.0 2.0 [3.0 4.0]] [1 2 null]
The arithmetic operators take packed vectors:
| Operands | Operators | Result |
|---|---|---|
two #vf of one length |
+ - * / |
lane by lane #vf |
two #vi of one length |
+ - * |
lane by lane #vi |
two #vi of one length |
/ |
lane by lane #vf |
#vf and a number, either order |
* |
scaled #vf |
#vf and a number |
/ |
#vf divided by the number |
#vi and an Integer, either order |
* |
scaled #vi |
#vi and an Integer |
/ |
#vf divided by the Integer |
| one vector | - |
negation |
As for Integers, a #vi result that passes the int64 range gives
#err <integer overflow>. A #vi quotient is a #vf, as an Integer quotient
is a Float. Two vectors of different lengths give #err <invalid value>.
These operands give #err <mismatch type>: a #vf with a #vi, a vector with
a fixed packed value such as #v3, a #vi with a Float, a vector and a number
with + or -, and a number divided by a vector.
(+ #vf [1.0 2.0 3.0] #vf [0.5 0.5 0.5]) (- #vf [1.0 2.0] #vf [0.5 2.0]) (* #vf [1.0 2.0] #vf [3.0 4.0]) (/ #vf [1.0 2.0] #vf [4.0 8.0]) (* 2 #vf [0.5 -0.25]) (/ #vf [1.0 3.0] 2) (- #vf [1.0 -2.0]) (+ #vi [1 2] #vi [10 20] #vi [100 200]) (* #vi [1 -2] 3) (/ #vi [1 3] 2) (- #vi [5 -6])
#vf [1.5 2.5 3.5] #vf [0.5 0.0] #vf [3.0 8.0] #vf [0.25 0.25] #vf [1.0 -0.5] #vf [0.5 1.5] #vf [-1.0 2.0] #vi [111 222] #vi [3 -6] #vf [0.5 1.5] #vi [-5 6]
(+ #vf [1.0 2.0] #vf [1.0]) (+ #vf [1.0] #vi [1]) (* #vi [1] 1.5) (+ #vf [1.0] 1) (+ #vf [1.0 2.0] #v2 [1.0 2.0]) (/ 2 #vf [1.0]) (/ #vf [1.0] 0) (/ #vi [1] 0) (* #vi [9223372036854775807] 2) (* #vf [1e+300] 10000000000.0)
#err {<invalid value> message "packed vectors must have the same length"} #err {<mismatch type> message "packed vector operands must both be #vf or both be #vi"} #err {<mismatch type> message "a #vi combines with Integers only"} #err {<mismatch type> message "packed + requires two vectors of one length"} #err {<mismatch type> message "a packed vector does not combine with a fixed packed value"} #err {<mismatch type> message "a number cannot be divided by a packed vector"} #err {<division float zero> message "division by zero"} #err {<division integer zero> message "division by zero"} #err {<integer overflow> message "integer overflow"} #err {<float overflow> message "floating-point overflow"}
19.2.5. Constants
These constants are packed values, not functions:
| Constant | Value |
|---|---|
:math/vectors/v2-zero, :math/vectors/v3-zero, :math/vectors/v4-zero |
zero vectors |
:math/vectors/v2-one, :math/vectors/v3-one, :math/vectors/v4-one |
vectors of ones |
:math/vectors/right |
#v3 [1.0 0.0 0.0] |
:math/vectors/up |
#v3 [0.0 1.0 0.0] |
:math/vectors/forward |
#v3 [0.0 0.0 -1.0] |
:math/quaternions/identity |
#quat [0.0 0.0 0.0 1.0] |
:math/matrices/identity-22, :math/matrices/identity-33, :math/matrices/identity-44 |
identity matrices |
:math/vectors/v2-zero :math/vectors/v3-zero :math/vectors/v4-zero :math/vectors/v2-one :math/vectors/v3-one :math/vectors/v4-one :math/vectors/right :math/vectors/up :math/vectors/forward :math/quaternions/identity :math/matrices/identity-22 :math/matrices/identity-33 :math/matrices/identity-44 (* :math/matrices/identity-33 #v3 [1 2 3])
:math/vectors/v2-zero :math/vectors/v3-zero :math/vectors/v4-zero :math/vectors/v2-one :math/vectors/v3-one :math/vectors/v4-one :math/vectors/right :math/vectors/up :math/vectors/forward :math/quaternions/identity :math/matrices/identity-22 :math/matrices/identity-33 :math/matrices/identity-44 (* :math/matrices/identity-33 #v3 [1.0 2.0 3.0])
#v2 [0.0 0.0] #v3 [0.0 0.0 0.0] #v4 [0.0 0.0 0.0 0.0] #v2 [1.0 1.0] #v3 [1.0 1.0 1.0] #v4 [1.0 1.0 1.0 1.0] #v3 [1.0 0.0 0.0] #v3 [0.0 1.0 0.0] #v3 [0.0 0.0 -1.0] #quat [0.0 0.0 0.0 1.0] #m22 [[1.0 0.0] [0.0 1.0]] #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]] #m44 [[1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]] #v3 [1.0 2.0 3.0]
19.2.6. math/vectors/v2
Function(math/vectors/v2 x y) → v2
Returns #v2 [x y].
(math/vectors/v2 1 2) (math/vectors/v2 -0.5 1e3) (math/vectors/v2 -0.0 0) (math/vectors/v2 1 "2") (math/vectors/v2 1.7976931348623157e308 5e-324) (math/vectors/v2 1)
(math/vectors/v2 1 2) (math/vectors/v2 -0.5 1000.0) (math/vectors/v2 -0.0 0) (math/vectors/v2 1 "2") (math/vectors/v2 1.7976931348623157e+308 5e-324) (math/vectors/v2 1)
#v2 [1.0 2.0] #v2 [-0.5 1000.0] #v2 [-0.0 0.0] #err {<mismatch type> message "math components must be numbers"} #v2 [1.7976931348623157e+308 5e-324] #err {<arity mismatch> message "wrong number of arguments"}
19.2.7. math/vectors/v3
Function(math/vectors/v3 x y z) → v3
Returns #v3 [x y z].
(math/vectors/v3 1 2 3) (math/vectors/v3 0.1 0.2 0.3) (math/vectors/v3 16777217 0 0) (math/vectors/v3 1 2 null) (math/vectors/v3 1 2 3 4)
#v3 [1.0 2.0 3.0] #v3 [0.1 0.2 0.3] #v3 [16777217.0 0.0 0.0] #err {<mismatch type> message "math components must be numbers"} #err {<arity mismatch> message "wrong number of arguments"}
19.2.8. math/vectors/v4
Function(math/vectors/v4 x y z w) → v4
Returns #v4 [x y z w].
(math/vectors/v4 1 2 3 4) (math/vectors/v4 0 0 0 1) (= (math/vectors/v4 0 0 0 1) #quat [0 0 0 1]) (math/vectors/v4 1 2 3 true)
(math/vectors/v4 1 2 3 4) (math/vectors/v4 0 0 0 1) (= (math/vectors/v4 0 0 0 1) #quat [0.0 0.0 0.0 1.0]) (math/vectors/v4 1 2 3 true)
#v4 [1.0 2.0 3.0 4.0] #v4 [0.0 0.0 0.0 1.0] #err {<mismatch type> message "equality requires matching types"} #err {<mismatch type> message "math components must be numbers"}
19.2.9. math/matrices/m22
Function(math/matrices/m22 m00 m01 m10 m11) → m22
Returns a row-major 2 by 2 matrix.
(math/matrices/m22 1 2 3 4) (math/matrices/m22 1 0 0 1) (= (math/matrices/m22 1 2 3 4) #m22 [[1 2] [3 4]]) (math/matrices/m22 1 2 3)
(math/matrices/m22 1 2 3 4) (math/matrices/m22 1 0 0 1) (= (math/matrices/m22 1 2 3 4) #m22 [[1.0 2.0] [3.0 4.0]]) (math/matrices/m22 1 2 3)
#m22 [[1.0 2.0] [3.0 4.0]] #m22 [[1.0 0.0] [0.0 1.0]] true #err {<arity mismatch> message "wrong number of arguments"}
19.2.10. math/matrices/m33
Function(math/matrices/m33 m00 m01 m02 m10 m11 m12 m20 m21 m22) → m33
Returns a row-major 3 by 3 matrix.
(math/matrices/m33 1 2 3 4 5 6 7 8 9) (math/matrices/m33 1 0 0 0 1 0 0 0 1) (= (math/matrices/m33 1 0 0 0 1 0 0 0 1) #m33 [[1 0 0] [0 1 0] [0 0 1]]) (math/matrices/m33 1 0 0 0 1 0 0 0 "1")
(math/matrices/m33 1 2 3 4 5 6 7 8 9) (math/matrices/m33 1 0 0 0 1 0 0 0 1) (= (math/matrices/m33 1 0 0 0 1 0 0 0 1) #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]]) (math/matrices/m33 1 0 0 0 1 0 0 0 "1")
#m33 [[1.0 2.0 3.0] [4.0 5.0 6.0] [7.0 8.0 9.0]] #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]] true #err {<mismatch type> message "math components must be numbers"}
19.2.11. math/matrices/m44
Function(math/matrices/m44 m00 m01 m02 m03 m10 m11 m12 m13 m20 m21 m22 m23 m30 m31 m32 m33) → m44
Returns a row-major 4 by 4 matrix.
(math/matrices/m44 1 0 0 10 0 1 0 20 0 0 1 30 0 0 0 1) (= (math/matrices/m44 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1) #m44 [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]) (math/matrices/m44 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 1) (math/matrices/m44 1 2 3)
(math/matrices/m44 1 0 0 10 0 1 0 20 0 0 1 30 0 0 0 1) (= (math/matrices/m44 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1) #m44 [[1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (math/matrices/m44 2 0 0 0 0 3 0 0 0 0 4 0 0 0 0 1) (math/matrices/m44 1 2 3)
#m44 [[1.0 0.0 0.0 10.0] [0.0 1.0 0.0 20.0] [0.0 0.0 1.0 30.0] [0.0 0.0 0.0 1.0]] true #m44 [[2.0 0.0 0.0 0.0] [0.0 3.0 0.0 0.0] [0.0 0.0 4.0 0.0] [0.0 0.0 0.0 1.0]] #err {<arity mismatch> message "wrong number of arguments"}
19.2.12. math/quaternions/quaternion
Function(math/quaternions/quaternion x y z w) → quat
Returns #quat [x y z w].
(math/quaternions/quaternion 0 0 0 1) (math/quaternions/quaternion 0.5 0.5 0.5 0.5) (= (math/quaternions/quaternion 0 0 0 1) #v4 [0 0 0 1]) (math/quaternions/quaternion 0 0 1)
(math/quaternions/quaternion 0 0 0 1) (math/quaternions/quaternion 0.5 0.5 0.5 0.5) (= (math/quaternions/quaternion 0 0 0 1) #v4 [0.0 0.0 0.0 1.0]) (math/quaternions/quaternion 0 0 1)
#quat [0.0 0.0 0.0 1.0] #quat [0.5 0.5 0.5 0.5] #err {<mismatch type> message "equality requires matching types"} #err {<arity mismatch> message "wrong number of arguments"}
19.2.13. math/aabbs/aabb
Function(math/aabbs/aabb minimum maximum) → aabb
Returns an AABB from two #v3 values.
(math/aabbs/aabb #v3 [-1 -1 -1] #v3 [1 1 1]) (math/aabbs/aabb #v3 [0 0 0] #v3 [0 0 0]) (= (math/aabbs/aabb #v3 [-1 -2 -3] #v3 [1 2 3]) #aabb [[-1 -2 -3] [1 2 3]]) (math/aabbs/aabb #v3 [0 0 0] #v2 [1 1]) (math/aabbs/aabb [0 0 0] [1 1 1])
(math/aabbs/aabb #v3 [-1.0 -1.0 -1.0] #v3 [1.0 1.0 1.0]) (math/aabbs/aabb #v3 [0.0 0.0 0.0] #v3 [0.0 0.0 0.0]) (= (math/aabbs/aabb #v3 [-1.0 -2.0 -3.0] #v3 [1.0 2.0 3.0]) #aabb [[-1.0 -2.0 -3.0] [1.0 2.0 3.0]]) (math/aabbs/aabb #v3 [0.0 0.0 0.0] #v2 [1.0 1.0]) (math/aabbs/aabb [0 0 0] [1 1 1])
#aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #aabb [[0.0 0.0 0.0] [0.0 0.0 0.0]] true #err {<mismatch type> message "geometry constructor received the wrong math value"} #err {<mismatch type> message "geometry constructor received the wrong math value"}
19.2.14. math/rays/ray
Function(math/rays/ray origin direction) → ray
Returns a ray from two #v3 values. The direction need not have unit length.
(math/rays/ray #v3 [0 0 0] #v3 [1 0 0]) (math/rays/ray #v3 [1 2 3] #v3 [0 0 -2]) (math/rays/ray #v3 [0 0 0] #v4 [1 0 0 0])
(math/rays/ray #v3 [0.0 0.0 0.0] #v3 [1.0 0.0 0.0]) (math/rays/ray #v3 [1.0 2.0 3.0] #v3 [0.0 0.0 -2.0]) (math/rays/ray #v3 [0.0 0.0 0.0] #v4 [1.0 0.0 0.0 0.0])
#ray [[0.0 0.0 0.0] [1.0 0.0 0.0]] #ray [[1.0 2.0 3.0] [0.0 0.0 -2.0]] #err {<mismatch type> message "geometry constructor received the wrong math value"}
19.2.15. math/spheres/sphere
Function(math/spheres/sphere center radius) → sphere
Returns a sphere from a #v3 center and a numeric radius.
(math/spheres/sphere #v3 [0 0 0] 1) (math/spheres/sphere #v3 [1 2 3] 0.5) (math/spheres/sphere #v3 [0 0 0] "1") (math/spheres/sphere #v2 [0 0] 1)
(math/spheres/sphere #v3 [0.0 0.0 0.0] 1) (math/spheres/sphere #v3 [1.0 2.0 3.0] 0.5) (math/spheres/sphere #v3 [0.0 0.0 0.0] "1") (math/spheres/sphere #v2 [0.0 0.0] 1)
#sphere [[0.0 0.0 0.0] 1.0] #sphere [[1.0 2.0 3.0] 0.5] #err {<mismatch type> message "math components must be numbers"} #err {<mismatch type> message "sphere center must be #v3"}
19.2.16. math/planes/plane
Function(math/planes/plane normal distance) → plane
Returns a plane from a #v3 normal and a numeric distance.
(math/planes/plane #v3 [0 1 0] -2) (math/planes/plane #v3 [1 0 0] 0) (math/planes/plane #v3 [0 1 0] null)
(math/planes/plane #v3 [0.0 1.0 0.0] -2) (math/planes/plane #v3 [1.0 0.0 0.0] 0) (math/planes/plane #v3 [0.0 1.0 0.0] null)
#plane [[0.0 1.0 0.0] -2.0] #plane [[1.0 0.0 0.0] 0.0] #err {<mismatch type> message "math components must be numbers"}
19.2.17. math/triangles/triangle
Function(math/triangles/triangle a b c) → triangle
Returns a triangle from three #v3 points.
(math/triangles/triangle #v3 [0 0 0] #v3 [1 0 0] #v3 [0 1 0]) (math/triangles/triangle #v3 [1 1 1] #v3 [2 2 2] #v3 [3 3 3]) (math/triangles/triangle #v3 [0 0 0] #v3 [1 0 0])
(math/triangles/triangle #v3 [0.0 0.0 0.0] #v3 [1.0 0.0 0.0] #v3 [0.0 1.0 0.0]) (math/triangles/triangle #v3 [1.0 1.0 1.0] #v3 [2.0 2.0 2.0] #v3 [3.0 3.0 3.0]) (math/triangles/triangle #v3 [0.0 0.0 0.0] #v3 [1.0 0.0 0.0])
#triangle [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]] #triangle [[1.0 1.0 1.0] [2.0 2.0 2.0] [3.0 3.0 3.0]] #err {<arity mismatch> message "wrong number of arguments"}
19.2.18. math/frustums/frustum
Function(math/frustums/frustum left right bottom top near far) → frustum
Returns a frustum from six #plane values.
(math/frustums/frustum #plane [[1 0 0] 1] #plane [[-1 0 0] 1] #plane [[0 1 0] 1] #plane [[0 -1 0] 1] #plane [[0 0 1] 1] #plane [[0 0 -1] 1]) (= (math/frustums/frustum #plane [[1 0 0] 1] #plane [[-1 0 0] 1] #plane [[0 1 0] 1] #plane [[0 -1 0] 1] #plane [[0 0 1] 1] #plane [[0 0 -1] 1]) #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]]) (math/frustums/frustum #plane [[1 0 0] 1] #plane [[-1 0 0] 1] #plane [[0 1 0] 1] #plane [[0 -1 0] 1] #plane [[0 0 1] 1] #v4 [0 0 -1 1])
(math/frustums/frustum #plane [[1.0 0.0 0.0] 1.0] #plane [[-1.0 0.0 0.0] 1.0] #plane [[0.0 1.0 0.0] 1.0] #plane [[0.0 -1.0 0.0] 1.0] #plane [[0.0 0.0 1.0] 1.0] #plane [[0.0 0.0 -1.0] 1.0]) (= (math/frustums/frustum #plane [[1.0 0.0 0.0] 1.0] #plane [[-1.0 0.0 0.0] 1.0] #plane [[0.0 1.0 0.0] 1.0] #plane [[0.0 -1.0 0.0] 1.0] #plane [[0.0 0.0 1.0] 1.0] #plane [[0.0 0.0 -1.0] 1.0]) #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]]) (math/frustums/frustum #plane [[1.0 0.0 0.0] 1.0] #plane [[-1.0 0.0 0.0] 1.0] #plane [[0.0 1.0 0.0] 1.0] #plane [[0.0 -1.0 0.0] 1.0] #plane [[0.0 0.0 1.0] 1.0] #v4 [0.0 0.0 -1.0 1.0])
#frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]] true #err {<mismatch type> message "geometry constructor received the wrong math value"}
19.2.19. v2?
Function(v2? value) → boolean
Returns true for a #v2 value, a two-dimensional vector, otherwise false.
Another packed kind with the same number of components, or a list of the
components, is not one.
(v2? #v2 [1 2]) (v2? #v4 [1 2 3 4]) (v2? [1 2])
(v2? #v2 [1.0 2.0]) (v2? #v4 [1.0 2.0 3.0 4.0]) (v2? [1 2])
true false false
19.2.20. v3?
Function(v3? value) → boolean
Returns true for a #v3 value, a three-dimensional vector, otherwise
false. Another packed kind with the same number of components, or a list of
the components, is not one. As a schema, v3? accepts a #v3 field value.
(v3? #v3 [1 2 3]) (v3? #v4 [1 2 3 4]) (v3? [1 2 3]) (schemas/valid? {position v3?} {position #v3 [1 2 3]}) (schemas/valid? {position v3?} {position [1 2 3]})
(v3? #v3 [1.0 2.0 3.0]) (v3? #v4 [1.0 2.0 3.0 4.0]) (v3? [1 2 3]) (schemas/valid? {position v3?} {position #v3 [1.0 2.0 3.0]}) (schemas/valid? {position v3?} {position [1 2 3]})
true false false true false
19.2.21. v4?
Function(v4? value) → boolean
Returns true for a #v4 value, a four-dimensional vector, otherwise
false. Another packed kind with the same number of components, or a list of
the components, is not one.
(v4? #v4 [1 2 3 4]) (v4? #quat [0 0 0 1]) (v4? [1 2 3 4])
(v4? #v4 [1.0 2.0 3.0 4.0]) (v4? #quat [0.0 0.0 0.0 1.0]) (v4? [1 2 3 4])
true false false
19.2.22. m22?
Function(m22? value) → boolean
Returns true for a #m22 value, a 2 by 2 matrix, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(m22? #m22 [[1 0] [0 1]]) (m22? #v4 [1 0 0 1]) (m22? [[1 0] [0 1]])
(m22? #m22 [[1.0 0.0] [0.0 1.0]]) (m22? #v4 [1.0 0.0 0.0 1.0]) (m22? [[1 0] [0 1]])
true false false
19.2.23. m33?
Function(m33? value) → boolean
Returns true for a #m33 value, a 3 by 3 matrix, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(m33? #m33 [[1 0 0] [0 1 0] [0 0 1]]) (m33? #triangle [[1 0 0] [0 1 0] [0 0 1]]) (m33? [[1 0 0] [0 1 0] [0 0 1]])
(m33? #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]]) (m33? #triangle [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]]) (m33? [[1 0 0] [0 1 0] [0 0 1]])
true false false
19.2.24. m44?
Function(m44? value) → boolean
Returns true for a #m44 value, a 4 by 4 matrix, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(m44? #m44 [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]) (m44? #m33 [[1 0 0] [0 1 0] [0 0 1]]) (m44? [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]])
(m44? #m44 [[1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (m44? #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]]) (m44? [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]])
true false false
19.2.25. quat?
Function(quat? value) → boolean
Returns true for a #quat value, a quaternion, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(quat? #quat [0 0 0 1]) (quat? #v4 [0 0 0 1]) (quat? [0 0 0 1])
(quat? #quat [0.0 0.0 0.0 1.0]) (quat? #v4 [0.0 0.0 0.0 1.0]) (quat? [0 0 0 1])
true false false
19.2.26. aabb?
Function(aabb? value) → boolean
Returns true for a #aabb value, an AABB, otherwise false. Another packed
kind with the same number of components, or a list of the components, is not
one.
(aabb? #aabb [[0 0 0] [1 1 1]]) (aabb? #ray [[0 0 0] [1 1 1]]) (aabb? [[0 0 0] [1 1 1]])
(aabb? #aabb [[0.0 0.0 0.0] [1.0 1.0 1.0]]) (aabb? #ray [[0.0 0.0 0.0] [1.0 1.0 1.0]]) (aabb? [[0 0 0] [1 1 1]])
true false false
19.2.27. ray?
Function(ray? value) → boolean
Returns true for a #ray value, a ray, otherwise false. Another packed
kind with the same number of components, or a list of the components, is not
one.
(ray? #ray [[0 0 0] [0 0 -1]]) (ray? #aabb [[0 0 0] [0 0 -1]]) (ray? [[0 0 0] [0 0 -1]])
(ray? #ray [[0.0 0.0 0.0] [0.0 0.0 -1.0]]) (ray? #aabb [[0.0 0.0 0.0] [0.0 0.0 -1.0]]) (ray? [[0 0 0] [0 0 -1]])
true false false
19.2.28. sphere?
Function(sphere? value) → boolean
Returns true for a #sphere value, a sphere, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(sphere? #sphere [[0 0 0] 1]) (sphere? #plane [[0 0 0] 1]) (sphere? [[0 0 0] 1])
(sphere? #sphere [[0.0 0.0 0.0] 1.0]) (sphere? #plane [[0.0 0.0 0.0] 1.0]) (sphere? [[0 0 0] 1])
true false false
19.2.29. plane?
Function(plane? value) → boolean
Returns true for a #plane value, a plane, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(plane? #plane [[0 1 0] 0]) (plane? #sphere [[0 1 0] 0]) (plane? [[0 1 0] 0])
(plane? #plane [[0.0 1.0 0.0] 0.0]) (plane? #sphere [[0.0 1.0 0.0] 0.0]) (plane? [[0 1 0] 0])
true false false
19.2.30. triangle?
Function(triangle? value) → boolean
Returns true for a #triangle value, a triangle, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(triangle? #triangle [[0 0 0] [1 0 0] [0 1 0]]) (triangle? #m33 [[0 0 0] [1 0 0] [0 1 0]]) (triangle? [[0 0 0] [1 0 0] [0 1 0]])
(triangle? #triangle [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]]) (triangle? #m33 [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]]) (triangle? [[0 0 0] [1 0 0] [0 1 0]])
true false false
19.2.31. frustum?
Function(frustum? value) → boolean
Returns true for a #frustum value, a frustum, otherwise false. Another
packed kind with the same number of components, or a list of the components,
is not one.
(frustum? #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]]) (frustum? #m44 [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]) (frustum? [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]])
(frustum? #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]]) (frustum? #m44 [[1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (frustum? [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]])
true false false
19.2.32. vf?
Function(vf? value) → boolean
Returns true for a #vf value, a packed vector of Float lanes, otherwise
false. A #vi value, a fixed packed value such as #v3, or a list of
numbers is not one.
(vf? #vf [1.0 2.0]) (vf? #vf []) (vf? #vi [1 2]) (vf? #v2 [1.0 2.0]) (vf? [1.0 2.0])
true true false false false
19.2.33. vi?
Function(vi? value) → boolean
Returns true for a #vi value, a packed vector of Integer lanes, otherwise
false. A #vf value or a list of integers is not one.
19.2.34. math/vectors/vector?
Function(math/vectors/vector? value) → boolean
Returns true for #v2, #v3, and #v4 values.
(math/vectors/vector? #v2 [1 2]) (math/vectors/vector? #v4 [1 2 3 4]) (math/vectors/vector? #quat [0 0 0 1]) (math/vectors/vector? [1 2 3]) (math/vectors/vector? null)
(math/vectors/vector? #v2 [1.0 2.0]) (math/vectors/vector? #v4 [1.0 2.0 3.0 4.0]) (math/vectors/vector? #quat [0.0 0.0 0.0 1.0]) (math/vectors/vector? [1 2 3]) (math/vectors/vector? null)
true true false false false
19.2.35. math/matrices/matrix?
Function(math/matrices/matrix? value) → boolean
Returns true for #m22, #m33, and #m44 values.
(math/matrices/matrix? #m22 [[1 2] [3 4]]) (math/matrices/matrix? #m44 [[1 0 0 10] [0 1 0 20] [0 0 1 30] [0 0 0 1]]) (math/matrices/matrix? #v4 [1 2 3 4]) (math/matrices/matrix? [[1 2] [3 4]])
(math/matrices/matrix? #m22 [[1.0 2.0] [3.0 4.0]]) (math/matrices/matrix? #m44 [[1.0 0.0 0.0 10.0] [0.0 1.0 0.0 20.0] [0.0 0.0 1.0 30.0] [0.0 0.0 0.0 1.0]]) (math/matrices/matrix? #v4 [1.0 2.0 3.0 4.0]) (math/matrices/matrix? [[1 2] [3 4]])
true true false false
19.2.36. math/quaternions/quaternion?
Function(math/quaternions/quaternion? value) → boolean
Returns true for #quat values.
(math/quaternions/quaternion? #quat [0 0 0 1]) (math/quaternions/quaternion? #v4 [0 0 0 1]) (math/quaternions/quaternion? "quat")
(math/quaternions/quaternion? #quat [0.0 0.0 0.0 1.0]) (math/quaternions/quaternion? #v4 [0.0 0.0 0.0 1.0]) (math/quaternions/quaternion? "quat")
true false false
19.2.37. math/aabbs/aabb?
Function(math/aabbs/aabb? value) → boolean
Returns true for #aabb values.
(math/aabbs/aabb? #aabb [[-1 -1 -1] [1 1 1]]) (math/aabbs/aabb? #ray [[0 0 0] [1 0 0]]) (math/aabbs/aabb? {})
(math/aabbs/aabb? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]]) (math/aabbs/aabb? #ray [[0.0 0.0 0.0] [1.0 0.0 0.0]]) (math/aabbs/aabb? {})
true false false
19.2.38. math/rays/ray?
Function(math/rays/ray? value) → boolean
Returns true for #ray values.
(math/rays/ray? #ray [[0 0 0] [1 0 0]]) (math/rays/ray? #aabb [[-1 -1 -1] [1 1 1]]) (math/rays/ray? 1)
(math/rays/ray? #ray [[0.0 0.0 0.0] [1.0 0.0 0.0]]) (math/rays/ray? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]]) (math/rays/ray? 1)
true false false
19.2.39. math/spheres/sphere?
Function(math/spheres/sphere? value) → boolean
Returns true for #sphere values.
(math/spheres/sphere? #sphere [[0 0 0] 1]) (math/spheres/sphere? #plane [[0 0 0] 1]) (math/spheres/sphere? null)
(math/spheres/sphere? #sphere [[0.0 0.0 0.0] 1.0]) (math/spheres/sphere? #plane [[0.0 0.0 0.0] 1.0]) (math/spheres/sphere? null)
true false false
19.2.40. math/planes/plane?
Function(math/planes/plane? value) → boolean
Returns true for #plane values.
(math/planes/plane? #plane [[0 1 0] -2]) (math/planes/plane? #sphere [[0 1 0] 2]) (math/planes/plane? [0 1 0 -2])
(math/planes/plane? #plane [[0.0 1.0 0.0] -2.0]) (math/planes/plane? #sphere [[0.0 1.0 0.0] 2.0]) (math/planes/plane? [0 1 0 -2])
true false false
19.2.41. math/triangles/triangle?
Function(math/triangles/triangle? value) → boolean
Returns true for #triangle values.
(math/triangles/triangle? #triangle [[0 0 0] [1 0 0] [0 1 0]]) (math/triangles/triangle? #m33 [[0 0 0] [1 0 0] [0 1 0]]) (math/triangles/triangle? false)
(math/triangles/triangle? #triangle [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]]) (math/triangles/triangle? #m33 [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]]) (math/triangles/triangle? false)
true false false
19.2.42. math/frustums/frustum?
Function(math/frustums/frustum? value) → boolean
Returns true for #frustum values.
(math/frustums/frustum? #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]]) (math/frustums/frustum? #aabb [[-1 -1 -1] [1 1 1]]) (math/frustums/frustum? [])
(math/frustums/frustum? #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]]) (math/frustums/frustum? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]]) (math/frustums/frustum? [])
true false false
19.2.43. math/vectors/swizzle
Function(math/vectors/swizzle vector pattern) → float
v2
v3
v4
Returns the components of a #v2, #v3 or #v4 that pattern names, in
its order. The pattern is one to four letters, all from xyzw or all from
rgba, and letters can repeat. One letter returns a Float; two to four
return the vector of that width. (v pattern) is the same swizzle. A literal
pattern compiles to direct component reads.
A pattern that mixes xyzw and rgba, has another letter, is empty or longer
than four letters, or names a component the vector does not have gives
#err <invalid value>. A value that is not a vector gives
#err <mismatch type>.
(math/vectors/swizzle #v3 [1 2 3] "zyx") (math/vectors/swizzle #v4 [1 2 3 4] "rgb") (math/vectors/swizzle #v2 [1 2] "yyxx") (math/vectors/swizzle #v3 [1 2 3] "y") (#v3 [1 2 3] "xz") (let [color #v4 [0.5 0.25 1 1]] (color "bgr"))
(math/vectors/swizzle #v3 [1.0 2.0 3.0] "zyx") (math/vectors/swizzle #v4 [1.0 2.0 3.0 4.0] "rgb") (math/vectors/swizzle #v2 [1.0 2.0] "yyxx") (math/vectors/swizzle #v3 [1.0 2.0 3.0] "y") (#v3 [1.0 2.0 3.0] "xz") (let [color #v4 [0.5 0.25 1.0 1.0]] (color "bgr"))
#v3 [3.0 2.0 1.0] #v3 [1.0 2.0 3.0] #v4 [2.0 2.0 1.0 1.0] 2.0 #v2 [1.0 3.0] #v3 [1.0 0.25 0.5]
(math/vectors/swizzle #v2 [1 2] "xz") (math/vectors/swizzle #v3 [1 2 3] "xg") (math/vectors/swizzle #v3 [1 2 3] "") (math/vectors/swizzle #v3 [1 2 3] "xyzwx") (math/vectors/swizzle #v3 [1 2 3] "xq") (math/vectors/swizzle #quat [0 0 0 1] "x") (math/vectors/swizzle #v3 [1 2 3] 0) (math/vectors/swizzle #v3 [1 2 3])
(math/vectors/swizzle #v2 [1.0 2.0] "xz") (math/vectors/swizzle #v3 [1.0 2.0 3.0] "xg") (math/vectors/swizzle #v3 [1.0 2.0 3.0] "") (math/vectors/swizzle #v3 [1.0 2.0 3.0] "xyzwx") (math/vectors/swizzle #v3 [1.0 2.0 3.0] "xq") (math/vectors/swizzle #quat [0.0 0.0 0.0 1.0] "x") (math/vectors/swizzle #v3 [1.0 2.0 3.0] 0) (math/vectors/swizzle #v3 [1.0 2.0 3.0])
#err {<invalid value> message "a swizzle names a component the vector does not have"} #err {<invalid value> message "a swizzle cannot mix xyzw and rgba"} #err {<invalid value> message "a swizzle names one to four components"} #err {<invalid value> message "a swizzle names one to four components"} #err {<invalid value> message "a swizzle uses the letters xyzw or rgba"} #err {<mismatch type> message "a swizzle requires a #v2, #v3, or #v4 vector"} #err {<mismatch type> message "a swizzle pattern must be a string"} #err {<arity mismatch> message "wrong number of arguments"}
19.2.44. math/vectors/vf
Function(math/vectors/vf numbers) → vf
Returns a #vf with the numbers of a list as its lanes. An Integer becomes the
nearest Float. A #vi argument becomes a #vf with the same numbers, and a
#vf argument is returned as it is.
(math/vectors/vf [1 2.5 -3]) (math/vectors/vf []) (math/vectors/vf #vi [1 2]) (math/vectors/vf [1 "two"])
#vf [1.0 2.5 -3.0] #vf [] #vf [1.0 2.0] #err {<mismatch type> message "math components must be numbers"}
19.2.45. math/vectors/vi
Function(math/vectors/vi integers) → vi
Returns a #vi with the Integers of a list as its lanes. A Float in the list
is an error. A #vi argument is returned as it is.
(math/vectors/vi [1 -2 9007199254740993]) (math/vectors/vi []) (math/vectors/vi [1.0])
#vi [1 -2 9007199254740993] #vi [] #err {<mismatch type> message "#vi lanes must be integers"}
19.2.46. math/vectors/normalize
Function(math/vectors/normalize vector) → v2
v3
v4
vf
Returns a vector with unit length. The vector is a #v2, #v3, #v4 or
#vf. Only the zero vector returns a zero
vector. The vector is divided by its largest component magnitude first, so
no intermediate value leaves the Float range for a finite input.
(math/vectors/normalize #v3 [3 0 4]) (math/vectors/normalize #v2 [0 -2]) (math/vectors/normalize #v4 [1 1 1 1]) (math/vectors/normalize #v3 [0 0 0]) (math/vectors/normalize #v3 [3e38 0 0]) (math/vectors/normalize #quat [0 0 0 1]) (math/vectors/normalize #vf [3 0 4 0])
(math/vectors/normalize #v3 [3.0 0.0 4.0]) (math/vectors/normalize #v2 [0.0 -2.0]) (math/vectors/normalize #v4 [1.0 1.0 1.0 1.0]) (math/vectors/normalize #v3 [0.0 0.0 0.0]) (math/vectors/normalize #v3 [3e+38 0.0 0.0]) (math/vectors/normalize #quat [0.0 0.0 0.0 1.0]) (math/vectors/normalize #vf [3.0 0.0 4.0 0.0])
#v3 [0.6 0.0 0.8] #v2 [0.0 -1.0] #v4 [0.5 0.5 0.5 0.5] #v3 [0.0 0.0 0.0] #v3 [1.0 0.0 0.0] #err {<mismatch type> message "normalize requires a vector"} #vf [0.6 0.0 0.8 0.0]
19.2.47. math/vectors/transform
Function(math/vectors/transform point matrix) → v3
Transforms a #v3 point with a #m44 matrix. The operation uses an implicit
fourth component of one.
(math/vectors/transform #v3 [1 2 3] #m44 [[1 0 0 10] [0 1 0 20] [0 0 1 30] [0 0 0 1]]) (math/vectors/transform #v3 [1 1 1] #m44 [[2 0 0 0] [0 3 0 0] [0 0 4 0] [0 0 0 1]]) (math/vectors/transform #v3 [0 0 0] #m44 [[-1 0 0 0] [0 -1 0 0] [0 0 1 0] [0 0 0 1]]) (math/vectors/transform #v3 [1e308 0 0] #m44 [[10 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]) (math/vectors/transform #v4 [1 2 3 1] #m44 [[1 0 0 10] [0 1 0 20] [0 0 1 30] [0 0 0 1]]) (math/vectors/transform #v3 [1e308 1e308 0] #m44 [[10 -10 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]) (math/vectors/transform #v3 [1.7976931348623157e308 -1.7976931348623157e308 0] #m44 [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]])
(math/vectors/transform #v3 [1.0 2.0 3.0] #m44 [[1.0 0.0 0.0 10.0] [0.0 1.0 0.0 20.0] [0.0 0.0 1.0 30.0] [0.0 0.0 0.0 1.0]]) (math/vectors/transform #v3 [1.0 1.0 1.0] #m44 [[2.0 0.0 0.0 0.0] [0.0 3.0 0.0 0.0] [0.0 0.0 4.0 0.0] [0.0 0.0 0.0 1.0]]) (math/vectors/transform #v3 [0.0 0.0 0.0] #m44 [[-1.0 0.0 0.0 0.0] [0.0 -1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (math/vectors/transform #v3 [1e+308 0.0 0.0] #m44 [[10.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (math/vectors/transform #v4 [1.0 2.0 3.0 1.0] #m44 [[1.0 0.0 0.0 10.0] [0.0 1.0 0.0 20.0] [0.0 0.0 1.0 30.0] [0.0 0.0 0.0 1.0]]) (math/vectors/transform #v3 [1e+308 1e+308 0.0] #m44 [[10.0 -10.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (math/vectors/transform #v3 [1.7976931348623157e+308 -1.7976931348623157e+308 0.0] #m44 [[1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]])
#v3 [11.0 22.0 33.0] #v3 [2.0 3.0 4.0] #v3 [0.0 0.0 0.0] #err {<float overflow> message "floating-point overflow"} #err {<mismatch type> message "point transform requires #v3"} #err {<float overflow> message "floating-point overflow"} #v3 [1.7976931348623157e+308 -1.7976931348623157e+308 0.0]
19.2.48. math/aabbs/transform
Function(math/aabbs/transform box matrix) → aabb
Transforms an AABB with a #m44 matrix and returns its axis-aligned bounds.
(math/aabbs/transform #aabb [[-1 -2 -3] [1 2 3]] #m44 [[1 0 0 10] [0 1 0 20] [0 0 1 30] [0 0 0 1]]) (math/aabbs/transform #aabb [[-1 -1 -1] [1 1 1]] #m44 [[0 -1 0 0] [1 0 0 0] [0 0 1 0] [0 0 0 1]]) (math/aabbs/transform #aabb [[0 0 0] [1 1 1]] #m44 [[2 0 0 0] [0 3 0 0] [0 0 4 0] [0 0 0 1]]) (math/aabbs/transform #aabb [[-1e308 0 0] [1e308 0 0]] #m44 [[10 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]) (math/aabbs/transform #aabb [[-1 -1 -1] [1 1 1]] #m33 [[1 0 0] [0 1 0] [0 0 1]]) (math/aabbs/transform #aabb [[-1.7976931348623157e308 0 0] [1.7976931348623157e308 1 1]] #m44 [[1 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]]) (math/aabbs/transform #aabb [[0 0 0] [1e308 1 1]] #m44 [[10 0 0 0] [0 1 0 0] [0 0 1 0] [0 0 0 1]])
(math/aabbs/transform #aabb [[-1.0 -2.0 -3.0] [1.0 2.0 3.0]] #m44 [[1.0 0.0 0.0 10.0] [0.0 1.0 0.0 20.0] [0.0 0.0 1.0 30.0] [0.0 0.0 0.0 1.0]]) (math/aabbs/transform #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #m44 [[0.0 -1.0 0.0 0.0] [1.0 0.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (math/aabbs/transform #aabb [[0.0 0.0 0.0] [1.0 1.0 1.0]] #m44 [[2.0 0.0 0.0 0.0] [0.0 3.0 0.0 0.0] [0.0 0.0 4.0 0.0] [0.0 0.0 0.0 1.0]]) (math/aabbs/transform #aabb [[-1e+308 0.0 0.0] [1e+308 0.0 0.0]] #m44 [[10.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (math/aabbs/transform #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #m33 [[1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0]]) (math/aabbs/transform #aabb [[-1.7976931348623157e+308 0.0 0.0] [1.7976931348623157e+308 1.0 1.0]] #m44 [[1.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]]) (math/aabbs/transform #aabb [[0.0 0.0 0.0] [1e+308 1.0 1.0]] #m44 [[10.0 0.0 0.0 0.0] [0.0 1.0 0.0 0.0] [0.0 0.0 1.0 0.0] [0.0 0.0 0.0 1.0]])
#aabb [[9.0 18.0 27.0] [11.0 22.0 33.0]] #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #aabb [[0.0 0.0 0.0] [2.0 3.0 4.0]] #err {<float overflow> message "floating-point overflow"} #err {<mismatch type> message "AABB transform requires #m44"} #aabb [[-1.7976931348623157e+308 0.0 0.0] [1.7976931348623157e+308 1.0 1.0]] #err {<float overflow> message "floating-point overflow"}
19.2.49. math/rays/intersect-sphere
Function(math/rays/intersect-sphere ray sphere) → list
null
Returns the near and far ray distances. Returns null when there is no hit.
A zero ray direction does not intersect.
(math/rays/intersect-sphere #ray [[-2 0 0] [1 0 0]] #sphere [[0 0 0] 1]) (math/rays/intersect-sphere #ray [[-2 1 0] [1 0 0]] #sphere [[0 0 0] 1]) (math/rays/intersect-sphere #ray [[-2 2 0] [1 0 0]] #sphere [[0 0 0] 1]) (math/rays/intersect-sphere #ray [[0 0 0] [1 0 0]] #sphere [[0 0 0] 1]) (math/rays/intersect-sphere #ray [[2 0 0] [1 0 0]] #sphere [[0 0 0] 1]) (math/rays/intersect-sphere #ray [[0 0 0] [0 0 0]] #sphere [[0 0 0] 1]) (math/rays/intersect-sphere #sphere [[0 0 0] 1] #ray [[-2 0 0] [1 0 0]]) (math/rays/intersect-sphere #ray [[-1e300 0 0] [1 0 0]] #sphere [[0 0 0] 1])
(math/rays/intersect-sphere #ray [[-2.0 0.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/rays/intersect-sphere #ray [[-2.0 1.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/rays/intersect-sphere #ray [[-2.0 2.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/rays/intersect-sphere #ray [[0.0 0.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/rays/intersect-sphere #ray [[2.0 0.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/rays/intersect-sphere #ray [[0.0 0.0 0.0] [0.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/rays/intersect-sphere #sphere [[0.0 0.0 0.0] 1.0] #ray [[-2.0 0.0 0.0] [1.0 0.0 0.0]]) (math/rays/intersect-sphere #ray [[-1e+300 0.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0])
[[1.0 3.0] [2.0 2.0] null [-1.0 1.0] null null #err {<mismatch type> message "ray-sphere intersection requires #ray"} #err {<float overflow> message "floating-point overflow"}]
19.2.50. math/noise/perlin2
Function(math/noise/perlin2 point) → float
Returns deterministic two-dimensional Perlin noise for a #v2 point. The
noise hash reduces each coordinate modulo 289. Past about 1e160 that
reduction can pass the Float range, which produces #err <float overflow>.
(math/noise/perlin2 #v2 [0 0]) (math/noise/perlin2 #v2 [1 1]) (math/noise/perlin2 #v2 [0.25 -0.75]) (= (math/noise/perlin2 #v2 [0.5 0.5]) (math/noise/perlin2 #v2 [0.5 0.5])) (math/noise/perlin2 #v3 [0 0 0]) (math/noise/perlin2 #v2 [1e300 0.5]) (math/noise/perlin2 #v2 [1e160 0.5])
(math/noise/perlin2 #v2 [0.0 0.0]) (math/noise/perlin2 #v2 [1.0 1.0]) (math/noise/perlin2 #v2 [0.25 -0.75]) (= (math/noise/perlin2 #v2 [0.5 0.5]) (math/noise/perlin2 #v2 [0.5 0.5])) (math/noise/perlin2 #v3 [0.0 0.0 0.0]) (math/noise/perlin2 #v2 [1e+300 0.5]) (math/noise/perlin2 #v2 [1e+160 0.5])
0.0 0.0 -0.2422113204513118 true #err {<mismatch type> message "perlin2 requires #v2"} #err {<float overflow> message "floating-point overflow"} 0.253452410121311
19.2.51. math/intersects?
Function(math/intersects? a b) → boolean
Tests these unordered geometry pairs: AABB with AABB, ray, sphere, or frustum;
ray with sphere, plane, or triangle; and sphere with sphere, plane, or frustum.
Other pairs produce #err <mismatch type>. A test value that passes the Float
range, such as the distance along a ray almost parallel to a plane, produces
#err <float overflow>.
(math/intersects? #aabb [[-1 -1 -1] [1 1 1]] #aabb [[1 1 1] [2 2 2]]) (math/intersects? #aabb [[-1 -1 -1] [1 1 1]] #aabb [[1.5 0 0] [2 1 1]]) (math/intersects? #aabb [[-1 -1 -1] [1 1 1]] #ray [[-2 0 0] [1 0 0]]) (math/intersects? #ray [[-2 0 0] [-1 0 0]] #aabb [[-1 -1 -1] [1 1 1]]) (math/intersects? #aabb [[-1 -1 -1] [1 1 1]] #sphere [[2 0 0] 1]) (math/intersects? #sphere [[2.5 0 0] 1] #aabb [[-1 -1 -1] [1 1 1]]) (math/intersects? #aabb [[-1 -1 -1] [1 1 1]] #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]]) (math/intersects? #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]] #aabb [[3 3 3] [4 4 4]]) (math/intersects? #ray [[-2 1 0] [1 0 0]] #sphere [[0 0 0] 1]) (math/intersects? #sphere [[0 0 0] 1] #ray [[-2 2 0] [1 0 0]]) (math/intersects? #ray [[0 5 0] [0 -1 0]] #plane [[0 1 0] 0]) (math/intersects? #ray [[0 5 0] [1 0 0]] #plane [[0 1 0] 0]) (math/intersects? #ray [[0.25 0.25 1] [0 0 -1]] #triangle [[0 0 0] [1 0 0] [0 1 0]]) (math/intersects? #triangle [[0 0 0] [1 0 0] [0 1 0]] #ray [[2 2 1] [0 0 -1]]) (math/intersects? #sphere [[0 0 0] 1] #sphere [[2 0 0] 1]) (math/intersects? #sphere [[0 0 0] 1] #sphere [[2.5 0 0] 1]) (math/intersects? #sphere [[0 1 0] 1] #plane [[0 1 0] 0]) (math/intersects? #plane [[0 1 0] 0] #sphere [[0 3 0] 1]) (math/intersects? #sphere [[0 0 0] 1] #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]]) (math/intersects? #frustum [[1 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]] #sphere [[3 0 0] 1]) (math/intersects? #v3 [0 0 0] #v3 [1 1 1]) (math/intersects? #plane [[0 1 0] 0] #plane [[1 0 0] 0]) (math/intersects? #aabb [[-1 -1 -1] [1 1 1]] [0 0 0]) (math/intersects? #ray [[-1e308 0 0] [1e-300 0 0]] #aabb [[-1 -1 -1] [1 1 1]]) (math/intersects? #ray [[-1.7976931348623157e308 0 0] [1 0 0]] #aabb [[-1 -1 -1] [1 1 1]]) (math/intersects? #ray [[0 1e308 0] [0 -1e-300 0]] #plane [[0 1 0] 0]) (math/intersects? #ray [[0 1.7976931348623157e308 0] [0 -1 0]] #plane [[0 1 0] 0]) (math/intersects? #ray [[0 0 1] [0 0 -1]] #triangle [[-1e308 0 0] [1e308 0 0] [0 1 0]]) (math/intersects? #ray [[-1e300 0 0] [1 0 0]] #sphere [[0 0 0] 1]) (math/intersects? #sphere [[-1e300 0 0] 1] #sphere [[1e300 0 0] 1]) (math/intersects? #aabb [[-1 -1 -1] [1 1 1]] #sphere [[1e300 0 0] 1]) (math/intersects? #plane [[0 1 0] 0] #sphere [[0 1e300 0] 1]) (math/intersects? #sphere [[0 0 0] 1] #frustum [[1e300 1e300 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]]) (math/intersects? #frustum [[1e300 0 0 1] [-1 0 0 1] [0 1 0 1] [0 -1 0 1] [0 0 1 1] [0 0 -1 1]] #aabb [[-1e10 -1 -1] [1e10 1 1]])
(math/intersects? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #aabb [[1.0 1.0 1.0] [2.0 2.0 2.0]]) (math/intersects? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #aabb [[1.5 0.0 0.0] [2.0 1.0 1.0]]) (math/intersects? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #ray [[-2.0 0.0 0.0] [1.0 0.0 0.0]]) (math/intersects? #ray [[-2.0 0.0 0.0] [-1.0 0.0 0.0]] #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]]) (math/intersects? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #sphere [[2.0 0.0 0.0] 1.0]) (math/intersects? #sphere [[2.5 0.0 0.0] 1.0] #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]]) (math/intersects? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]]) (math/intersects? #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]] #aabb [[3.0 3.0 3.0] [4.0 4.0 4.0]]) (math/intersects? #ray [[-2.0 1.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/intersects? #sphere [[0.0 0.0 0.0] 1.0] #ray [[-2.0 2.0 0.0] [1.0 0.0 0.0]]) (math/intersects? #ray [[0.0 5.0 0.0] [0.0 -1.0 0.0]] #plane [[0.0 1.0 0.0] 0.0]) (math/intersects? #ray [[0.0 5.0 0.0] [1.0 0.0 0.0]] #plane [[0.0 1.0 0.0] 0.0]) (math/intersects? #ray [[0.25 0.25 1.0] [0.0 0.0 -1.0]] #triangle [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]]) (math/intersects? #triangle [[0.0 0.0 0.0] [1.0 0.0 0.0] [0.0 1.0 0.0]] #ray [[2.0 2.0 1.0] [0.0 0.0 -1.0]]) (math/intersects? #sphere [[0.0 0.0 0.0] 1.0] #sphere [[2.0 0.0 0.0] 1.0]) (math/intersects? #sphere [[0.0 0.0 0.0] 1.0] #sphere [[2.5 0.0 0.0] 1.0]) (math/intersects? #sphere [[0.0 1.0 0.0] 1.0] #plane [[0.0 1.0 0.0] 0.0]) (math/intersects? #plane [[0.0 1.0 0.0] 0.0] #sphere [[0.0 3.0 0.0] 1.0]) (math/intersects? #sphere [[0.0 0.0 0.0] 1.0] #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]]) (math/intersects? #frustum [[1.0 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]] #sphere [[3.0 0.0 0.0] 1.0]) (math/intersects? #v3 [0.0 0.0 0.0] #v3 [1.0 1.0 1.0]) (math/intersects? #plane [[0.0 1.0 0.0] 0.0] #plane [[1.0 0.0 0.0] 0.0]) (math/intersects? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] [0 0 0]) (math/intersects? #ray [[-1e+308 0.0 0.0] [1e-300 0.0 0.0]] #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]]) (math/intersects? #ray [[-1.7976931348623157e+308 0.0 0.0] [1.0 0.0 0.0]] #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]]) (math/intersects? #ray [[0.0 1e+308 0.0] [0.0 -1e-300 0.0]] #plane [[0.0 1.0 0.0] 0.0]) (math/intersects? #ray [[0.0 1.7976931348623157e+308 0.0] [0.0 -1.0 0.0]] #plane [[0.0 1.0 0.0] 0.0]) (math/intersects? #ray [[0.0 0.0 1.0] [0.0 0.0 -1.0]] #triangle [[-1e+308 0.0 0.0] [1e+308 0.0 0.0] [0.0 1.0 0.0]]) (math/intersects? #ray [[-1e+300 0.0 0.0] [1.0 0.0 0.0]] #sphere [[0.0 0.0 0.0] 1.0]) (math/intersects? #sphere [[-1e+300 0.0 0.0] 1.0] #sphere [[1e+300 0.0 0.0] 1.0]) (math/intersects? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #sphere [[1e+300 0.0 0.0] 1.0]) (math/intersects? #plane [[0.0 1.0 0.0] 0.0] #sphere [[0.0 1e+300 0.0] 1.0]) (math/intersects? #sphere [[0.0 0.0 0.0] 1.0] #frustum [[1e+300 1e+300 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]]) (math/intersects? #frustum [[1e+300 0.0 0.0 1.0] [-1.0 0.0 0.0 1.0] [0.0 1.0 0.0 1.0] [0.0 -1.0 0.0 1.0] [0.0 0.0 1.0 1.0] [0.0 0.0 -1.0 1.0]] #aabb [[-10000000000.0 -1.0 -1.0] [10000000000.0 1.0 1.0]])
true false true false true false true false true false true false true false true false true false true false #err {<mismatch type> message "unsupported geometry intersection pair"} #err {<mismatch type> message "unsupported geometry intersection pair"} #err {<mismatch type> message "intersects? requires geometry values"} #err {<float overflow> message "floating-point overflow"} true #err {<float overflow> message "floating-point overflow"} true #err {<float overflow> message "floating-point overflow"} #err {<float overflow> message "floating-point overflow"} #err {<float overflow> message "floating-point overflow"} #err {<float overflow> message "floating-point overflow"} #err {<float overflow> message "floating-point overflow"} #err {<float overflow> message "floating-point overflow"} #err {<float overflow> message "floating-point overflow"}
19.2.52. math/contains?
Function(math/contains? container point) → boolean
Returns true when an AABB or sphere contains a #v3 point. A point on the
boundary is contained. Other containers produce #err <mismatch type>. A
squared distance from a sphere's center past the Float range produces
#err <float overflow>.
(math/contains? #aabb [[-1 -1 -1] [1 1 1]] #v3 [0 0 0]) (math/contains? #aabb [[-1 -1 -1] [1 1 1]] #v3 [1 1 1]) (math/contains? #aabb [[-1 -1 -1] [1 1 1]] #v3 [1 1 1.5]) (math/contains? #sphere [[0 0 0] 1] #v3 [1 0 0]) (math/contains? #sphere [[0 0 0] 1] #v3 [1 1 0]) (math/contains? #ray [[0 0 0] [1 0 0]] #v3 [0 0 0]) (math/contains? #aabb [[-1 -1 -1] [1 1 1]] #v2 [0 0]) (math/contains? #sphere [[0 0 0] 1] #v3 [1e300 0 0])
(math/contains? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #v3 [0.0 0.0 0.0]) (math/contains? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #v3 [1.0 1.0 1.0]) (math/contains? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #v3 [1.0 1.0 1.5]) (math/contains? #sphere [[0.0 0.0 0.0] 1.0] #v3 [1.0 0.0 0.0]) (math/contains? #sphere [[0.0 0.0 0.0] 1.0] #v3 [1.0 1.0 0.0]) (math/contains? #ray [[0.0 0.0 0.0] [1.0 0.0 0.0]] #v3 [0.0 0.0 0.0]) (math/contains? #aabb [[-1.0 -1.0 -1.0] [1.0 1.0 1.0]] #v2 [0.0 0.0]) (math/contains? #sphere [[0.0 0.0 0.0] 1.0] #v3 [1e+300 0.0 0.0])
true true false true false #err {<mismatch type> message "contains? requires an AABB or sphere"} #err {<mismatch type> message "contains? point must be #v3"} #err {<float overflow> message "floating-point overflow"}