-
Notifications
You must be signed in to change notification settings - Fork 65
/
decimal.tcl
executable file
·1741 lines (1586 loc) · 39 KB
/
decimal.tcl
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
package require Tcl 8.5
package provide math::decimal 1.0.3
#
# Copyright 2011, 2013 Mark Alston. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Mark Alston ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL Mark Alston OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# decimal.tcl --
#
# Tcl implementation of a General Decimal Arithmetic as defined
# by the IEEE 754 standard as given on http:://speleotrove.com/decimal
#
# Decimal numbers are defined as a list of sign mantissa exponent
#
# The following operations are current implemented:
#
# fromstr tostr -- for converting to and from decimal numbers.
#
# add subtract divide multiply abs compare -- basic operations
# max min plus minus copynegate copysign is-zero is-signed
# is-NaN is-infinite is-finite
#
# round_half_even round_half_up round_half_down -- rounding methods
# round_down round_up round_floor round_ceiling
# round_05up
#
# By setting the extended variable to 0 you get the behavior of the decimal
# subset arithmetic X3.274 as defined on
# http://speleotrove.com/decimal/dax3274.html#x3274
#
# This package passes all tests in test suites:
# http://speleotrove.com/decimal/dectest.html
# and http://speleotrove.com/decimal/dectest0.html
#
# with the following exceptions:
#
# This version fails some tests that require setting the max
# or min exponent to force truncation or rounding.
#
# This version fails some tests which require the sign of zero to be set
# correctly during rounding
#
# This version cannot handle sNaN's (Not sure that they are of any use for
# tcl programmers anyway.
#
# If you find errors in this code please let me know at
# mark at beernut dot com
#
# Decimal --
# Namespace for the decimal arithmetic procedures
#
namespace eval ::math::decimal {
variable precision 20
variable maxExponent 999
variable minExponent -998
variable tinyExponent [expr {$minExponent - ($precision - 1)}]
variable rounding half_up
variable extended 1
# Some useful variables to set.
variable zero [list 0 0 0]
variable one [list 0 1 0]
variable ten [list 0 1 1]
variable onehundred [list 0 1 2]
variable minusone [list 1 1 0]
namespace export tostr fromstr setVariable getVariable\
add + subtract - divide / multiply * \
divide-int remainder \
fma fused-multiply-add \
plus minus copynegate negate copysign \
abs compare max min \
is-zero is-signed is-NaN is-infinite is-finite \
round_half_even round_half_up round_half_down \
round_down round_up round_floor round_ceiling round_05up
}
# setVariable
# Set the desired variable
#
# Arguments:
# variable setting
#
# Result:
# None
#
proc ::math::decimal::setVariable {variable setting} {
variable rounding
variable precision
variable extended
variable maxExponent
variable minExponent
variable tinyExponent
switch -nocase -- $variable {
rounding {set rounding $setting}
precision {set precision $setting}
extended {set extended $setting}
maxExponent {set maxExponent $setting}
minExponent {
set minExponent $setting
set tinyExponent [expr {$minExponent - ($precision - 1)}]
}
default {}
}
}
# setVariable
# Set the desired variable
#
# Arguments:
# variable setting
#
# Result:
# None
#
proc ::math::decimal::getVariable {variable} {
variable rounding
variable precision
variable extended
variable maxExponent
variable minExponent
switch -- $variable {
rounding {return $rounding}
precision {return $precision}
extended {return $extended}
maxExponent {return $maxExponent}
minExponent {return $minExponent}
default {}
}
}
# add or +
# Add two numbers
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# Sum of both (rescaled)
#
proc ::math::decimal::add {a b {rescale 1}} {
return [+ $a $b $rescale]
}
proc ::math::decimal::+ {a b {rescale 1}} {
variable extended
variable rounding
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if {!$extended} {
if {$ma == 0 } {
return $b
}
if {$mb == 0 } {
return $a
}
}
if { $ma eq "NaN" || $mb eq "NaN" } {
return [list 0 "NaN" 0]
}
if { $ma eq "Inf" || $mb eq "Inf" } {
if { $ma ne "Inf" } {
return $b
} elseif { $mb ne "Inf" } {
return $a
} elseif { $sb != $sa } {
return [list 0 "NaN" 0]
} else {
return $a
}
}
if { $ea > $eb } {
set ma [expr {$ma * 10 ** ($ea-$eb)}]
set er $eb
} else {
set mb [expr {$mb * 10 ** ($eb-$ea)}]
set er $ea
}
if { $sa == $sb } {
# Both are either postive or negative
# Sign remains the same.
set mr [expr {$ma + $mb}]
set sr $sa
} else {
# one is negative and one is positive.
# Set sign to the same as the larger number
# and subract the smaller from the larger.
if { $ma > $mb } {
set sr $sa
set mr [expr {$ma - $mb}]
} elseif { $mb > $ma } {
set sr $sb
set mr [expr {$mb - $ma}]
} else {
if { $rounding == "floor" } {
set sr 1
} else {
set sr 0
}
set mr 0
}
}
if { $rescale } {
return [Rescale [list $sr $mr $er]]
} else {
return [list $sr $mr $er]
}
}
# copynegate --
# Takes one operand and returns a copy with the sign inverted.
# In this implementation it works nearly the same as minus
# but is probably much faster. The main difference is that no
# rescaling is done.
#
#
# Arguments:
# a operand
#
# Result:
# a with sign flipped
#
proc ::math::decimal::negate { a } {
return [copynegate $a]
}
proc ::math::decimal::copynegate { a } {
lset a 0 [expr {![lindex $a 0]}]
return $a
}
# copysign --
# Takes two operands and returns a copy of the first with the
# sign set to the sign of the second.
#
#
# Arguments:
# a operand
# b operand
#
# Result:
# b with a's sign
#
proc ::math::decimal::copysign { a b } {
lset a 0 [lindex $b 0]
return $a
}
# minus --
# subtract 0 $a
#
# Note: does not pass all tests on extended mode.
#
# Arguments:
# a operand
#
# Result:
# 0 - $a
#
proc ::math::decimal::minus { a } {
return [- [list 0 0 0] $a]
}
# plus --
# add 0 $a
#
# Note: does not pass all tests on extended mode.
#
# Arguments:
# a operand
#
# Result:
# 0 + $a
#
proc ::math::decimal::plus {a} {
return [+ [list 0 0 0] $a]
}
# subtract or -
# Subtract two numbers (or unary minus)
#
# Arguments:
# a First operand
# b Second operand (optional)
#
# Result:
# Sum of both (rescaled)
#
proc ::math::decimal::subtract {a {b {}} {rescale 1}} {
return [- $a $b]
}
proc ::math::decimal::- {a {b {}} {rescale 1}} {
variable extended
if {!$extended} {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if {$ma == 0 } {
lset b 0 [expr {![lindex $b 0]}]
return $b
}
if {$mb == 0 } {
return $a
}
}
if { $b == {} } {
lset a 0 [expr {![lindex $a 0]}]
return $a
} else {
lset b 0 [expr {![lindex $b 0]}]
return [+ $a $b $rescale]
}
}
# compare
# Compare two numbers.
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# 1 if a is larger than b
# 0 if a is equal to b
# -1 if a is smaller than b.
#
proc ::math::decimal::compare {a b} {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $sa != $sb } {
if {$ma != 0 } {
set ma 1
set ea 0
} elseif { $mb != 0 } {
set mb 1
set eb 0
} else {
return 0
}
}
if { $ma eq "Inf" && $mb eq "Inf" } {
if { $sa == $sb } {
return 0
} elseif { $sa > $sb } {
return -1
} else {
return 1
}
}
set comparison [- [list $sa $ma $ea] [list $sb $mb $eb] 0]
if { [lindex $comparison 0] && [lindex $comparison 1] != 0 } {
return -1
} elseif { [lindex $comparison 1] == 0 } {
return 0
} else {
return 1
}
}
# min
# Return the smaller of two numbers
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# smaller of a or b
#
proc ::math::decimal::min {a b} {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $sa != $sb } {
if {$ma != 0 } {
set ma 1
set ea 0
} elseif { $mb != 0 } {
set mb 1
set eb 0
}
}
if { $ma eq "Inf" && $mb eq "Inf" } {
if { $sa == $sb } {
return [list $sa "Inf" 0]
} else {
return [list 1 "Inf" 0]
}
}
set comparison [compare [list $sa $ma $ea] [list $sb $mb $eb]]
if { $comparison == 1 } {
return [Rescale $b]
} elseif { $comparison == -1 } {
return [Rescale $a]
} elseif { $sb != $sa } {
if { $sa } {
return [Rescale $a]
} else {
return [Rescale $b]
}
} elseif { $sb && $eb > $ea } {
# Both are negative and the same numerically. So return the one with the largest exponent.
return [Rescale $b]
} elseif { $sb } {
# Negative with $eb < $ea now.
return [Rescale $a]
} elseif { $ea > $eb } {
# Both are positive so return the one with the smaller
return [Rescale $b]
} else {
return [Rescale $a]
}
}
# max
# Return the larger of two numbers
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# larger of a or b
#
proc ::math::decimal::max {a b} {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $sa != $sb } {
if {$ma != 0 } {
set ma 1
set ea 0
} elseif { $mb != 0 } {
set mb 1
set eb 0
}
}
if { $ma eq "Inf" && $mb eq "Inf" } {
if { $sa == $sb } {
return [list $sa "Inf" 0]
} else {
return [list 0 "Inf" 0]
}
}
set comparison [compare [list $sa $ma $ea] [list $sb $mb $eb]]
if { $comparison == 1 } {
return [Rescale $a]
} elseif { $comparison == -1 } {
return [Rescale $b]
} elseif { $sb != $sa } {
if { $sa } {
return [Rescale $b]
} else {
return [Rescale $a]
}
} elseif { $sb && $eb > $ea } {
# Both are negative and the same numerically. So return the one with the smallest exponent.
return [Rescale $a]
} elseif { $sb } {
# Negative with $eb < $ea now.
return [Rescale $b]
} elseif { $ea > $eb } {
# Both are positive so return the one with the larger exponent
return [Rescale $a]
} else {
return [Rescale $b]
}
}
# maxmag -- max-magnitude
# Return the larger of two numbers ignoring their signs.
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# larger of a or b ignoring their signs.
#
proc ::math::decimal::maxmag {a b} {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $ma eq "Inf" && $mb eq "Inf" } {
if { $sa == 0 || $sb == 0 } {
return [list 0 "Inf" 0]
} else {
return [list 1 "Inf" 0]
}
}
set comparison [compare [list 0 $ma $ea] [list 0 $mb $eb]]
if { $comparison == 1 } {
return [Rescale $a]
} elseif { $comparison == -1 } {
return [Rescale $b]
} elseif { $sb != $sa } {
if { $sa } {
return [Rescale $b]
} else {
return [Rescale $a]
}
} elseif { $sb && $eb > $ea } {
# Both are negative and the same numerically. So return the one with the smallest exponent.
return [Rescale $a]
} elseif { $sb } {
# Negative with $eb < $ea now.
return [Rescale $b]
} elseif { $ea > $eb } {
# Both are positive so return the one with the larger exponent
return [Rescale $a]
} else {
return [Rescale $b]
}
}
# minmag -- min-magnitude
# Return the smaller of two numbers ignoring their signs.
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# smaller of a or b ignoring their signs.
#
proc ::math::decimal::minmag {a b} {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $ma eq "Inf" && $mb eq "Inf" } {
if { $sa == 1 || $sb == 1 } {
return [list 1 "Inf" 0]
} else {
return [list 0 "Inf" 0]
}
}
set comparison [compare [list 0 $ma $ea] [list 0 $mb $eb]]
if { $comparison == 1 } {
return [Rescale $b]
} elseif { $comparison == -1 } {
return [Rescale $a]
} else {
# They compared the same so now we use a normal comparison including the signs. This is per the specs.
if { $sa > $sb } {
return [Rescale $a]
} elseif { $sb > $sa } {
return [Rescale $b]
} elseif { $sb && $eb > $ea } {
# Both are negative and the same numerically. So return the one with the largest exponent.
return [Rescale $b]
} elseif { $sb } {
# Negative with $eb < $ea now.
return [Rescale $a]
} elseif { $ea > $eb } {
return [Rescale $b]
} else {
return [Rescale $a]
}
}
}
# fma - fused-multiply-add
# Takes three operands. Multiplies the first two and then adds the third.
# Only one rounding (Rescaling) takes place at the end instead of after
# both the multiplication and again after the addition.
#
# Arguments:
# a First operand
# b Second operand
# c Third operand
#
# Result:
# (a*b)+c
#
proc ::math::decimal::fused-multiply-add {a b c} {
return [fma $a $b $c]
}
proc ::math::decimal::fma {a b c} {
return [+ $c [* $a $b 0]]
}
# multiply or *
# Multiply two numbers
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# Product of both (rescaled)
#
proc ::math::decimal::multiply {a b {rescale 1}} {
return [* $a $b $rescale]
}
proc ::math::decimal::* {a b {rescale 1}} {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $ma eq "NaN" || $mb eq "NaN" } {
return [list 0 "NaN" 0]
}
set sr [expr {$sa^$sb}]
if { $ma eq "Inf" || $mb eq "Inf" } {
if { $ma == 0 || $mb == 0 } {
return [list 0 "NaN" 0]
} else {
return [list $sr "Inf" 0]
}
}
set mr [expr {$ma * $mb}]
set er [expr {$ea + $eb}]
if { $rescale } {
return [Rescale [list $sr $mr $er]]
} else {
return [list $sr $mr $er]
}
}
# divide or /
# Divide two numbers
#
# Arguments:
# a First operand
# b Second operand
#
# Result:
# Quotient of both (rescaled)
#
proc ::math::decimal::divide {a b {rescale 1}} {
return [/ $a $b]
}
proc ::math::decimal::/ {a b {rescale 1}} {
variable precision
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $ma eq "NaN" || $mb eq "NaN" } {
return [list 0 "NaN" 0]
}
set sr [expr {$sa^$sb}]
if { $ma eq "Inf" } {
if { $mb ne "Inf"} {
return [list $sr "Inf" 0]
} else {
return [list 0 "NaN" 0]
}
}
if { $mb eq "Inf" } {
if { $ma ne "Inf"} {
return [list $sr 0 0]
} else {
return [list 0 "NaN" 0]
}
}
if { $mb == 0 } {
if { $ma == 0 } {
return [list 0 "NaN" 0]
} else {
return [list $sr "Inf" 0]
}
}
set adjust 0
set mr 0
if { $ma == 0 } {
set er [expr {$ea - $eb}]
return [list $sr 0 $er]
}
if { $ma < $mb } {
while { $ma < $mb } {
set ma [expr {$ma * 10}]
incr adjust
}
} elseif { $ma >= $mb * 10 } {
while { $ma >= [expr {$mb * 10}] } {
set mb [expr {$mb * 10}]
incr adjust -1
}
}
while { 1 } {
while { $mb <= $ma } {
set ma [expr {$ma - $mb}]
incr mr
}
if { ( $ma == 0 && $adjust >= 0 ) || [string length $mr] > $precision + 1 } {
break
} else {
set ma [expr {$ma * 10}]
set mr [expr {$mr * 10}]
incr adjust
}
}
set er [expr {$ea - ($eb + $adjust)}]
if { $rescale } {
return [Rescale [list $sr $mr $er]]
} else {
return [list $sr $mr $er]
}
}
# divideint -- Divide integer
# Divide a by b and return the integer part of the division.
#
# Basically, if we send a and b to the divideint (which returns i)
# and remainder function (which returns r) then the following is true:
# a = i*b + r
#
# Arguments:
# a First operand
# b Second operand
#
#
proc ::math::decimal::divideint { a b } {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
set sr [expr {$sa^$sb}]
if { $sr == 1 } {
set sign_string "-"
} else {
set sign_string ""
}
if { ($ma eq "NaN" || $mb eq "NaN") || ($ma == 0 && $mb == 0 ) } {
return "NaN"
}
if { $ma eq "Inf" || $mb eq "Inf" } {
if { $ma eq $mb } {
return "NaN"
} elseif { $mb eq "Inf" } {
return "${sign_string}0"
} else {
return "${sign_string}Inf"
}
}
if { $mb == 0 } {
return "${sign_string}Inf"
}
if { $mb == "Inf" } {
return "${sign_string}0"
}
set adjust [expr {abs($ea - $eb)}]
if { $ea < $eb } {
set a_adjust 0
set b_adjust $adjust
} elseif { $ea > $eb } {
set b_adjust 0
set a_adjust $adjust
} else {
set a_adjust 0
set b_adjust 0
}
set integer [expr {($ma*10**$a_adjust)/($mb*10**$b_adjust)}]
return $sign_string$integer
}
# remainder -- Remainder from integer division.
# Divide a by b and return the remainder part of the division.
#
# Basically, if we send a and b to the divideint (which returns i)
# and remainder function (which returns r) then the following is true:
# a = i*b + r
#
# Arguments:
# a First operand
# b Second operand
#
#
proc ::math::decimal::remainder { a b } {
foreach {sa ma ea} $a {break}
foreach {sb mb eb} $b {break}
if { $sa == 1 } {
set sign_string "-"
} else {
set sign_string ""
}
if { ($ma eq "NaN" || $mb eq "NaN") || ($ma == 0 && $mb == 0 ) } {
if { $mb eq "NaN" && $mb ne $ma } {
if { $sb == 1 } {
set sign_string "-"
} else {
set sign_string ""
}
return "${sign_string}NaN"
} elseif { $ma eq "NaN" } {
return "${sign_string}NaN"
} else {
return "NaN"
}
} elseif { $mb == 0 } {
return "NaN"
}
if { $ma eq "Inf" || $mb eq "Inf" } {
if { $ma eq $mb } {
return "NaN"
} elseif { $mb eq "Inf" } {
return [tostr $a]
} else {
return "NaN"
}
}
if { $mb == 0 } {
return "${sign_string}Inf"
}
if { $mb == "Inf" } {
return "${sign_string}0"
}
lset a 0 0
lset b 0 0
if { $mb == 0 } {
return "${sign_string}Inf"
}
if { $mb == "Inf" } {
return "${sign_string}0"
}
set adjust [expr {abs($ea - $eb)}]
if { $ea < $eb } {
set a_adjust 0
set b_adjust $adjust
} elseif { $ea > $eb } {
set b_adjust 0
set a_adjust $adjust
} else {
set a_adjust 0
set b_adjust 0
}
set integer [expr {($ma*10**$a_adjust)/($mb*10**$b_adjust)}]
set remainder [tostr [- $a [* [fromstr $integer] $b 0]]]
return $sign_string$remainder
}
# abs --
# Returns the Absolute Value of a number
#
# Arguments:
# Number in the form of {sign mantisse exponent}
#
# Result:
# Absolute value (as a list)
#
proc ::math::decimal::abs {a} {
lset a 0 0
return [Rescale $a]
}
# Rescale --
# Rescale the number (using proper rounding)
#
# Arguments:
# a Number in decimal format
#
# Result:
# Rescaled number
#
proc ::math::decimal::Rescale { a } {
variable precision
variable rounding
variable maxExponent
variable minExponent
variable tinyExponent
foreach {sign mantisse exponent} $a {break}
set man_length [string length $mantisse]
set adjusted_exponent [expr {$exponent + ($man_length -1)}]
if { $adjusted_exponent < $tinyExponent } {
set mantisse [lindex [round_$rounding [list $sign $mantisse [expr {abs($tinyExponent) - abs($adjusted_exponent)}]] 0] 1]
return [list $sign $mantisse $tinyExponent]
} elseif { $adjusted_exponent > $maxExponent } {
if { $mantisse == 0 } {
return [list $sign 0 $maxExponent]
} else {
switch -- $rounding {
half_even -
half_up { return [list $sign "Inf" 0] }
down -
05up {
return [list $sign [string repeat 9 $precision] $maxExponent]
}
ceiling {
if { $sign } {
return [list $sign [string repeat 9 $precision] $maxExponent]
} else {
return [list 0 "Inf" 0]
}
}
floor {
if { !$sign } {
return [list $sign [string repeat 9 $precision] $maxExponent]
} else {
return [list 1 "Inf" 0]
}
}
default { }
}
}
}
if { $man_length <= $precision } {
return [list $sign $mantisse $exponent]
}
set mantisse [lindex [round_$rounding [list $sign $mantisse [expr {$precision - $man_length}]] 0] 1]
set exponent [expr {$exponent + ($man_length - $precision)}]
# it is possible now that our rounding gave us a new digit in our mantisse
# example rounding 999.9 to 1 digits with precision 3 will give us
# 1000 back.
# This can only happen by adding a zero on the end of our mantisse however.
# So we just chomp it off.
set man_length_now [string length $mantisse]
if { $man_length_now > $precision } {
set mantisse [string range $mantisse 0 end-1]
incr exponent
# Check again to see if we have overflowed