-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloat.go
359 lines (326 loc) · 9.07 KB
/
float.go
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package gost
import "math"
// Returns the largest integer less than or equal to self.
//
// f := gost.F32(3.7)
// g := gost.F32(3.0)
// h := gost.F32(-3.7)
//
// gost.AssertEq(f.Floor(), F32(3.0))
// gost.AssertEq(g.Floor(), F32(3.0))
// gost.AssertEq(h.Floor(), F32(-4.0))
func (self F32) Floor() F32 {
return F32(math.Floor(float64(self)))
}
// Returns the largest integer less than or equal to self.
//
// f := gost.F64(3.7)
// g := gost.F64(3.0)
// h := gost.F64(-3.7)
//
// gost.AssertEq(f.Floor(), F64(3.0))
// gost.AssertEq(g.Floor(), F64(3.0))
// gost.AssertEq(h.Floor(), F64(-4.0))
func (self F64) Floor() F64 {
return F64(math.Floor(float64(self)))
}
// Returns the smallest integer greater than or equal to self.
//
// f := gost.F32(3.01)
// g := gost.F32(4.0)
//
// gost.AssertEq(f.Ceil(), F32(4.0))
// gost.AssertEq(g.Ceil(), F32(4.0))
func (self F32) Ceil() F32 {
return F32(math.Ceil(float64(self)))
}
// Returns the smallest integer greater than or equal to self.
//
// f := gost.F64(3.01)
// g := gost.F64(4.0)
//
// gost.AssertEq(f.Ceil(), F64(4.0))
// gost.AssertEq(g.Ceil(), F64(4.0))
func (self F64) Ceil() F64 {
return F64(math.Ceil(float64(self)))
}
// Returns the nearest integer to self. If a value is half-way between two integers, round away from 0.0.
//
// f := gost.F32(3.3)
// g := gost.F32(-3.3)
// h := gost.F32(-3.7)
// i := gost.F32(3.5)
// j := gost.F32(4.5)
//
// gost.AssertEq(f.Round(), F32(3.0))
// gost.AssertEq(g.Round(), F32(-3.0))
// gost.AssertEq(h.Round(), F32(-4.0))
// gost.AssertEq(i.Round(), F32(4.0))
// gost.AssertEq(j.Round(), F32(5.0))
func (self F32) Round() F32 {
return F32(math.Round(float64(self)))
}
// Returns the nearest integer to self. If a value is half-way between two integers, round away from 0.0.
//
// f := gost.F64(3.3)
// g := gost.F64(-3.3)
// h := gost.F64(-3.7)
// i := gost.F64(3.5)
// j := gost.F64(4.5)
//
// gost.AssertEq(f.Round(), F64(3.0))
// gost.AssertEq(g.Round(), F64(-3.0))
// gost.AssertEq(h.Round(), F64(-4.0))
// gost.AssertEq(i.Round(), F64(4.0))
// gost.AssertEq(j.Round(), F64(5.0))
func (self F64) Round() F64 {
return F64(math.Round(float64(self)))
}
// Computes the absolute value of self.
//
// f := gost.F32(-3.0)
// g := gost.F32(3.0)
//
// gost.AssertEq(f.Abs(), F32(3.0))
// gost.AssertEq(g.Abs(), F32(3.0))
func (self F32) Abs() F32 {
return F32(math.Abs(float64(self)))
}
// Computes the absolute value of self.
//
// f := gost.F64(-3.0)
// g := gost.F64(3.0)
//
// gost.AssertEq(f.Abs(), F64(3.0))
// gost.AssertEq(g.Abs(), F64(3.0))
func (self F64) Abs() F64 {
return F64(math.Abs(float64(self)))
}
// Returns the integer part of self. This means that non-integer numbers are always truncated towards zero.
//
// f := gost.F32(3.7)
// g := gost.F32(-3.7)
//
// gost.AssertEq(f.Trunc(), F32(3.0))
// gost.AssertEq(g.Trunc(), F32(-3.0))
func (self F32) Trunc() F32 {
return F32(math.Trunc(float64(self)))
}
// Returns the integer part of self. This means that non-integer numbers are always truncated towards zero.
//
// f := gost.F64(3.7)
// g := gost.F64(-3.7)
//
// gost.AssertEq(f.Trunc(), F64(3.0))
// gost.AssertEq(g.Trunc(), F64(-3.0))
func (self F64) Trunc() F64 {
return F64(math.Trunc(float64(self)))
}
// Returns the fractional part of self.
//
// f := gost.F32(3.7)
// g := gost.F32(-3.7)
//
// gost.AssertEq(f.Frac(), F32(0.7))
// gost.AssertEq(g.Frac(), F32(-0.7))
func (self F32) Frac() F32 {
_, frac := math.Modf(float64(self))
return F32(frac)
}
// Returns the fractional part of self.
//
// f := gost.F64(3.7)
// g := gost.F64(-3.7)
//
// gost.AssertEq(f.Frac(), F64(0.7))
// gost.AssertEq(g.Frac(), F64(-0.7))
func (self F64) Frac() F64 {
_, frac := math.Modf(float64(self))
return F64(frac)
}
// Raises a number to an integer power.
// Using this function is generally faster than using powf. It might have a different sequence of rounding operations than powf, so the results are not guaranteed to agree.
//
// f := gost.F32(3.0)
// g := gost.F32(3.0)
//
// gost.AssertEq(f.Powi(2), F32(9.0))
// gost.AssertEq(g.Powi(3), F32(27.0))
func (self F32) Powi(n int) F32 {
return F32(math.Pow(float64(self), float64(n)))
}
// Raises a number to an integer power.
// Using this function is generally faster than using powf. It might have a different sequence of rounding operations than powf, so the results are not guaranteed to agree.
//
// f := gost.F64(3.0)
// g := gost.F64(3.0)
//
// gost.AssertEq(f.Powi(2), F64(9.0))
// gost.AssertEq(g.Powi(3), F64(27.0))
func (self F64) Powi(n int) F64 {
return F64(math.Pow(float64(self), float64(n)))
}
// Raises a number to a floating point power.
//
// f := gost.F32(3.0)
// g := gost.F32(3.0)
//
// gost.AssertEq(f.Powf(2.0), F32(9.0))
// gost.AssertEq(g.Powf(3.0), F32(27.0))
func (self F32) Powf(n F32) F32 {
return F32(math.Pow(float64(self), float64(n)))
}
// Raises a number to a floating point power.
//
// f := gost.F64(3.0)
// g := gost.F64(3.0)
//
// gost.AssertEq(f.Powf(2.0), F64(9.0))
// gost.AssertEq(g.Powf(3.0), F64(27.0))
func (self F64) Powf(n F64) F64 {
return F64(math.Pow(float64(self), float64(n)))
}
// Returns the square root of a number.
// Returns NaN if self is a negative number other than -0.0.
//
// f := gost.F32(9.0)
// g := gost.F32(16.0)
//
// gost.AssertEq(f.Sqrt(), F32(3.0))
// gost.AssertEq(g.Sqrt(), F32(4.0))
func (self F32) Sqrt() F32 {
return F32(math.Sqrt(float64(self)))
}
// Returns the square root of a number.
// Returns NaN if self is a negative number other than -0.0.
//
// f := gost.F64(9.0)
// g := gost.F64(16.0)
//
// gost.AssertEq(f.Sqrt(), F64(3.0))
// gost.AssertEq(g.Sqrt(), F64(4.0))
func (self F64) Sqrt() F64 {
return F64(math.Sqrt(float64(self)))
}
// Returns e^(self), (the exponential function).
//
// f := gost.F32(1.0)
// g := gost.F32(2.0)
//
// gost.AssertEq(f.Exp(), F32(2.7182817))
// gost.AssertEq(g.Exp(), F32(7.389056))
func (self F32) Exp() F32 {
return F32(math.Exp(float64(self)))
}
// Returns e^(self), (the exponential function).
//
// f := gost.F64(1.0)
// g := gost.F64(2.0)
//
// gost.AssertEq(f.Exp(), F64(2.718281828459045))
// gost.AssertEq(g.Exp(), F64(7.38905609893065))
func (self F64) Exp() F64 {
return F64(math.Exp(float64(self)))
}
// Returns 2^(self).
//
// f := gost.F32(1.0)
// g := gost.F32(2.0)
//
// gost.AssertEq(f.Exp2(), F32(2.0))
// gost.AssertEq(g.Exp2(), F32(4.0))
func (self F32) Exp2() F32 {
return F32(math.Exp2(float64(self)))
}
// Returns 2^(self).
//
// f := gost.F64(1.0)
// g := gost.F64(2.0)
//
// gost.AssertEq(f.Exp2(), F64(2.0))
// gost.AssertEq(g.Exp2(), F64(4.0))
func (self F64) Exp2() F64 {
return F64(math.Exp2(float64(self)))
}
// Returns the natural logarithm of the number.
//
// f := gost.F32(2.7182817)
// g := gost.F32(7.389056)
//
// gost.AssertEq(f.Ln(), F32(1.0))
// gost.AssertEq(g.Ln(), F32(2.0))
func (self F32) Ln() F32 {
return F32(math.Log(float64(self)))
}
// Returns the natural logarithm of the number.
//
// f := gost.F64(2.718281828459045)
// g := gost.F64(7.38905609893065)
//
// gost.AssertEq(f.Ln(), F64(1.0))
// gost.AssertEq(g.Ln(), F64(2.0))
func (self F64) Ln() F64 {
return F64(math.Log(float64(self)))
}
// Returns the logarithm of the number with respect to an arbitrary base.
// The result might not be correctly rounded owing to implementation details; self.log2() can produce more accurate results for base 2, and self.log10() can produce more accurate results for base 10.
//
// f := gost.F32(2.0)
// g := gost.F32(4.0)
//
// gost.AssertEq(f.Log(2.0), F32(1.0))
// gost.AssertEq(g.Log(2.0), F32(2.0))
func (self F32) Log(base F32) F32 {
return F32(math.Log(float64(self)) / math.Log(float64(base)))
}
// Returns the logarithm of the number with respect to an arbitrary base.
// The result might not be correctly rounded owing to implementation details; self.log2() can produce more accurate results for base 2, and self.log10() can produce more accurate results for base 10.
//
// f := gost.F64(2.0)
// g := gost.F64(4.0)
//
// gost.AssertEq(f.Log(2.0), F64(1.0))
// gost.AssertEq(g.Log(2.0), F64(2.0))
func (self F64) Log(base F64) F64 {
return F64(math.Log(float64(self)) / math.Log(float64(base)))
}
// Returns the base 2 logarithm of the number.
//
// f := gost.F32(2.0)
// g := gost.F32(4.0)
//
// gost.AssertEq(f.Log2(), F32(1.0))
// gost.AssertEq(g.Log2(), F32(2.0))
func (self F32) Log2() F32 {
return F32(math.Log2(float64(self)))
}
// Returns the base 2 logarithm of the number.
//
// f := gost.F64(2.0)
// g := gost.F64(4.0)
//
// gost.AssertEq(f.Log2(), F64(1.0))
// gost.AssertEq(g.Log2(), F64(2.0))
func (self F64) Log2() F64 {
return F64(math.Log2(float64(self)))
}
// Returns the base 10 logarithm of the number.
//
// f := gost.F32(10.0)
// g := gost.F32(100.0)
//
// gost.AssertEq(f.Log10(), F32(1.0))
// gost.AssertEq(g.Log10(), F32(2.0))
func (self F32) Log10() F32 {
return F32(math.Log10(float64(self)))
}
// Returns the base 10 logarithm of the number.
//
// f := gost.F64(10.0)
// g := gost.F64(100.0)
//
// gost.AssertEq(f.Log10(), F64(1.0))
// gost.AssertEq(g.Log10(), F64(2.0))
func (self F64) Log10() F64 {
return F64(math.Log10(float64(self)))
}