-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathFloat.carp
More file actions
180 lines (161 loc) · 5.93 KB
/
Float.carp
File metadata and controls
180 lines (161 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
(system-include "carp_float.h")
(doc Float "is a smaller numerical floating point type. Its literals are
suffixed with `f`.")
(defmodule Float
(doc pi "the mathematical constant π (3.1415926536f).")
(def pi 3.1415926536f)
(doc MAX "the maximum value a Float can hold.")
(register MAX Float "CARP_FLT_MAX")
(implements MAX MAX)
(doc neg "negates a Float.")
(register neg (Fn [Float] Float))
(doc + "adds two floats.")
(register + (Fn [Float Float] Float))
(doc - "subtracts `b` from `a`.")
(register - (Fn [Float Float] Float))
(doc * "multiplies two floats.")
(register * (Fn [Float Float] Float))
(doc / "divides `a` by `b`.")
(register / (Fn [Float Float] Float))
(doc to-int "converts a Float to an Int by truncating toward zero.")
(register to-int (Fn [Float] Int))
(doc to-bytes "returns the raw bit representation of a Float as an Int.")
(register to-bytes (Fn [Float] Int))
(doc from-int "converts an Int to a Float.")
(register from-int (Fn [Int] Float))
(doc copy "copies a Float.")
(register copy (Fn [(Ref Float)] Float))
(doc round "rounds a Float to the nearest integer.")
(register round (Fn [Float] Int))
(doc = "returns true if `a` equals `b`.")
(register = (Fn [Float Float] Bool))
(doc < "returns true if `a` is less than `b`.")
(register < (Fn [Float Float] Bool))
(doc > "returns true if `a` is greater than `b`.")
(register > (Fn [Float Float] Bool))
(implements pi Float.pi)
(implements + Float.+)
(implements - Float.-)
(implements * Float.*)
(implements / Float./)
(implements < Float.<)
(implements > Float.>)
(implements = Float.=)
(implements copy Float.copy)
(implements neg Float.neg)
(implements to-int Float.to-int)
(implements from-int Float.from-int)
(doc clamp "clamps `val` to the range [`min`, `max`].")
(defn clamp [min, max, val]
(if (> val max)
max
(if (< val min)
min
val)))
(doc sign "Returns `-1.0f` if `x` is negative, `0.0f` if zero, or `1.0f` if positive.
Note: returns `0.0f` for NaN inputs.")
(defn sign [x]
(if (> x 0.0f) 1.0f (if (< x 0.0f) -1.0f 0.0f)))
(doc approx "checks whether `x` and `y` are approximately equal.
The margin of error is 0.00001.")
(defn approx [x y]
(Generics.approx x y))
(register copy (Fn [(Ref Float)] Float))
(doc abs "returns the absolute value of `a`.")
(register abs (Fn [Float] Float))
(doc acos "returns the arc cosine of `a` in radians.")
(register acos (Fn [Float] Float))
(doc asin "returns the arc sine of `a` in radians.")
(register asin (Fn [Float] Float))
(doc atan "returns the arc tangent of `a` in radians.")
(register atan (Fn [Float] Float))
(doc atan2 "returns the arc tangent of `y`/`x`, using the signs of both to determine the quadrant.")
(register atan2 (Fn [Float Float] Float))
(doc ceil "rounds `a` up to the nearest integer.")
(register ceil (Fn [Float] Float))
(doc cos "returns the cosine of `a` (in radians).")
(register cos (Fn [Float] Float))
(doc cosh "returns the hyperbolic cosine of `a`.")
(register cosh (Fn [Float] Float))
(doc exp "returns e raised to the power of `a`.")
(register exp (Fn [Float] Float))
(doc floor "rounds `a` down to the nearest integer.")
(register floor (Fn [Float] Float))
(doc frexp "splits `a` into a normalized fraction and an exponent, storing the exponent in `exp`.")
(register frexp (Fn [Float (Ref Int)] Float))
(doc ldexp "returns `a` multiplied by 2 raised to the power of `exp`.")
(register ldexp (Fn [Float Int] Float))
(doc log "returns the natural logarithm of `a`.")
(register log (Fn [Float] Float))
(doc log10 "returns the base-10 logarithm of `a`.")
(register log10 (Fn [Float] Float))
(doc mod "returns the floating-point remainder of dividing `a` by `b`.")
(register mod (Fn [Float Float] Float))
(doc modf "splits `a` into integer and fractional parts, storing the integer part in `int-part`.")
(register modf (Fn [Float (Ref Float)] Float))
(doc pow "returns `a` raised to the power of `b`.")
(register pow (Fn [Float Float] Float))
(doc sin "returns the sine of `a` (in radians).")
(register sin (Fn [Float] Float))
(doc sinh "returns the hyperbolic sine of `a`.")
(register sinh (Fn [Float] Float))
(doc sqrt "returns the square root of `a`.")
(register sqrt (Fn [Float] Float))
(doc tan "returns the tangent of `a` (in radians).")
(register tan (Fn [Float] Float))
(doc tanh "returns the hyperbolic tangent of `a`.")
(register tanh (Fn [Float] Float))
(implements abs Float.abs)
(implements acos Float.acos)
(implements asin Float.asin)
(implements atan Float.atan)
(implements atan2 Float.atan2)
(implements ceil Float.ceil)
(implements cos Float.cos)
(implements cosh Float.cosh)
(implements exp Float.exp)
(implements floor Float.floor)
(implements frexp Float.frexp)
(implements ldexp Float.ldexp)
(implements log Float.log)
(implements log10 Float.log10)
(implements mod Float.mod)
(implements modf Float.modf)
(implements pow Float.pow)
(implements sin Float.sin)
(implements sinh Float.sinh)
(implements sqrt Float.sqrt)
(implements tan Float.tan)
(implements tanh Float.tanh)
(doc zero "returns `0.0f`.")
(defn zero []
0.0f)
(implements zero Float.zero)
(doc inc "increments a Float by 1.0f.")
(defn inc [x]
(+ 1.0f x))
(implements inc Float.inc)
(doc dec "decrements a Float by 1.0f.")
(defn dec [x]
(- x 1.0f))
(implements dec Float.dec)
(doc add-ref "adds two Float values passed as references.")
(defn add-ref [x y]
(Float.+ @x @y))
(implements add-ref Float.add-ref)
(doc lerp "Linearly interpolates between `a` and `b` by factor `t`.
Returns `a` when `t` is `0.0f` and `b` when `t` is `1.0f`.")
(defn lerp [a b t]
(+ a (* (- b a) t)))
)
(defmodule FloatRef
(defn = [a b]
(Float.= @a @b))
(implements = FloatRef.=)
(defn < [a b]
(Float.< @a @b))
(implements < FloatRef.<)
(defn > [a b]
(Float.> @a @b))
(implements > FloatRef.>)
)