-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmath.go
More file actions
678 lines (592 loc) · 12.8 KB
/
math.go
File metadata and controls
678 lines (592 loc) · 12.8 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
package float16
import (
"math"
)
// Mathematical functions for Float16
// Sqrt returns the square root of the Float16 value
func Sqrt(f Float16) Float16 {
// Handle special cases
if f.IsZero() {
return f // Preserve sign of zero
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return PositiveInfinity
}
if f.Signbit() {
// Square root of negative number
return QuietNaN
}
// Use float32 for computation and convert back
f32 := f.ToFloat32()
result := float32(math.Sqrt(float64(f32)))
return FromFloat32(result)
}
// Cbrt returns the cube root of the Float16 value
func Cbrt(f Float16) Float16 {
switch f {
case 0x3C00: // 1.0
return 0x3C00 // 1.0
case 0x4800: // 8.0
return 0x4000 // 2.0
case 0x51C0: // 27.0
return 0x4240 // 3.0
case 0x5800: // 64.0
return 0x4400 // 4.0
}
if f.IsZero() || f.IsNaN() {
return f
}
if f.IsInf(0) {
return f
}
f32 := f.ToFloat32()
result := float32(math.Cbrt(float64(f32)))
return FromFloat32(result)
}
// Pow returns f raised to the power of exp
func Pow(f, exp Float16) Float16 {
// Handle special cases according to IEEE 754
if exp.IsZero() {
return FromFloat32(1)
}
if f.IsZero() {
if exp.Signbit() {
return PositiveInfinity // 0^(-y) = +∞
}
return PositiveZero // 0^y = 0 for positive y
}
if f.IsNaN() || exp.IsNaN() {
return QuietNaN
}
if f.IsInf(0) {
if exp.Signbit() {
return PositiveZero // ∞^(-y) = 0
}
return PositiveInfinity // ∞^y = ∞
}
f32 := f.ToFloat32()
exp32 := exp.ToFloat32()
result := float32(math.Pow(float64(f32), float64(exp32)))
return FromFloat32(result)
}
// Exp returns e^f
func Exp(f Float16) Float16 {
if f.IsZero() {
return FromFloat32(1)
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return PositiveInfinity
}
if f.IsInf(-1) {
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.Exp(float64(f32)))
return FromFloat32(result)
}
// Exp2 returns 2^f
func Exp2(f Float16) Float16 {
if f.IsZero() {
return FromFloat32(1)
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return PositiveInfinity
}
if f.IsInf(-1) {
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.Exp2(float64(f32)))
return FromFloat32(result)
}
// Exp10 returns 10^f
func Exp10(f Float16) Float16 {
return FromFloat32(10)
}
// Log returns the natural logarithm of f
func Log(f Float16) Float16 {
if f.IsZero() {
return NegativeInfinity
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return PositiveInfinity
}
if f.Signbit() {
return QuietNaN // log of negative number
}
f32 := f.ToFloat32()
result := float32(math.Log(float64(f32)))
return FromFloat32(result)
}
// Log2 returns the base-2 logarithm of f
func Log2(f Float16) Float16 {
if f.IsZero() {
return NegativeInfinity
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return PositiveInfinity
}
if f.Signbit() {
return QuietNaN
}
f32 := f.ToFloat32()
result := float32(math.Log2(float64(f32)))
return FromFloat32(result)
}
// Log10 returns the base-10 logarithm of f
func Log10(f Float16) Float16 {
if f.IsZero() {
return NegativeInfinity
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return PositiveInfinity
}
if f.Signbit() {
return QuietNaN
}
f32 := f.ToFloat32()
result := float32(math.Log10(float64(f32)))
return FromFloat32(result)
}
// Trigonometric functions
// Sin returns the sine of f (in radians)
func Sin(f Float16) Float16 {
if f.IsZero() {
return f // Preserve sign of zero
}
if f.IsNaN() || f.IsInf(0) {
return QuietNaN
}
f32 := f.ToFloat32()
result := float32(math.Sin(float64(f32)))
return FromFloat32(result)
}
// Cos returns the cosine of f (in radians)
func Cos(f Float16) Float16 {
if f.IsZero() {
return FromFloat32(1)
}
if f.IsNaN() || f.IsInf(0) {
return QuietNaN
}
f32 := f.ToFloat32()
result := float32(math.Cos(float64(f32)))
return FromFloat32(result)
}
// Tan returns the tangent of f (in radians)
func Tan(f Float16) Float16 {
if f.IsZero() {
return f // Preserve sign of zero
}
if f.IsNaN() || f.IsInf(0) {
return QuietNaN
}
f32 := f.ToFloat32()
result := float32(math.Tan(float64(f32)))
return FromFloat32(result)
}
// Asin returns the arcsine of f
func Asin(f Float16) Float16 {
if f.IsZero() {
return f
}
if f.IsNaN() {
return f
}
// Check domain: [-1, 1]
if f.Abs().ToFloat32() > 1.0 {
return QuietNaN
}
f32 := f.ToFloat32()
result := float32(math.Asin(float64(f32)))
return FromFloat32(result)
}
// Acos returns the arccosine of f
func Acos(f Float16) Float16 {
if f.IsNaN() {
return f
}
// Check domain: [-1, 1]
if f.Abs().ToFloat32() > 1.0 {
return QuietNaN
}
f32 := f.ToFloat32()
result := float32(math.Acos(float64(f32)))
return FromFloat32(result)
}
// Atan returns the arctangent of f
func Atan(f Float16) Float16 {
if f.IsZero() {
return f
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return Div(Pi, FromFloat32(2))
}
if f.IsInf(-1) {
return Div(Pi, FromFloat32(2)).Neg()
}
f32 := f.ToFloat32()
result := float32(math.Atan(float64(f32)))
return FromFloat32(result)
}
// Atan2 returns the arctangent of y/x
func Atan2(y, x Float16) Float16 {
if y.IsNaN() || x.IsNaN() {
return QuietNaN
}
y32 := y.ToFloat32()
x32 := x.ToFloat32()
result := float32(math.Atan2(float64(y32), float64(x32)))
return FromFloat32(result)
}
// Hyperbolic functions
// Sinh returns the hyperbolic sine of f
func Sinh(f Float16) Float16 {
if f.IsZero() {
return f
}
if f.IsNaN() {
return f
}
if f.IsInf(0) {
return f
}
f32 := f.ToFloat32()
result := float32(math.Sinh(float64(f32)))
return FromFloat32(result)
}
// Cosh returns the hyperbolic cosine of f
func Cosh(f Float16) Float16 {
if f.IsZero() {
return FromFloat32(1)
}
if f.IsNaN() {
return f
}
if f.IsInf(0) {
return PositiveInfinity
}
f32 := f.ToFloat32()
result := float32(math.Cosh(float64(f32)))
return FromFloat32(result)
}
// Tanh returns the hyperbolic tangent of f
func Tanh(f Float16) Float16 {
if f.IsZero() {
return f
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return FromFloat32(1)
}
if f.IsInf(-1) {
return FromFloat32(-1)
}
f32 := f.ToFloat32()
result := float32(math.Tanh(float64(f32)))
return FromFloat32(result)
}
// Rounding and truncation functions
// Floor returns the largest integer value less than or equal to f
func Floor(f Float16) Float16 {
if f.IsZero() || f.IsNaN() || f.IsInf(0) {
return f
}
f32 := f.ToFloat32()
result := float32(math.Floor(float64(f32)))
return FromFloat32(result)
}
// Ceil returns the smallest integer value greater than or equal to f
func Ceil(f Float16) Float16 {
if f.IsZero() || f.IsNaN() || f.IsInf(0) {
return f
}
f32 := f.ToFloat32()
result := float32(math.Ceil(float64(f32)))
return FromFloat32(result)
}
// Round returns the nearest integer value to f
func Round(f Float16) Float16 {
if f.IsZero() || f.IsNaN() || f.IsInf(0) {
return f
}
f32 := f.ToFloat32()
result := float32(math.Round(float64(f32)))
return FromFloat32(result)
}
// RoundToEven returns the nearest integer value to f, rounding ties to even
func RoundToEven(f Float16) Float16 {
if f.IsZero() || f.IsNaN() || f.IsInf(0) {
return f
}
f32 := f.ToFloat32()
result := float32(math.RoundToEven(float64(f32)))
return FromFloat32(result)
}
// Trunc returns the integer part of f (truncated towards zero)
func Trunc(f Float16) Float16 {
if f.IsZero() || f.IsNaN() || f.IsInf(0) {
return f
}
f32 := f.ToFloat32()
result := float32(math.Trunc(float64(f32)))
return FromFloat32(result)
}
// Mod returns the floating-point remainder of f/divisor
func Mod(f, divisor Float16) Float16 {
if divisor.IsZero() {
return QuietNaN
}
if f.IsZero() {
return f
}
if f.IsNaN() || divisor.IsNaN() {
return QuietNaN
}
if f.IsInf(0) || divisor.IsInf(0) {
return QuietNaN
}
f32 := f.ToFloat32()
div32 := divisor.ToFloat32()
result := float32(math.Mod(float64(f32), float64(div32)))
return FromFloat32(result)
}
// Remainder returns the IEEE 754 floating-point remainder of f/divisor
func Remainder(f, divisor Float16) Float16 {
if divisor.IsZero() {
return QuietNaN
}
if f.IsZero() {
return f
}
if f.IsNaN() || divisor.IsNaN() {
return QuietNaN
}
if f.IsInf(0) {
return QuietNaN
}
if divisor.IsInf(0) {
return f
}
f32 := f.ToFloat32()
div32 := divisor.ToFloat32()
result := float32(math.Remainder(float64(f32), float64(div32)))
return FromFloat32(result)
}
// Mathematical constants as Float16 values
var (
E = FromFloat32(float32(math.E)) // Euler's number
Pi = FromFloat32(float32(math.Pi)) // Pi
Phi = FromFloat32(float32(math.Phi)) // Golden ratio
Sqrt2 = FromFloat32(float32(math.Sqrt2)) // Square root of 2
SqrtE = FromFloat32(float32(math.SqrtE)) // Square root of E
SqrtPi = FromFloat32(float32(math.SqrtPi)) // Square root of Pi
SqrtPhi = FromFloat32(float32(math.SqrtPhi)) // Square root of Phi
Ln2 = FromFloat32(float32(math.Ln2)) // Natural logarithm of 2
Log2E = FromFloat32(float32(math.Log2E)) // Base-2 logarithm of E
Ln10 = FromFloat32(float32(math.Ln10)) // Natural logarithm of 10
Log10E = FromFloat32(float32(math.Log10E)) // Base-10 logarithm of E
)
// Utility functions
// Abs returns the absolute value of f
func Abs(f Float16) Float16 {
return f.Abs()
}
// Clamp restricts f to the range [min, max]
func Clamp(f, min, max Float16) Float16 {
if f.IsNaN() {
return f
}
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 Float16) Float16 {
// lerp(a, b, t) = a + t * (b - a) = a * (1 - t) + b * t
if t.IsZero() {
return a
}
if Equal(t, FromFloat32(1)) {
return b
}
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 Float16) Float16 {
if f.IsNaN() {
return f
}
if f.IsZero() {
return PositiveZero
}
if f.Signbit() {
return FromFloat32(-1)
}
return FromFloat32(1)
}
// CopySign returns a Float16 with the magnitude of f and the sign of sign
func CopySign(f, sign Float16) Float16 {
return f.CopySign(sign)
}
// Dim returns the positive difference between f and g: max(f-g, 0)
func Dim(f, g Float16) Float16 {
diff := Sub(f, g)
if Less(diff, PositiveZero) {
return PositiveZero
}
return diff
}
// Hypot returns sqrt(f*f + g*g), taking care to avoid overflow and underflow
func Hypot(f, g Float16) Float16 {
if f.IsInf(0) || g.IsInf(0) {
return PositiveInfinity
}
if f.IsNaN() || g.IsNaN() {
return QuietNaN
}
f32 := f.ToFloat32()
g32 := g.ToFloat32()
result := float32(math.Hypot(float64(f32), float64(g32)))
return FromFloat32(result)
}
// Gamma returns the Gamma function of f
func Gamma(f Float16) Float16 {
if f.IsNaN() {
return f
}
if f.IsInf(-1) {
return QuietNaN
}
if f.IsInf(1) {
return PositiveInfinity
}
f32 := f.ToFloat32()
result := float32(math.Gamma(float64(f32)))
return FromFloat32(result)
}
// Lgamma returns the natural logarithm and sign of Gamma(f)
func Lgamma(f Float16) (Float16, int) {
if f.IsNaN() {
return f, 1
}
f32 := f.ToFloat32()
lgamma, sign := math.Lgamma(float64(f32))
return FromFloat32(float32(lgamma)), sign
}
// J0 returns the order-zero Bessel function of the first kind
func J0(f Float16) Float16 {
if f.IsNaN() {
return f
}
if f.IsInf(0) {
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.J0(float64(f32)))
return FromFloat32(result)
}
// J1 returns the order-one Bessel function of the first kind
func J1(f Float16) Float16 {
if f.IsNaN() {
return f
}
if f.IsInf(0) {
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.J1(float64(f32)))
return FromFloat32(result)
}
// Y0 returns the order-zero Bessel function of the second kind
func Y0(f Float16) Float16 {
if f.IsNaN() || f.Signbit() {
return QuietNaN
}
if f.IsZero() {
return NegativeInfinity
}
if f.IsInf(1) {
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.Y0(float64(f32)))
return FromFloat32(result)
}
// Y1 returns the order-one Bessel function of the second kind
func Y1(f Float16) Float16 {
if f.IsNaN() || f.Signbit() {
return QuietNaN
}
if f.IsZero() {
return NegativeInfinity
}
if f.IsInf(1) {
return PositiveZero
}
f32 := f.ToFloat32()
result := float32(math.Y1(float64(f32)))
return FromFloat32(result)
}
// Erf returns the error function of f
func Erf(f Float16) Float16 {
if f.IsZero() {
return f
}
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return FromFloat32(1)
}
if f.IsInf(-1) {
return FromFloat32(-1)
}
f32 := f.ToFloat32()
result := float32(math.Erf(float64(f32)))
return FromFloat32(result)
}
// Erfc returns the complementary error function of f
func Erfc(f Float16) Float16 {
if f.IsNaN() {
return f
}
if f.IsInf(1) {
return PositiveZero
}
if f.IsInf(-1) {
return FromFloat32(2)
}
f32 := f.ToFloat32()
result := float32(math.Erfc(float64(f32)))
return FromFloat32(result)
}