-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
fn_numeric.go
1391 lines (1171 loc) · 26.9 KB
/
fn_numeric.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package evalengine
import (
"errors"
"hash/crc32"
"math"
"strconv"
"vitess.io/vitess/go/mysql/collations"
"vitess.io/vitess/go/mysql/decimal"
"vitess.io/vitess/go/mysql/fastparse"
"vitess.io/vitess/go/sqltypes"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
)
type builtinCeil struct {
CallExpr
}
var _ IR = (*builtinCeil)(nil)
func (call *builtinCeil) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
switch num := arg.(type) {
case *evalInt64, *evalUint64:
return num, nil
case *evalDecimal:
dec := num.dec
dec = dec.Ceil()
intnum, isfit := dec.Int64()
if isfit {
return newEvalInt64(intnum), nil
}
return newEvalDecimalWithPrec(dec, 0), nil
default:
f, _ := evalToFloat(num)
return newEvalFloat(math.Ceil(f.f)), nil
}
}
func (call *builtinCeil) compile(c *compiler) (ctype, error) {
return c.compileFn_rounding(call.Arguments[0], c.asm.Fn_CEIL_f, c.asm.Fn_CEIL_d)
}
type builtinFloor struct {
CallExpr
}
var _ IR = (*builtinFloor)(nil)
func (call *builtinFloor) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
switch num := arg.(type) {
case *evalInt64, *evalUint64:
return num, nil
case *evalDecimal:
dec := num.dec
dec = dec.Floor()
intnum, isfit := dec.Int64()
if isfit {
return newEvalInt64(intnum), nil
}
return newEvalDecimalWithPrec(dec, 0), nil
default:
f, _ := evalToFloat(num)
return newEvalFloat(math.Floor(f.f)), nil
}
}
func (call *builtinFloor) compile(c *compiler) (ctype, error) {
return c.compileFn_rounding(call.Arguments[0], c.asm.Fn_FLOOR_f, c.asm.Fn_FLOOR_d)
}
type builtinAbs struct {
CallExpr
}
var _ IR = (*builtinAbs)(nil)
func (call *builtinAbs) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
switch num := arg.(type) {
case *evalUint64:
return num, nil
case *evalInt64:
if num.i < 0 {
if num.i == math.MinInt64 {
return nil, vterrors.NewErrorf(vtrpcpb.Code_INVALID_ARGUMENT, vterrors.DataOutOfRange, "BIGINT value is out of range")
}
return newEvalInt64(-num.i), nil
}
return num, nil
case *evalDecimal:
return newEvalDecimalWithPrec(num.dec.Abs(), num.length), nil
default:
f, _ := evalToFloat(num)
return newEvalFloat(math.Abs(f.f)), nil
}
}
func (expr *builtinAbs) compile(c *compiler) (ctype, error) {
arg, err := expr.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}
if arg.Type == sqltypes.Uint64 {
// No-op if it's unsigned since that's already positive.
return arg, nil
}
skip := c.compileNullCheck1(arg)
convt := ctype{Type: arg.Type, Col: collationNumeric, Flag: nullableFlags(arg.Flag)}
switch arg.Type {
case sqltypes.Int64:
c.asm.Fn_ABS_i()
case sqltypes.Float64:
c.asm.Fn_ABS_f()
case sqltypes.Decimal:
// We assume here the most common case here is that
// the decimal fits into an integer.
c.asm.Fn_ABS_d()
default:
convt.Type = sqltypes.Float64
c.asm.Convert_xf(1)
c.asm.Fn_ABS_f()
}
c.asm.jumpDestination(skip)
return convt, nil
}
type builtinPi struct {
CallExpr
}
var _ IR = (*builtinPi)(nil)
func (call *builtinPi) eval(env *ExpressionEnv) (eval, error) {
return newEvalFloat(math.Pi), nil
}
func (*builtinPi) compile(c *compiler) (ctype, error) {
c.asm.Fn_PI()
return ctype{Type: sqltypes.Float64, Col: collationNumeric}, nil
}
func isFinite(f float64) bool {
const mask = 0x7FF
const shift = 64 - 11 - 1
x := math.Float64bits(f)
return uint32(x>>shift)&mask != mask
}
type builtinAcos struct {
CallExpr
}
var _ IR = (*builtinAcos)(nil)
func (call *builtinAcos) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
if f.f < -1 || f.f > 1 {
return nil, nil
}
return newEvalFloat(math.Acos(f.f)), nil
}
func (call *builtinAcos) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_ACOS, flagNullable)
}
type builtinAsin struct {
CallExpr
}
var _ IR = (*builtinAsin)(nil)
func (call *builtinAsin) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
if f.f < -1 || f.f > 1 {
return nil, nil
}
return newEvalFloat(math.Asin(f.f)), nil
}
func (call *builtinAsin) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_ASIN, flagNullable)
}
type builtinAtan struct {
CallExpr
}
var _ IR = (*builtinAtan)(nil)
func (call *builtinAtan) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
return newEvalFloat(math.Atan(f.f)), nil
}
func (call *builtinAtan) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_ATAN, 0)
}
type builtinAtan2 struct {
CallExpr
}
var _ IR = (*builtinAtan2)(nil)
func (call *builtinAtan2) eval(env *ExpressionEnv) (eval, error) {
arg1, arg2, err := call.arg2(env)
if err != nil {
return nil, err
}
if arg1 == nil || arg2 == nil {
return nil, nil
}
f1, _ := evalToFloat(arg1)
f2, _ := evalToFloat(arg2)
return newEvalFloat(math.Atan2(f1.f, f2.f)), nil
}
func (expr *builtinAtan2) compile(c *compiler) (ctype, error) {
arg1, err := expr.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}
arg2, err := expr.Arguments[1].compile(c)
if err != nil {
return ctype{}, err
}
skip := c.compileNullCheck2(arg1, arg2)
c.compileToFloat(arg1, 2)
c.compileToFloat(arg2, 1)
c.asm.Fn_ATAN2()
c.asm.jumpDestination(skip)
return ctype{Type: sqltypes.Float64, Col: collationNumeric, Flag: nullableFlags(arg1.Flag | arg2.Flag)}, nil
}
type builtinCos struct {
CallExpr
}
var _ IR = (*builtinCos)(nil)
func (call *builtinCos) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
return newEvalFloat(math.Cos(f.f)), nil
}
func (call *builtinCos) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_COS, 0)
}
type builtinCot struct {
CallExpr
}
var _ IR = (*builtinCot)(nil)
func (call *builtinCot) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
return newEvalFloat(1.0 / math.Tan(f.f)), nil
}
func (call *builtinCot) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_COT, 0)
}
type builtinSin struct {
CallExpr
}
var _ IR = (*builtinSin)(nil)
func (call *builtinSin) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
return newEvalFloat(math.Sin(f.f)), nil
}
func (call *builtinSin) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_SIN, 0)
}
type builtinTan struct {
CallExpr
}
var _ IR = (*builtinTan)(nil)
func (call *builtinTan) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
return newEvalFloat(math.Tan(f.f)), nil
}
func (call *builtinTan) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_TAN, 0)
}
type builtinDegrees struct {
CallExpr
}
var _ IR = (*builtinDegrees)(nil)
func (call *builtinDegrees) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
return newEvalFloat(f.f * (180 / math.Pi)), nil
}
func (call *builtinDegrees) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_DEGREES, 0)
}
type builtinRadians struct {
CallExpr
}
var _ IR = (*builtinRadians)(nil)
func (call *builtinRadians) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
return newEvalFloat(f.f * (math.Pi / 180)), nil
}
func (call *builtinRadians) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_RADIANS, 0)
}
type builtinExp struct {
CallExpr
}
var _ IR = (*builtinExp)(nil)
func (call *builtinExp) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
a := math.Exp(f.f)
if !isFinite(a) {
return nil, nil
}
return newEvalFloat(a), nil
}
func (call *builtinExp) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_EXP, flagNullable)
}
type builtinLn struct {
CallExpr
}
var _ IR = (*builtinLn)(nil)
func (call *builtinLn) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
a, ok := math_log(f.f)
if !ok {
return nil, nil
}
return newEvalFloat(a), nil
}
func (call *builtinLn) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_LN, flagNullable)
}
type builtinLog struct {
CallExpr
}
var _ IR = (*builtinLog)(nil)
func (call *builtinLog) eval(env *ExpressionEnv) (eval, error) {
arg1, arg2, err := call.arg2(env)
if err != nil {
return nil, err
}
if arg1 == nil || arg2 == nil {
return nil, nil
}
f1, _ := evalToFloat(arg1)
f2, _ := evalToFloat(arg2)
a, ok := math_logN(f1.f, f2.f)
if !ok {
return nil, nil
}
return newEvalFloat(a), nil
}
func (expr *builtinLog) compile(c *compiler) (ctype, error) {
arg1, err := expr.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}
arg2, err := expr.Arguments[1].compile(c)
if err != nil {
return ctype{}, err
}
skip := c.compileNullCheck2(arg1, arg2)
c.compileToFloat(arg1, 2)
c.compileToFloat(arg2, 1)
c.asm.Fn_LOG()
c.asm.jumpDestination(skip)
return ctype{Type: sqltypes.Float64, Col: collationNumeric, Flag: nullableFlags(arg1.Flag | arg2.Flag)}, nil
}
type builtinLog10 struct {
CallExpr
}
var _ IR = (*builtinLog10)(nil)
func (call *builtinLog10) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
a, ok := math_log10(f.f)
if !ok {
return nil, nil
}
return newEvalFloat(a), nil
}
func (call *builtinLog10) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_LOG10, flagNullable)
}
type builtinLog2 struct {
CallExpr
}
var _ IR = (*builtinLog2)(nil)
func (call *builtinLog2) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
a, ok := math_log2(f.f)
if !ok {
return nil, nil
}
return newEvalFloat(a), nil
}
func (call *builtinLog2) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_LOG2, flagNullable)
}
type builtinPow struct {
CallExpr
}
var _ IR = (*builtinPow)(nil)
func (call *builtinPow) eval(env *ExpressionEnv) (eval, error) {
arg1, arg2, err := call.arg2(env)
if err != nil {
return nil, err
}
if arg1 == nil || arg2 == nil {
return nil, nil
}
f1, _ := evalToFloat(arg1)
f2, _ := evalToFloat(arg2)
a := math.Pow(f1.f, f2.f)
if !isFinite(a) {
return nil, nil
}
return newEvalFloat(a), nil
}
func (expr *builtinPow) compile(c *compiler) (ctype, error) {
arg1, err := expr.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}
arg2, err := expr.Arguments[1].compile(c)
if err != nil {
return ctype{}, err
}
skip := c.compileNullCheck2(arg1, arg2)
c.compileToFloat(arg1, 2)
c.compileToFloat(arg2, 1)
c.asm.Fn_POW()
c.asm.jumpDestination(skip)
return ctype{Type: sqltypes.Float64, Col: collationNumeric, Flag: nullableFlags(arg1.Flag | arg2.Flag)}, nil
}
type builtinSign struct {
CallExpr
}
var _ IR = (*builtinSign)(nil)
func (call *builtinSign) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
switch arg := arg.(type) {
case *evalInt64:
if arg.i < 0 {
return newEvalInt64(-1), nil
} else if arg.i > 0 {
return newEvalInt64(1), nil
} else {
return newEvalInt64(0), nil
}
case *evalUint64:
if arg.u > 0 {
return newEvalInt64(1), nil
} else {
return newEvalInt64(0), nil
}
case *evalDecimal:
return newEvalInt64(int64(arg.dec.Sign())), nil
case *evalFloat:
if arg.f < 0 {
return newEvalInt64(-1), nil
} else if arg.f > 0 {
return newEvalInt64(1), nil
} else {
return newEvalInt64(0), nil
}
default:
f, _ := evalToFloat(arg)
if f.f < 0 {
return newEvalInt64(-1), nil
} else if f.f > 0 {
return newEvalInt64(1), nil
} else {
return newEvalInt64(0), nil
}
}
}
func (expr *builtinSign) compile(c *compiler) (ctype, error) {
arg, err := expr.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}
skip := c.compileNullCheck1(arg)
switch arg.Type {
case sqltypes.Int64:
c.asm.Fn_SIGN_i()
case sqltypes.Uint64:
c.asm.Fn_SIGN_u()
case sqltypes.Float64:
c.asm.Fn_SIGN_f()
case sqltypes.Decimal:
// We assume here the most common case here is that
// the decimal fits into an integer.
c.asm.Fn_SIGN_d()
default:
c.asm.Convert_xf(1)
c.asm.Fn_SIGN_f()
}
c.asm.jumpDestination(skip)
return ctype{Type: sqltypes.Int64, Col: collationNumeric, Flag: nullableFlags(arg.Flag)}, nil
}
type builtinSqrt struct {
CallExpr
}
var _ IR = (*builtinSqrt)(nil)
func (call *builtinSqrt) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
f, _ := evalToFloat(arg)
a := math.Sqrt(f.f)
if !isFinite(a) {
return nil, nil
}
return newEvalFloat(a), nil
}
func (call *builtinSqrt) compile(c *compiler) (ctype, error) {
return c.compileFn_math1(call.Arguments[0], c.asm.Fn_SQRT, flagNullable)
}
// Math helpers extracted from `math` package
func math_log(x float64) (float64, bool) {
const (
Ln2Hi = 6.93147180369123816490e-01 /* 3fe62e42 fee00000 */
Ln2Lo = 1.90821492927058770002e-10 /* 3dea39ef 35793c76 */
L1 = 6.666666666666735130e-01 /* 3FE55555 55555593 */
L2 = 3.999999999940941908e-01 /* 3FD99999 9997FA04 */
L3 = 2.857142874366239149e-01 /* 3FD24924 94229359 */
L4 = 2.222219843214978396e-01 /* 3FCC71C5 1D8E78AF */
L5 = 1.818357216161805012e-01 /* 3FC74664 96CB03DE */
L6 = 1.531383769920937332e-01 /* 3FC39A09 D078C69F */
L7 = 1.479819860511658591e-01 /* 3FC2F112 DF3E5244 */
)
// special cases
switch {
case math.IsNaN(x) || math.IsInf(x, 1):
return 0, false
case x < 0:
return 0, false
case x == 0:
return 0, false
}
// reduce
f1, ki := math.Frexp(x)
if f1 < math.Sqrt2/2 {
f1 *= 2
ki--
}
f := f1 - 1
k := float64(ki)
// compute
s := f / (2 + f)
s2 := s * s
s4 := s2 * s2
t1 := s2 * (L1 + s4*(L3+s4*(L5+s4*L7)))
t2 := s4 * (L2 + s4*(L4+s4*L6))
R := t1 + t2
hfsq := 0.5 * f * f
return k*Ln2Hi - ((hfsq - (s*(hfsq+R) + k*Ln2Lo)) - f), true
}
func math_logN(f1, f2 float64) (float64, bool) {
a1, _ := math_log(f1)
if a1 == 0 {
return 0, false
}
a2, ok := math_log(f2)
if !ok {
return 0, false
}
return a2 / a1, true
}
func math_log10(f float64) (float64, bool) {
if a, ok := math_log(f); ok {
return a * (1 / math.Ln10), true
}
return 0, false
}
func math_log2(f float64) (float64, bool) {
frac, exp := math.Frexp(f)
// Make sure exact powers of two give an exact answer.
// Don't depend on Log(0.5)*(1/Ln2)+exp being exactly exp-1.
if frac == 0.5 {
return float64(exp - 1), true
}
if a, ok := math_log(frac); ok {
return a*(1/math.Ln2) + float64(exp), true
}
return 0, false
}
type builtinRound struct {
CallExpr
}
var _ IR = (*builtinRound)(nil)
func clampRounding(round int64) int64 {
// Use some reasonable lower limit to avoid too slow
// iteration for very large numbers. We need to be able
// to at least truncate math.MaxFloat64 to 0 for the largest
// possible values.
if round < -decimal.ExponentLimit {
round = -decimal.ExponentLimit
} else if round > 30 {
round = 30
}
return round
}
func roundSigned(v int64, round int64) int64 {
if round >= 0 {
return v
}
round = clampRounding(round)
if v == 0 {
return 0
}
for i := round; i < -1 && v != 0; i++ {
v /= 10
}
if v == 0 {
return 0
}
if v%10 <= -5 {
v -= 10
} else if v%10 >= 5 {
v += 10
}
v /= 10
for i := round; i < 0; i++ {
v *= 10
}
return v
}
func roundUnsigned(v uint64, round int64) uint64 {
if round >= 0 {
return v
}
round = clampRounding(round)
if v == 0 {
return 0
}
for i := round; i < -1 && v != 0; i++ {
v /= 10
}
if v == 0 {
return 0
}
if v%10 >= 5 {
v += 10
}
v /= 10
for i := round; i < 0; i++ {
v *= 10
}
return v
}
func (call *builtinRound) eval(env *ExpressionEnv) (eval, error) {
arg, err := call.arg1(env)
if err != nil {
return nil, err
}
if arg == nil {
return nil, nil
}
round := int64(0)
if len(call.Arguments) > 1 {
d, err := call.Arguments[1].eval(env)
if err != nil {
return nil, err
}
if d == nil {
return nil, nil
}
switch d := d.(type) {
case *evalUint64:
round = int64(d.u)
if d.u > math.MaxInt64 {
round = math.MaxInt64
}
default:
round = evalToInt64(d).i
}
}
switch arg := arg.(type) {
case *evalInt64:
return newEvalInt64(roundSigned(arg.i, round)), nil
case *evalUint64:
return newEvalUint64(roundUnsigned(arg.u, round)), nil
case *evalDecimal:
if arg.dec.IsZero() {
return arg, nil
}
if round == 0 {
return newEvalDecimalWithPrec(arg.dec.Round(0), 0), nil
}
round = clampRounding(round)
digit := int32(round)
if digit < 0 {
digit = 0
}
if digit > arg.length {
digit = arg.length
}
rounded := arg.dec.Round(int32(round))
if rounded.IsZero() {
return newEvalDecimalWithPrec(decimal.Zero, 0), nil
}
return newEvalDecimalWithPrec(rounded, digit), nil
case *evalFloat:
if arg.f == 0.0 {
return arg, nil
}
if round == 0 {
return newEvalFloat(math.Round(arg.f)), nil
}
round = clampRounding(round)
f := math.Pow(10, float64(round))
if f == 0 {
return newEvalFloat(0), nil
}
return newEvalFloat(math.Round(arg.f*f) / f), nil
default:
v, _ := evalToFloat(arg)
if v.f == 0.0 {
return v, nil
}
if round == 0 {
return newEvalFloat(math.Round(v.f)), nil
}
round = clampRounding(round)
f := math.Pow(10, float64(round))
if f == 0 {
return newEvalFloat(0), nil
}
return newEvalFloat(math.Round(v.f*f) / f), nil
}
}
func (expr *builtinRound) compile(c *compiler) (ctype, error) {
arg, err := expr.Arguments[0].compile(c)
if err != nil {
return ctype{}, err
}