-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
math.cr
264 lines (224 loc) · 5.86 KB
/
math.cr
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
require "./libm"
module Math
extend self
PI = 3.14159265358979323846
E = LibM.exp_f64(1.0)
LOG2 = LibM.log_f64(2.0)
LOG10 = LibM.log_f64(10.0)
{% for name in %w(acos acosh asin asinh atan atanh cbrt cos cosh erf erfc exp
exp2 expm1 ilogb log log10 log1p log2 logb sin sinh sqrt tan tanh) %}
# Calculates the {{name.id}} of *value*.
def {{name.id}}(value : Float32)
LibM.{{name.id}}_f32(value)
end
# ditto
def {{name.id}}(value : Float64)
LibM.{{name.id}}_f64(value)
end
# ditto
def {{name.id}}(value)
{{name.id}}(value.to_f)
end
{% end %}
{% for name in %w(besselj0 besselj1 bessely0 bessely1) %}
# Calculates the {{name.id}} function of *value*.
def {{name.id}}(value : Float32)
{% if flag?(:darwin) %}
LibM.{{name.id}}_f64(value).to_f32
{% else %}
LibM.{{name.id}}_f32(value)
{% end %}
end
# ditto
def {{name.id}}(value : Float64)
LibM.{{name.id}}_f64(value)
end
# ditto
def {{name.id}}(value)
{{name.id}}(value.to_f)
end
{% end %}
# Calculates the gamma function of *value*.
#
# Note that `gamma(n)` is same as `fact(n - 1)` for integer `n > 0`.
# However `gamma(n)` returns float and can be an approximation.
def gamma(value : Float32)
LibM.tgamma_f32(value)
end
# ditto
def gamma(value : Float64)
LibM.tgamma_f64(value)
end
# ditto
def gamma(value)
gamma(value.to_f)
end
# Calculates the logarithmic gamma of *value*.
#
# ```
# Math.lgamma(2.96)
# ```
# is the same as
# ```
# Math.log(Math.gamma(2.96).abs)
# ```
def lgamma(value : Float32)
{% if flag?(:darwin) %}
LibM.gamma_f64(value).to_f32
{% else %}
LibM.gamma_f32(value)
{% end %}
end
# ditto
def lgamma(value : Float64)
LibM.gamma_f64(value)
end
# ditto
def lgamma(value)
lgamma(value.to_f)
end
{% for name in %w(atan2 copysign hypot) %}
# Calculates {{name.id}} with parameters *value1* and *value2*.
def {{name.id}}(value1 : Float32, value2 : Float32)
LibM.{{name.id}}_f32(value1, value2)
end
# ditto
def {{name.id}}(value1 : Float64, value2 : Float64)
LibM.{{name.id}}_f64(value1, value2)
end
# ditto
def {{name.id}}(value1, value2)
{{name.id}}(value1.to_f, value2.to_f)
end
{% end %}
# ## To be uncommented once LLVM is updated
# def div(value1 : Int32, value2 : Int32)
# LibM.div_i32(value1, value2)
# end
#
# def div(value1 : Float32, value2 : Float32)
# LibM.div_f32(value1, value2)
# end
#
# def div(value1 : Float64, value2 : Float64)
# LibM.div_f64(value1, value2)
# end
#
# def div(value1, value2)
# LibM.div(value1, value2)
# end
# Returns the logarithm of *numeric* to the base *base*.
def log(numeric, base)
log(numeric) / log(base)
end
# ## To be uncommented once LLVM is updated
# def max(value1 : Float32, value2 : Float32)
# LibM.max_f32(value1, value2)
# end
#
# def max(value1 : Float64, value2 : Float64)
# LibM.max_f64(value1, value2)
# end
# Returns the greater of *value1* and *value2*.
def max(value1, value2)
value1 >= value2 ? value1 : value2
end
# ## To be uncommented once LLVM is updated
# def min(value1 : Float32, value2 : Float32)
# LibM.min_f32(value1, value2)
# end
#
# def min(value1 : Float64, value2 : Float64)
# LibM.min_f64(value1, value2)
# end
# Returns the smaller of *value1* and *value2*.
def min(value1, value2)
value1 <= value2 ? value1 : value2
end
# ## To be uncommented once LLVM is updated
# def rem(value1 : Int32, value2 : Int32)
# LibM.rem_i32(value1, value2)
# end
# def rem(value1 : Float32, value2 : Float32)
# LibM.rem_f32(value1, value2)
# end
# def rem(value1 : Float64, value2 : Float64)
# LibM.rem_f64(value1, value2)
# end
# def rem(value1, value2)
# LibM.rem(value1, value2)
# end
{% for name in %w(besselj bessely) %}
# Calculates {{name.id}} with parameters *value1* and *value2*.
def {{name.id}}(value1 : Int32, value2 : Float32)
{% if flag?(:darwin) %}
LibM.{{name.id}}_f64(value1, value2).to_f32
{% else %}
LibM.{{name.id}}_f32(value1, value2)
{% end %}
end
# ditto
def {{name.id}}(value1 : Int32, value2 : Float64)
LibM.{{name.id}}_f64(value1, value2)
end
# ditto
def {{name.id}}(value1, value2)
{{name.id}}(value1.to_i32, value1.to_f)
end
{% end %}
{% for name in %w(ldexp scalbn) %}
# Calculates {{name.id}} with parameters *value1* and *value2*.
def {{name.id}}(value1 : Float32, value2 : Int32)
LibM.{{name.id}}_f32(value1, value2)
end
# ditto
def {{name.id}}(value1 : Float64, value2 : Int32)
LibM.{{name.id}}_f64(value1, value2)
end
# ditto
def {{name.id}}(value1, value2)
{{name.id}}(value1.to_f, value2.to_i32)
end
{% end %}
# Multiplies *value* by 2 raised to power *exp*.
def scalbln(value : Float32, exp : Int64)
LibM.scalbln_f32(value, exp)
end
# ditto
def scalbln(value : Float64, exp : Int64)
LibM.scalbln_f64(value, exp)
end
# ditto
def scalbln(value, exp)
scalbln(value.to_f, exp.to_i64)
end
# Decomposes given floating point *value* into a normalized fraction and an integral power of two.
def frexp(value : Float32)
frac = LibM.frexp_f32(value, out exp)
{frac, exp}
end
# ditto
def frexp(value : Float64)
frac = LibM.frexp_f64(value, out exp)
{frac, exp}
end
# ditto
def frexp(value)
frexp(value.to_f)
end
# Computes the next highest power of 2 of *v*.
#
# ```
# Math.pw2ceil(33) # => 64
# ```
def pw2ceil(v)
# Taken from http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
v -= 1
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v += 1
end
end