-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath.go
More file actions
385 lines (352 loc) · 9.09 KB
/
math.go
File metadata and controls
385 lines (352 loc) · 9.09 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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package float8
import (
"math"
)
// Mathematical functions for Float8
// Sqrt returns the square root of the Float8 value.
//
// Special cases are:
//
// Sqrt(+0) = +0
// Sqrt(-0) = -0
// Sqrt(+Inf) = +Inf
// Sqrt(x) = NaN if x < 0 (including -Inf)
// Sqrt(NaN) = NaN
//
// For finite x ≥ 0, the result is the greatest Float8 value y such that y² ≤ x.
// The result is rounded to the nearest representable Float8 value.
func Sqrt(f Float8) Float8 {
if f == PositiveZero || f == NegativeZero {
return PositiveZero
}
if f == PositiveInfinity {
return PositiveInfinity
}
if f.Sign() < 0 {
// Square root of negative number - return zero (NaN equivalent)
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.Sqrt(float64(f32)))
return ToFloat8(result)
}
// Pow returns f raised to the power of exp.
//
// Special cases are:
//
// Pow(±0, exp) = ±0 for exp > 0
// Pow(±0, exp) = +Inf for exp < 0
// Pow(1, exp) = 1 for any exp (even NaN)
// Pow(f, 0) = 1 for any f (including NaN, +Inf, -Inf)
// Pow(f, 1) = f for any f
// Pow(NaN, exp) = NaN
// Pow(f, NaN) = NaN
// Pow(±0, -Inf) = +Inf
// Pow(±0, +Inf) = +0
// Pow(+Inf, exp) = +Inf for exp > 0
// Pow(+Inf, exp) = +0 for exp < 0
// Pow(-Inf, exp) = -0 for exp a negative odd integer
// Pow(-Inf, exp) = +0 for exp a negative non-odd integer
// Pow(-Inf, exp) = -Inf for exp a positive odd integer
// Pow(-Inf, exp) = +Inf for exp a positive non-odd integer
// Pow(-1, ±Inf) = 1
// Pow(f, +Inf) = +Inf for |f| > 1
// Pow(f, -Inf) = +0 for |f| > 1
// Pow(f, +Inf) = +0 for |f| < 1
// Pow(f, -Inf) = +Inf for |f| < 1
//
// The result is rounded to the nearest representable Float8 value.
func Pow(f, exp Float8) Float8 {
// Handle special cases
if f == PositiveZero || f == NegativeZero {
if exp.Sign() > 0 {
return PositiveZero
}
if exp.Sign() < 0 {
return PositiveInfinity
}
return ToFloat8(1.0) // 0^0 = 1
}
if f == PositiveInfinity {
if exp.Sign() > 0 {
return PositiveInfinity
}
if exp.Sign() < 0 {
return PositiveZero
}
return ToFloat8(1.0) // inf^0 = 1
}
f32 := f.ToFloat32()
exp32 := exp.ToFloat32()
result := float32(math.Pow(float64(f32), float64(exp32)))
return ToFloat8(result)
}
// Exp returns e^f
func Exp(f Float8) Float8 {
if f == PositiveZero || f == NegativeZero {
return ToFloat8(1.0)
}
if f == PositiveInfinity {
return PositiveInfinity
}
if f == NegativeInfinity {
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.Exp(float64(f32)))
return ToFloat8(result)
}
// Log returns the natural logarithm of f.
//
// Special cases are:
//
// Log(+Inf) = +Inf
// Log(0) = -Inf
// Log(x < 0) = NaN
// Log(NaN) = NaN
//
// For finite x > 0, the result is the natural logarithm of x.
// The result is rounded to the nearest representable Float8 value.
func Log(f Float8) Float8 {
if f == PositiveZero || f == NegativeZero {
return NegativeInfinity
}
if f == PositiveInfinity {
return PositiveInfinity
}
if f.Sign() < 0 {
// Log of negative number - return zero (NaN equivalent)
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.Log(float64(f32)))
return ToFloat8(result)
}
// Sin returns the sine of f (in radians).
//
// Special cases are:
//
// Sin(±0) = ±0
// Sin(±Inf) = NaN
// Sin(NaN) = NaN
//
// For finite x, the result is the sine of x in the range [-1, 1].
// The result is rounded to the nearest representable Float8 value.
func Sin(f Float8) Float8 {
if f == PositiveZero || f == NegativeZero {
return f // Preserve sign of zero
}
if f.IsInf() {
return PositiveZero // sin(inf) is undefined, return 0
}
f32 := f.ToFloat32()
result := float32(math.Sin(float64(f32)))
return ToFloat8(result)
}
// Cos returns the cosine of f (in radians).
//
// Special cases are:
//
// Cos(±0) = 1
// Cos(±Inf) = NaN
// Cos(NaN) = NaN
//
// For finite x, the result is the cosine of x in the range [-1, 1].
// The result is rounded to the nearest representable Float8 value.
func Cos(f Float8) Float8 {
if f == PositiveZero || f == NegativeZero {
return ToFloat8(1.0)
}
if f.IsInf() {
return PositiveZero // cos(inf) is undefined, return 0
}
f32 := f.ToFloat32()
result := float32(math.Cos(float64(f32)))
return ToFloat8(result)
}
// Tan returns the tangent of f (in radians).
//
// Special cases are:
//
// Tan(±0) = ±0
// Tan(±Inf) = NaN
// Tan(NaN) = NaN
//
// For finite x, the result is the tangent of x.
// The result is rounded to the nearest representable Float8 value.
// Note that the result may be extremely large or small for inputs near (2n+1)π/2.
func Tan(f Float8) Float8 {
if f == PositiveZero || f == NegativeZero {
return f // Preserve sign of zero
}
if f.IsInf() {
return PositiveZero // tan(inf) is undefined, return 0
}
f32 := f.ToFloat32()
result := float32(math.Tan(float64(f32)))
return ToFloat8(result)
}
// Floor returns the greatest integer value less than or equal to f.
//
// Special cases are:
//
// Floor(±0) = ±0
// Floor(±Inf) = ±Inf
// Floor(NaN) = NaN
//
// For finite x, the result is the greatest integer value ≤ x.
// The result is exact (no rounding occurs).
func Floor(f Float8) Float8 {
if f.IsZero() || f.IsInf() {
return f
}
f32 := f.ToFloat32()
result := float32(math.Floor(float64(f32)))
return ToFloat8(result)
}
// Ceil returns the least integer value greater than or equal to f.
//
// Special cases are:
//
// Ceil(±0) = ±0
// Ceil(±Inf) = ±Inf
// Ceil(NaN) = NaN
//
// For finite x, the result is the least integer value ≥ x.
// The result is exact (no rounding occurs).
func Ceil(f Float8) Float8 {
if f.IsZero() || f.IsInf() {
return f
}
f32 := f.ToFloat32()
result := float32(math.Ceil(float64(f32)))
return ToFloat8(result)
}
// Round returns the nearest integer value to f, rounding ties to even.
//
// Special cases are:
//
// Round(±0) = ±0
// Round(±Inf) = ±Inf
// Round(NaN) = NaN
//
// For finite x, the result is the nearest integer to x.
// Ties are rounded to the nearest even integer.
// The result is exact (no rounding occurs).
func Round(f Float8) Float8 {
if f.IsZero() || f.IsInf() {
return f
}
f32 := f.ToFloat32()
result := float32(math.Round(float64(f32)))
return ToFloat8(result)
}
// Trunc returns the integer value of f with any fractional part removed.
//
// Special cases are:
//
// Trunc(±0) = ±0
// Trunc(±Inf) = ±Inf
// Trunc(NaN) = NaN
//
// For finite x, the result is the integer part of x with the sign of x.
// This is equivalent to rounding toward zero.
// The result is exact (no rounding occurs).
func Trunc(f Float8) Float8 {
if f.IsZero() || f.IsInf() {
return f
}
f32 := f.ToFloat32()
result := float32(math.Trunc(float64(f32)))
return ToFloat8(result)
}
// Fmod returns the floating-point remainder of x/y.
//
// The result has the same sign as x and magnitude less than the magnitude of y.
//
// Special cases are:
//
// Fmod(±0, y) = ±0 for y != 0
// Fmod(±Inf, y) = NaN
// Fmod(x, 0) = NaN
// Fmod(NaN, y) = NaN
// Fmod(x, NaN) = NaN
// Fmod(x, ±Inf) = x for x not infinite
//
// For finite x and y (y ≠ 0), the result is x - n*y where n is the integer
// nearest to x/y. If two integers are equally near, the even one is chosen.
// The result is rounded to the nearest representable Float8 value.
func Fmod(x, y Float8) Float8 {
if y.IsZero() {
// fmod(x, ±0) is undefined, but we return 0 for compatibility
return PositiveZero
}
if x.IsZero() {
// fmod(±0, y) = ±0
return x
}
if y.IsInf() {
// fmod(x, ±Inf) = x for finite x
return x
}
if x.IsInf() {
// fmod(±Inf, y) = NaN, but we return 0 for compatibility
return PositiveZero
}
f32 := x.ToFloat32()
div32 := y.ToFloat32()
result := float32(math.Mod(float64(f32), float64(div32)))
return ToFloat8(result)
}
// Constants as Float8 values
var (
E = ToFloat8(2.718281828459045) // Euler's number
Pi = ToFloat8(3.141592653589793) // Pi
Phi = ToFloat8(1.618033988749895) // Golden ratio
Sqrt2 = ToFloat8(1.4142135623730951) // Square root of 2
SqrtE = ToFloat8(1.6487212707001282) // Square root of E
SqrtPi = ToFloat8(1.7724538509055159) // Square root of Pi
Ln2 = ToFloat8(0.6931471805599453) // Natural logarithm of 2
Log2E = ToFloat8(1.4426950408889634) // Base-2 logarithm of E
Ln10 = ToFloat8(2.302585092994046) // Natural logarithm of 10
Log10E = ToFloat8(0.4342944819032518) // Base-10 logarithm of E
)
// Utility functions
// Clamp restricts f to the range [min, max]
func Clamp(f, min, max Float8) Float8 {
if Less(f, min) {
return min
}
if Greater(f, max) {
return max
}
return f
}
// Lerp performs linear interpolation between a and b by factor t
func Lerp(a, b, t Float8) Float8 {
// lerp(a, b, t) = a + t * (b - a)
diff := Sub(b, a)
scaled := Mul(t, diff)
return Add(a, scaled)
}
// Sign returns -1, 0, or 1 depending on the sign of f
func Sign(f Float8) Float8 {
sign := f.Sign()
switch sign {
case -1:
return ToFloat8(-1.0)
case 0:
return PositiveZero
case 1:
return ToFloat8(1.0)
default:
return PositiveZero
}
}
// CopySign returns a Float8 with the magnitude of f and the sign of sign
func CopySign(f, sign Float8) Float8 {
if sign.Sign() < 0 {
return f.Abs() | SignMask // Set sign bit
}
return f.Abs() // Clear sign bit
}