-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxmath.go
740 lines (681 loc) · 16.2 KB
/
xmath.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
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// Package xmath provides some extended capabilities according to GoLang's math.
package xmath
import (
crand "crypto/rand"
"math"
"math/big"
)
const (
MinBase = 2
MaxBase = 36
MaxInt8Value = 1<<7 - 1
MinInt8Value = -1 << 7
MaxInt16Value = 1<<15 - 1
MinInt16Value = -1 << 15
MaxInt32Value = 1<<31 - 1
MinInt32Value = -1 << 31
MaxInt64Value = 1<<63 - 1
MinInt64Value = -1 << 63
MaxUint8Value = 1<<8 - 1
MaxUint16Value = 1<<16 - 1
MaxUint32Value = 1<<32 - 1
MaxUint64Value = 1<<64 - 1
MaxIntValue = 1<<(UintSize-1) - 1
MinIntValue = -1 << (UintSize - 1)
MaxUintValue = 1<<UintSize - 1
UintSize = 32 << (^uint(0) >> 32 & 1)
)
// FloorP returns the greatest value less than or equal to x with specified decimal precision.
func FloorP(x float64, prec int) float64 {
k := math.Pow10(prec)
return math.Floor(x*k) / k
}
// FloorPB returns the greatest value less than or equal to x with specified precision of base.
// It panics unless base is in valid range.
func FloorPB(x float64, prec int, base int) float64 {
panicForInvalidBase(base)
k := math.Pow(float64(base), float64(prec))
return math.Floor(x*k) / k
}
// CeilP returns the least value greater than or equal to x with specified decimal precision.
func CeilP(x float64, prec int) float64 {
k := math.Pow10(prec)
return math.Ceil(x*k) / k
}
// CeilPB returns the least value greater than or equal to x with specified precision of base.
// It panics unless base is in valid range.
func CeilPB(x float64, prec int, base int) float64 {
panicForInvalidBase(base)
k := math.Pow(float64(base), float64(prec))
return math.Ceil(x*k) / k
}
// Round returns the nearest integer value, rounding half away from zero.
func Round(x float64) float64 {
return math.Floor(x + 0.5)
}
// RoundP returns the nearest integer value, rounding half away from zero with specified decimal precision.
func RoundP(x float64, prec int) float64 {
k := math.Pow10(prec)
return math.Floor(x*k+0.5) / k
}
// RoundPB returns the nearest integer value, rounding half away from zero with specified precision of base.
// It panics unless base is in valid range.
func RoundPB(x float64, prec int, base int) float64 {
panicForInvalidBase(base)
k := math.Pow(float64(base), float64(prec))
return math.Floor(x*k+0.5) / k
}
// Max returns the larger of x...
//
// Special cases are:
// Max(x, +Inf) = Max(+Inf, x) = +Inf
// Max(x, NaN) = Max(NaN, x) = NaN
// Max(+0, ±0) = Max(±0, +0) = +0
// Max(-0, -0) = -0
// Max(x) = x
// Max() = +Inf
func Max(x ...float64) float64 {
if len(x) <= 0 {
return math.Inf(+1)
}
result := math.Inf(-1)
for _, a := range x {
result = math.Max(a, result)
}
return result
}
// Min returns the smaller of x...
//
// Special cases are:
// Min(x, -Inf) = Min(-Inf, x) = -Inf
// Min(x, NaN) = Min(NaN, x) = NaN
// Min(-0, ±0) = Min(±0, -0) = -0
// Min(x) = x
// Min() = -Inf
func Min(x ...float64) float64 {
if len(x) <= 0 {
return math.Inf(-1)
}
result := math.Inf(+1)
for _, a := range x {
result = math.Min(a, result)
}
return result
}
// MaxMin returns the max, min values in this order, similar with Max and Min functions.
//
// Special cases are:
// MaxMin(x) = x, x
// MaxMin() = +Inf, -Inf
func MaxMin(x ...float64) (max float64, min float64) {
min, max = MinMax(x...)
return
}
// MinMax returns the min, max values in this order, similar with Min and Max functions.
//
// Special cases are:
// MinMax(x) = x, x
// MinMax() = -Inf, +Inf
func MinMax(x ...float64) (min float64, max float64) {
if len(x) <= 0 {
return math.Inf(-1), math.Inf(+1)
}
min = math.Inf(+1)
max = math.Inf(-1)
for _, a := range x {
min = math.Min(a, min)
max = math.Max(a, max)
}
return
}
// MaxInt returns the larger integer of x...
//
// Special cases are:
// MaxInt(x) = x
// MaxInt() = math.MaxInt64
func MaxInt(x ...int64) int64 {
if len(x) <= 0 {
return int64(math.MaxInt64)
}
result := int64(math.MinInt64)
for _, a := range x {
if a > result {
result = a
}
}
return result
}
// MinInt returns the smaller integer of x...
//
// Special cases are:
// MinInt(x) = x
// MinInt() = math.MinInt64
func MinInt(x ...int64) int64 {
if len(x) <= 0 {
return int64(math.MinInt64)
}
result := int64(math.MaxInt64)
for _, a := range x {
if a < result {
result = a
}
}
return result
}
// MaxMinInt returns the max, min integers in this order, similar with MaxInt and MinInt functions.
//
// Special cases are:
// MaxMinInt(x) = x, x
// MaxMinInt() = math.MaxInt64, math.MinInt64
func MaxMinInt(x ...int64) (max int64, min int64) {
min, max = MinMaxInt(x...)
return
}
// MinMaxInt returns the min, max integers in this order, similar with MinInt and MaxInt functions.
//
// Special cases are:
// MinMaxInt(x) = x, x
// MinMaxInt() = math.MinInt64, math.MaxInt64
func MinMaxInt(x ...int64) (min int64, max int64) {
if len(x) <= 0 {
return int64(math.MinInt64), int64(math.MaxInt64)
}
min = int64(math.MaxInt64)
max = int64(math.MinInt64)
for _, a := range x {
if a < min {
min = a
}
if a > max {
max = a
}
}
return
}
// MaxUint returns the larger unsigned integer of x...
//
// Special cases are:
// MaxUint(x) = x
// MaxUint() = math.MaxUint64
func MaxUint(x ...uint64) uint64 {
if len(x) <= 0 {
return uint64(math.MaxUint64)
}
result := uint64(0)
for _, a := range x {
if a > result {
result = a
}
}
return result
}
// MinUint returns the smaller unsigned integer of x...
//
// Special cases are:
// MinUint(x) = x
// MinUint() = 0
func MinUint(x ...uint64) uint64 {
if len(x) <= 0 {
return uint64(0)
}
result := uint64(math.MaxUint64)
for _, a := range x {
if a < result {
result = a
}
}
return result
}
// MaxMinUint returns the max, min unsigned integers in this order, similar with MaxUint and MinUint functions.
//
// Special cases are:
// MaxMinUint(x) = x, x
// MaxMinUint() = math.MaxUint64, 0
func MaxMinUint(x ...uint64) (max uint64, min uint64) {
min, max = MinMaxUint(x...)
return
}
// MinMaxUint returns the min, max unsigned integers in this order, similar with MinUint and MaxUint functions.
//
// Special cases are:
// MinMaxUint(x) = x, x
// MinMaxUint() = 0, math.MaxUint64
func MinMaxUint(x ...uint64) (min uint64, max uint64) {
if len(x) <= 0 {
return uint64(0), uint64(math.MaxUint64)
}
min = uint64(math.MaxUint64)
max = uint64(0)
for _, a := range x {
if a < min {
min = a
}
if a > max {
max = a
}
}
return
}
// Between checks x is between a and b
func Between(x float64, a, b float64) bool {
min, max := MinMax(a, b)
return min < x && x < max
}
// BetweenIn checks x is in a and b
func BetweenIn(x float64, a, b float64) bool {
min, max := MinMax(a, b)
return min <= x && x <= max
}
// SafeDiv divides x to y. For 'division by zero', it returns 0 if allowNaN is false.
// The GoLang's default behaviour is same with SafeDiv(x, y, true).
// Special cases are:
// SafeDiv(0, ±n, true) = ±0
// SafeDiv(0, ±n, false) = ±0
// SafeDiv(±n, 0, true) = ±Inf
// SafeDiv(±n, 0, false) = ±Inf
// SafeDiv(0, 0, true) = NaN
// SafeDiv(0, 0, false) = 0
func SafeDiv(x, y float64, allowNaN bool) float64 {
if y == 0 {
if x < 0 {
return math.Inf(-1)
}
if x > 0 {
return math.Inf(+1)
}
if allowNaN {
return math.NaN()
}
return 0
}
return x / y
}
// CryptoRandInt returns a random integer in [0, max).
// It returns -1 when error occurs.
func CryptoRandInt(max int64) int64 {
if max <= 0 {
return -1
}
num, _ := crand.Int(crand.Reader, big.NewInt(max))
if num == nil {
return -1
}
return num.Int64()
}
// CryptoRandFloat returns a random decimal number in [0, 1).
// It returns -1 when error occurs.
func CryptoRandFloat() float64 {
r := CryptoRandInt(math.MaxInt64)
if r < 0 {
return -1
}
return float64(r) / math.MaxInt64
}
// CryptoRand is synonym with CryptoRandFloat.
func CryptoRand() float64 {
return CryptoRandFloat()
}
// CryptoRandCode generates random code in [10^(n-1), 10^n).
// It returns -1 when error occurs.
func CryptoRandCode(n int) int64 {
if n < 1 || n > 18 {
return -1
}
start := int64(1)
for i := 0; i < n-1; i++ {
start *= 10
}
r := CryptoRandInt(start*10 - start)
if r < 0 {
return -1
}
return start + r
}
// AlmostEqualP64 checks almost equality of all given 64-bit floating points values.
// Argument p is measure of precision. If p is 0, it checks exact equality.
// It returns true if all values are almost equal.
//
// Special cases are:
// AlmostEqualP64(p) = false
// AlmostEqualP64(p, x) = true
// AlmostEqualP64(p, NaN) = false
// AlmostEqualP64(p, NaN, x) = false
// AlmostEqualP64(p, x, NaN) = false
// AlmostEqualP64(p, +Inf, +Inf) = true
// AlmostEqualP64(p, -Inf, -Inf) = true
func AlmostEqualP64(p uint64, x ...float64) bool {
if len(x) <= 0 {
return false
}
var a float64
for i, b := range x {
if math.IsNaN(b) {
return false
}
if i > 0 {
c, d := math.Float64bits(a), math.Float64bits(b)
if c < d {
c, d = d, c
}
if (c>>52 != d>>52) || c-d > p {
return false
}
}
a = b
}
return true
}
// AlmostEqualP32 checks almost equality of all given 32-bit floating points values.
// Argument p is measure of precision. If p is 0, it checks exact equality.
// It returns true if all values are almost equal.
//
// Special cases are:
// AlmostEqualP32(p) = false
// AlmostEqualP32(p, x) = true
// AlmostEqualP32(p, NaN) = false
// AlmostEqualP32(p, NaN, x) = false
// AlmostEqualP32(p, x, NaN) = false
// AlmostEqualP32(p, +Inf, +Inf) = true
// AlmostEqualP32(p, -Inf, -Inf) = true
func AlmostEqualP32(p uint32, x ...float32) bool {
if len(x) <= 0 {
return false
}
var a float32
for i, b := range x {
if math.IsNaN(float64(b)) {
return false
}
if i > 0 {
c, d := math.Float32bits(a), math.Float32bits(b)
if c < d {
c, d = d, c
}
if (c>>23 != d>>23) || c-d > p {
return false
}
}
a = b
}
return true
}
// AlmostEqualP is synonym with AlmostEqualP64.
func AlmostEqualP(p uint64, x ...float64) bool {
return AlmostEqualP64(p, x...)
}
// AlmostEqual64 is synonym with AlmostEqualP64(1, x...).
func AlmostEqual64(x ...float64) bool {
return AlmostEqualP64(1, x...)
}
// AlmostEqual32 is synonym with AlmostEqualP32(1, x...).
func AlmostEqual32(x ...float32) bool {
return AlmostEqualP32(1, x...)
}
// AlmostEqual is synonym with AlmostEqualP(1, x...).
func AlmostEqual(x ...float64) bool {
return AlmostEqualP(1, x...)
}
// AlmostEqualD64 checks almost equality of all given 64-bit floating points values.
// Argument d is the least difference value of inequality. If d is 0, it checks exact equality.
// It returns true if all values are almost equal.
//
// Special cases are:
// AlmostEqualD64(d) = false
// AlmostEqualD64(d, x) = true
// AlmostEqualD64(d, NaN) = false
// AlmostEqualD64(d, NaN, x) = false
// AlmostEqualD64(d, x, NaN) = false
// AlmostEqualD64(d, +Inf, +Inf) = true
// AlmostEqualD64(d, -Inf, -Inf) = true
func AlmostEqualD64(d float64, x ...float64) bool {
if len(x) <= 0 {
return false
}
var a float64
for i, b := range x {
if math.IsNaN(b) {
return false
}
if i > 0 && math.Abs(a-b) >= d {
return false
}
a = b
}
return true
}
// AlmostEqualD32 checks almost equality of all given 32-bit floating points values.
// Argument d is the least difference value of inequality. If d is 0, it checks exact equality.
// It returns true if all values are almost equal.
//
// Special cases are:
// AlmostEqualD32(d) = false
// AlmostEqualD32(d, x) = true
// AlmostEqualD32(d, NaN) = false
// AlmostEqualD32(d, NaN, x) = false
// AlmostEqualD32(d, x, NaN) = false
// AlmostEqualD32(d, +Inf, +Inf) = true
// AlmostEqualD32(d, -Inf, -Inf) = true
func AlmostEqualD32(d float32, x ...float32) bool {
if len(x) <= 0 {
return false
}
var a float32
for i, b := range x {
if math.IsNaN(float64(b)) {
return false
}
if i > 0 && math.Abs(float64(a-b)) >= float64(d) {
return false
}
a = b
}
return true
}
// AlmostEqualD is synonym with AlmostEqualD64.
func AlmostEqualD(d float64, x ...float64) bool {
return AlmostEqualD64(d, x...)
}
// Equal64 checks exact equality of all given 64-bit floating points values by comparing.
// It returns true if all values are equal.
//
// Special cases are:
// Equal64() = false
// Equal64(x) = true
// Equal64(NaN) = false
// Equal64(NaN, x) = false
// Equal64(x, NaN) = false
// Equal64(+Inf, +Inf) = true
// Equal64(-Inf, -Inf) = true
func Equal64(x ...float64) bool {
if len(x) <= 0 {
return false
}
var a float64
for i, b := range x {
if math.IsNaN(b) {
return false
}
if i > 0 {
if !math.IsNaN(a-b) && (a > b || a < b) {
return false
}
}
a = b
}
return true
}
// Equal32 checks exact equality of all given 32-bit floating points values by comparing.
// It returns true if all values are equal.
//
// Special cases are:
// Equal32() = false
// Equal32(x) = true
// Equal32(NaN) = false
// Equal32(NaN, x) = false
// Equal32(x, NaN) = false
// Equal32(+Inf, +Inf) = true
// Equal32(-Inf, -Inf) = true
func Equal32(x ...float32) bool {
if len(x) <= 0 {
return false
}
var a float32
for i, b := range x {
if math.IsNaN(float64(b)) {
return false
}
if i > 0 {
if !math.IsNaN(float64(a-b)) && (a > b || a < b) {
return false
}
}
a = b
}
return true
}
// Equal is synonym with Equal64.
func Equal(x ...float64) bool {
return Equal64(x...)
}
// IsZero checks whether fraction of the given value is zero.
// It may return true even exponential isn't zero.
func IsZero(x float64) bool {
return math.Float64bits(x)<<12 == 0
}
// Zero returns zero floating point value by given sign.
// -0.0 if sign < 0
// +0.0 if sign is 0
// +0.0 if sign > 0
func Zero(sign int) float64 {
return math.Copysign(0, float64(sign))
}
// Sign returns:
// -1 if x < 0
// 0 if x is ±0
// +1 if x > 0
// Sign panics if x is NaN.
func Sign(x float64) int {
panicForNaN(x)
switch {
case x < 0:
return -1
case x > 0:
return 1
}
return 0
}
// SignInt returns:
// -1 if x < 0
// 0 if x is 0
// +1 if x > 0
func SignInt(x int64) int {
switch {
case x < 0:
return -1
case x > 0:
return 1
}
return 0
}
// Sum returns the sum of x...
func Sum(x ...float64) (sum float64) {
for _, y := range x {
sum += y
}
return
}
// Avg returns the arithmetic mean of x...
func Avg(x ...float64) (avg float64) {
k := float64(len(x))
for _, y := range x {
avg += y / k
}
return
}
// SumInt returns the floating point of sum of x...
func SumInt(x ...int64) (sum float64) {
for _, y := range x {
sum += float64(y)
}
return
}
// AvgInt returns the floating point of arithmetic mean of x...
func AvgInt(x ...int64) (avg float64) {
k := float64(len(x))
for _, y := range x {
avg += float64(y) / k
}
return
}
// SumUint returns the floating point of sum of x...
func SumUint(x ...uint64) (sum float64) {
for _, y := range x {
sum += float64(y)
}
return
}
// AvgUint returns the floating point of arithmetic mean of x...
func AvgUint(x ...uint64) (avg float64) {
k := float64(len(x))
for _, y := range x {
avg += float64(y) / k
}
return
}
// SumInt2 returns the sum of x...
// If the result overflows, it returns overflow is true.
func SumInt2(x ...int64) (sum int64, overflow bool) {
var last int64
for _, y := range x {
sum += y
signLast, signSum, signY := SignInt(last), SignInt(sum), SignInt(y)
if !overflow && signLast != signSum && signLast == signY {
overflow = true
}
last = sum
}
return
}
// AvgInt2 returns the arithmetic mean of x...
// If the result overflows, it returns overflow is true.
func AvgInt2(x ...int64) (avg int64, overflow bool) {
var sum int64
sum, overflow = SumInt2(x...)
if count := len(x); count > 0 {
avg = sum / int64(count)
}
return
}
// SumUint2 returns the sum of x...
// If the result overflows, it returns overflow is true.
func SumUint2(x ...uint64) (sum uint64, overflow bool) {
var last uint64
for _, y := range x {
sum += y
if !overflow && sum < last {
overflow = true
}
last = sum
}
return
}
// AvgUint2 returns the arithmetic mean of x...
// If the result overflows, it returns overflow is true.
func AvgUint2(x ...uint64) (avg uint64, overflow bool) {
var sum uint64
sum, overflow = SumUint2(x...)
if count := len(x); count > 0 {
avg = sum / uint64(count)
}
return
}
func panicForInvalidBase(base int) {
if !(MinBase <= base && base <= MaxBase) {
panic("invalid base")
}
}
func panicForNaN(x float64) {
if math.IsNaN(x) {
panic("NaN value")
}
}