-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFixedPointTest.lua
1450 lines (1212 loc) · 53.9 KB
/
FixedPointTest.lua
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
-- Roblox only
local FixedPoint = require(game:GetService("ReplicatedStorage").Physics.FixedPoint)
-- default rounding policy
local drp = "HALF_AWAY_FROM_ZERO"
local rng = Random.new()
local randfn = function(fix)
return rng:NextInteger(-fix.OVERFLOW_VAL + 1, fix.OVERFLOW_VAL + 1)
end
local randfn2 = function(u, l)
return rng:NextInteger(u, l)
end
-- changing radius below 7 will cause problems
local test_radius = 14
-- changing radix point will cause problems, because rounding testing is sort of embedded in other test contexts where it is used
-- this radix point is not always used, like for example in Trigonometry
local test_radix_point = 4
FixedPointTest = {
Consistency = function()
local fix = FixedPoint:new(test_radius, test_radix_point, drp)
print("Test: convertInt has identity on zero")
assert(fix:convertInt(0) == 0)
print("Good")
end,
Addition = function()
local fix = FixedPoint:new(test_radius, test_radix_point, drp)
print("Fixed point context: " .. fix:describe())
local ONE = fix:convertInt(1)
local BIG = fix.OVERFLOW_VAL - 1
local BIG_n = -fix.OVERFLOW_VAL + 1
local ONE_n = fix:convertInt(-1)
local TWO = fix:convertInt(2)
local FOUR = fix:convertInt(4)
local HUNDRED = fix:convertInt(100)
local ZERO = fix:convertInt(0)
local RAND1, RAND2, RAND3 = fix:genRand(randfn) % fix.RADIX_RHS_VAL, fix:genRand(randfn) % fix.RADIX_RHS_VAL, math.fmod(fix:genRand(randfn), fix.RADIX_RHS_VAL)
local function str(v)
return fix:toString(v)
end
print("Test: one plus one")
local result1 = fix:add(ONE, ONE)
print(str(ONE) .. " + " .. str(ONE) .. " = " .. str(result1))
assert(result1 == TWO)
print("Good")
print("Test: one plus negative one")
local result2 = fix:add(ONE, ONE_n)
print(str(ONE) .. " + (" .. str(ONE_n) ..") = ".. str(result2))
assert(result2 == ZERO)
print("Good")
print("Test: overflow error")
print("Attempting: " .. str(BIG) .. " + " .. str(ONE))
assert(not pcall(function()
print(fix:toString(fix:add(BIG, ONE)))
end))
print("Good")
print("Attempting: (" .. str(BIG_n) .. ") + (".. str(ONE_n) .. ")")
assert(not pcall(function()
fix:add(BIG_n, ONE_n)
end))
print("Good")
print("Test: associativity")
print("Attempting simple check.")
local result3_1 = fix:add(fix:add(ONE, TWO), ONE)
print(str(ONE) .. " + ( " .. str(TWO) .. " + " .. str(ONE) .. " ) = " .. str(result3_1))
local result3_2 = fix:add(ONE, fix:add(TWO, ONE))
print("( " .. str(ONE) .. " + " .. str(TWO) .. " ) + " .. str(ONE) .. " = " .. str(result3_2))
assert(result3_1 == result3_2)
assert(result3_1 == FOUR)
print("Good")
print("Attempting randomized check.")
local result3_3 = fix:add(fix:add(RAND1, RAND2), RAND3)
print(str(RAND1) .. " + ( " .. str(RAND2) .. " + " .. str(RAND3) .. " ) = " .. str(result3_3))
local result3_4 = fix:add(RAND1, fix:add(RAND2, RAND3))
print("( " .. str(RAND1) .. " + " .. str(RAND2) .. " ) + " .. str(RAND3) .. " = " .. str(result3_4))
assert(result3_3 == result3_4)
print("Good")
print("Test: iterated addition")
local N = ZERO
for i = 1, 100 do
N = fix:add(N, ONE)
end
assert(N == HUNDRED)
print("Good")
end,
Subtraction = function()
local fix = FixedPoint:new(test_radius, 4, drp)
print("Fixed point context: " .. fix:describe())
local ONE = fix:convertInt(1)
local BIG = fix.OVERFLOW_VAL - 1
local BIG_n = -fix.OVERFLOW_VAL + 1
local ONE_n = fix:convertInt(-1)
local TWO = fix:convertInt(2)
local FOUR = fix:convertInt(4)
local HUNDRED = fix:convertInt(100)
local ZERO = fix:convertInt(0)
local function str(v)
return fix:toString(v)
end
print("Test: one minus one")
local result1 = fix:sub(ONE, ONE)
print(str(ONE) .. " - " .. str(ONE) .. " = " .. str(result1))
assert(result1 == ZERO)
print("Good")
print("Test: one minus negative one")
local result2 = fix:sub(ONE, ONE_n)
print(str(ONE) .. " - (" .. str(ONE_n) ..") = ".. str(result2))
assert(result2 == TWO)
print("Good")
print("Test: overflow error")
print("Attempting: " .. str(BIG_n) .. " - " .. str(ONE))
assert(not pcall(function()
print(fix:toString(fix:sub(BIG_n, ONE)))
end))
print("Good")
print("Attempting: (" .. str(BIG) .. ") - (".. str(ONE_n) .. ")")
assert(not pcall(function()
fix:sub(BIG, ONE_n)
end))
print("Good")
print("Test: non-associativity")
print("Attempting simple check.")
local result3_1 = fix:sub(fix:sub(ONE, TWO), ONE)
print("( " .. str(ONE) .. " - " .. str(TWO) .. " ) - " .. str(ONE) .. " = " .. str(result3_1))
local result3_2 = fix:sub(ONE, fix:sub(TWO, ONE))
print(str(ONE) .. " - ( " .. str(TWO) .. " - " .. str(ONE) .. " ) = " .. str(result3_2))
assert(result3_1 ~= result3_2)
assert(result3_1 == -TWO)
assert(result3_2 == ZERO)
print("Good")
print("Test: iterated subtraction")
local N = ZERO
for i = 1, 100 do
N = fix:sub(N, ONE)
end
assert(N == -HUNDRED)
print("Good")
end,
Multiplication = function()
local fix = FixedPoint:new(test_radius, test_radix_point, drp)
print("Fixed point context: " .. fix:describe())
local ONE = fix:convertInt(1)
local OVF_TENTH = fix.OVERFLOW_VAL / 10
local ONE_n = fix:convertInt(-1)
local TWO = fix:convertInt(2)
local FOUR = fix:convertInt(4)
local HUNDRED = fix:convertInt(100)
local ZERO = fix:convertInt(0)
local HALF = fix.RADIX_RHS_VAL / 2
local THREE_HALFS = fix:add(ONE, HALF)
local THREE_FOURTHS = THREE_HALFS / 2
local HUNDREDTH = fix.RADIX_RHS_VAL / 100
local TENTHOUSANDTH = fix.RADIX_RHS_VAL / 10000
local NINE = fix:convertInt(9)
local RAND1, RAND2, RAND3 = fix:genRand(randfn) % fix.RADIX_RHS_VAL, fix:genRand(randfn) % fix.RADIX_RHS_VAL, math.fmod(fix:genRand(randfn), fix.RADIX_RHS_VAL)
local function str(v)
return fix:toString(v)
end
print("Test: one times one")
local result1 = fix:mult(ONE, ONE)
print(str(ONE) .. " * " .. str(ONE) .. " = " .. str(result1))
assert(result1 == ONE)
print("Good")
print("Test: one times negative one")
local result2 = fix:mult(ONE, ONE_n)
print(str(ONE) .. " * ( " .. str(ONE_n) .. " ) = " .. str(result2))
assert(result2 == ONE_n)
print("Good")
print("Test: negative one times negative one")
local result3 = fix:mult(ONE_n, ONE_n)
print("( ".. str(ONE_n) .. " ) * ( " .. str(ONE_n) .. " ) = " .. str(result3))
assert(result3 == ONE)
print("Good")
print("Test: one times a half")
local result4 = fix:mult(ONE, HALF)
print(str(ONE) .. " * " .. str(HALF) .. " = " .. str(result4))
assert(result4 == HALF)
print("Good")
print("Test: one point five times a half")
local result5 = fix:mult(THREE_HALFS, HALF)
print(str(THREE_HALFS) .. " * " .. str(HALF) .. " = " .. str(result5))
assert(result5 == THREE_FOURTHS)
print("Good")
print("Test: a hundredth times a hundredth")
local result6 = fix:mult(HUNDREDTH, HUNDREDTH)
print(str(HUNDREDTH) .. " * " .. str(HUNDREDTH) .. " = " .. str(result6))
assert(result6 == TENTHOUSANDTH)
print("Good")
print("Test: a negative tenthousandth times a half with HALF_AWAY_FROM_ZERO rounding policy")
local result7 = fix:mult(-TENTHOUSANDTH, HALF)
print(str(-TENTHOUSANDTH) .. " * " .. str(HALF) .. " = " .. str(result7))
assert(result7 == -TENTHOUSANDTH)
print("Good")
print("Test: a negative tenthousandth times a half with HALF_UP rounding policy")
local result8 = fix:mult(-TENTHOUSANDTH, HALF, "HALF_UP")
print(str(-TENTHOUSANDTH) .. " * " .. str(HALF) .. " = " .. str(result8))
assert(result8 == ZERO)
print("Good")
print("Test: a negative tenthousandth times a negative half with HALF_UP rounding policy")
local result9 = fix:mult(-TENTHOUSANDTH, -HALF, "HALF_UP")
print(str(-TENTHOUSANDTH) .. " * " .. str(-HALF) .. " = " .. str(result9))
assert(result9 == TENTHOUSANDTH)
print("Good")
print("Test: three tenthousandths times a half with HALF_TO_EVEN rounding policy")
local result10 = fix:mult(3 * TENTHOUSANDTH, HALF, "HALF_TO_EVEN")
print(str(3 * TENTHOUSANDTH) .. " * " .. str(HALF) .. " = " .. str(result10))
assert(result10 == 2 * TENTHOUSANDTH)
print("Good")
print("Test: five tenthousandths times a half with HALF_TO_EVEN rounding policy")
local result10 = fix:mult(5 * TENTHOUSANDTH, HALF, "HALF_TO_EVEN")
print(str(5 * TENTHOUSANDTH) .. " * " .. str(HALF) .. " = " .. str(result10))
assert(result10 == 2 * TENTHOUSANDTH)
print("Good")
print("Test: tenth of overflow value times nine")
local result11 = fix:mult(OVF_TENTH, NINE)
print(str(OVF_TENTH) .. " * " .. str(NINE) .. " = " .. str(result11))
assert(result11 == 9 * OVF_TENTH)
print("Good")
print("Test: tenth of overflow and tenthousandth times nine and hundredth with default policy")
local result12 = fix:mult(OVF_TENTH+TENTHOUSANDTH, NINE+HUNDREDTH)
print(str(OVF_TENTH+TENTHOUSANDTH) .. " * " .. str(NINE+HUNDREDTH) .. " = " .. str(result12))
assert(result12 == 90100000000009)
print("Good")
print("Test: tenth of overflow and tenthousandth times nine and hundredth with NEXT_INT policy")
local result12 = fix:mult(OVF_TENTH+TENTHOUSANDTH, NINE+HUNDREDTH, "NEXT_INT")
print(str(OVF_TENTH+TENTHOUSANDTH) .. " * " .. str(NINE+HUNDREDTH) .. " = " .. str(result12))
assert(result12 == 90100000000010)
print("Good")
print("Test: overflow error")
print("Attempting: tenth of overflow times ten")
assert(not pcall(function()
fix:mult(OVF_TENTH, NINE+ONE)
end))
print("Good")
print("Attempting: negative tenth of overflow times ten")
assert(not pcall(function()
fix:mult(-OVF_TENTH, NINE+ONE)
end))
print("Good")
print("Attempting: overflow times a half")
assert(not pcall(function()
fix:mult(fix.OVERFLOW_VAL, HALF)
end))
print("Good")
print("Test: near-associativity")
print("Attempting: associativity margin check with random fractional numbers using HALF_TO_EVEN")
local result13_1 = fix:mult(fix:mult(RAND1, RAND2, "HALF_TO_EVEN"), RAND3, "HALF_TO_EVEN")
local result13_2 = fix:mult(RAND1, fix:mult(RAND2, RAND3, "HALF_TO_EVEN"), "HALF_TO_EVEN")
print("( " .. str(RAND1) .. " * " .. str(RAND2) .. " ) * " .. str(RAND3) .. " = " .. str(result13_1))
print(str(RAND1) .. " * ( " .. str(RAND2) .. " * " .. str(RAND3) .. " ) = " .. str(result13_2))
print(">>> the associativity margin is " .. (result13_1 - result13_2))
assert(math.abs(result13_1 - result13_2) < 10)
print("Good")
print("Test: iterated multiplication")
local N = 1
local I = 0
for i = 1, 100 do
N = fix:mult(N, ONE)
end
assert(N == 1)
while N < OVF_TENTH do
N = fix:mult(N, TWO)
I = I + 1
end
print("N began at "..str(1).." and became " .. str(N) .. " after " .. I .. " iterated multiplications by 2")
assert(N == 2^44)
print("Good")
print("Test: near-distributivity")
print("Attempting: distributivity margin check with random fractional numbers using HALF_TO_EVEN")
local result14_1 = fix:mult(RAND1, fix:add(RAND2, RAND3), "HALF_TO_EVEN")
local result14_2 = fix:add(fix:mult(RAND1, RAND2, "HALF_TO_EVEN"), fix:mult(RAND1, RAND3, "HALF_TO_EVEN"))
print(str(RAND1) .. " * ( " ..str(RAND2) .. " + " .. str(RAND3) .. " ) = " .. str(result14_1))
print("( " .. str(RAND1) .. " * " .. str(RAND2) .. " ) + ( " .. str(RAND1) .. " * " .. str(RAND3) .. " ) = " .. str(result14_2))
print(">>> the distributivity margin is " .. (result14_1 - result14_2))
assert(math.abs(result14_1 - result14_2) < 10)
print("Good")
print("Test: edge cases")
print("Attempting: 0002069999.9999 * 0000001990.9999")
local EDGE1_1 = fix:asValue(2069999, 999900)
local EDGE1_2 = fix:asValue(1990, 999900)
local result15 = fix:mult(EDGE1_1, EDGE1_2)
print(str(EDGE1_1) .. " * " .. str(EDGE1_2) .. " = " .. str(result15))
assert(result15 == fix:asValue(4121369792, 800900))
print("Good")
print("Test: multOvf")
local fix9_3 = FixedPoint:new(9, 3, drp)
print("Notice: Switching fixed point context to " .. fix9_3:describe())
print("Attempting: overmultiply two large numbers in radius 9 radix 3")
local r16op1 = fix9_3:asValue(123456, 789000)
local r16op2 = fix9_3:asValue(987654, 321000)
local result16 = fix9_3:multOvf(r16op1, r16op2)
print("multOvf( " .. fix9_3:toString(r16op1) .. " , " .. fix9_3:toString(r16op2) .. " ) = " .. fix9_3:toString(result16))
assert(result16 == fix9_3:asValue(121932, 631000))
print("Good")
local fix14_6 = FixedPoint:new(14, 6, drp)
print("Notice: Switching fixed point context to " .. fix14_6:describe())
print("Attempting: overmultiply two large numbers in radius 14 radix 6")
local r17op1 = fix14_6:asValue(12345678, 901234)
local r17op2 = fix14_6:asValue(98765432, 109876)
local result17 = fix14_6:multOvf(r17op1, r17op2)
print("multOvf( " .. fix14_6:toString(r17op1) .. " , " .. fix14_6:toString(r17op2) .. " ) = " .. fix14_6:toString(result17))
assert(result17 == fix14_6:asValue(12193263, 113701))
print("Good")
print("Attempting: overmultiply near-overflow and one")
local r18op1 = fix14_6.OVERFLOW_VAL - 1
local r18op2 = fix14_6.RADIX_RHS_VAL
local result18 = fix14_6:multOvf(r18op1, r18op2)
print("multOvf( " .. fix14_6:toString(r18op1) .." , " .. fix14_6:toString(r18op2) .. " ) = " .. fix14_6:toString(result18))
assert(result18 == fix14_6.RADIX_RHS_VAL)
print("Good")
end,
Division = function()
local fix = FixedPoint:new(test_radius, test_radix_point, drp)
print("Fixed point context: " .. fix:describe())
local ONE = fix:convertInt(1)
local OVF_TENTH = fix.OVERFLOW_VAL / 10
local OVF_TENTHOUSANDTH = fix.OVERFLOW_VAL / 10000
local ONE_n = fix:convertInt(-1)
local TWO = fix:convertInt(2)
local FOUR = fix:convertInt(4)
local HUNDRED = fix:convertInt(100)
local ZERO = fix:convertInt(0)
local HALF = fix.RADIX_RHS_VAL / 2
local THREE_HALFS = fix:add(ONE, HALF)
local THREE_FOURTHS = THREE_HALFS / 2
local TENTH = fix.RADIX_RHS_VAL / 10
local HUNDREDTH = fix.RADIX_RHS_VAL / 100
local TENTHOUSANDTH = fix.RADIX_RHS_VAL / 10000
local NINE = fix:convertInt(9)
local RAND1, RAND2, RAND3, RAND4 = math.fmod(fix:genRand(randfn), fix.RADIX_RHS_VAL * 10),
math.fmod(fix:genRand(randfn), fix.RADIX_RHS_VAL * 10),
math.fmod(fix:genRand(randfn), fix.RADIX_RHS_VAL * 10),
math.fmod(fix:genRand(randfn), fix.RADIX_RHS_VAL * 10)
local BIGRAND1, BIGRAND2 = (RAND1 + fix.RADIX_RHS_VAL) * 1000000, (RAND2 + fix.RADIX_RHS_VAL) * 1000000
local SAFERAND3 = RAND3 + fix.RADIX_RHS_VAL
local function str(v)
return fix:toString(v)
end
print("Test: one by one")
local result1 = fix:div(ONE, ONE)
print(str(ONE) .. " / " .. str(ONE) .. " = " .. str(result1))
assert(result1 == ONE)
print("Good")
print("Test: negative one by one")
local result2 = fix:div(ONE_n, ONE)
print(str(ONE_n) .. " / " .. str(ONE) .. " = " .. str(result2))
assert(result2 == ONE_n)
print("Good")
print("Test: one by negative one")
local result3 = fix:div(ONE, ONE_n)
print(str(ONE) .. " / " .. str(ONE_n) .. " = " .. str(result3))
assert(result3 == ONE_n)
print("Good")
print("Test: division by zero")
print("Attempting: nonzero value by zero")
local result4 = fix:div(ONE, ZERO)
print(str(ONE) .. " / " .. str(ZERO) .. " = " .. str(result4))
assert(result4 == math.huge)
print("Good")
print("Attempting: zero by zero")
local result5 = fix:div(ZERO, ZERO)
print(str(ZERO) .. " / " .. str(ZERO) .. " = " .. str(result5))
assert(not (result5 < 0 or result5 >= 0))
print("Good")
print("Test: one by two")
local result6 = fix:div(ONE, TWO)
print(str(ONE) .. " / " .. str(TWO) .. " = " .. str(result6))
assert(result6 == HALF)
print("Good")
print("Test: tenth of overflow by two")
local result7 = fix:div(OVF_TENTH, TWO)
print(str(OVF_TENTH) .. " / " .. str(TWO) .. " = " .. str(result7))
assert(result7 == OVF_TENTH / 2)
print("Good")
print("Test: tenth of overflow by half")
local result8 = fix:div(OVF_TENTH, HALF)
print(str(OVF_TENTH) .. " / " .. str(HALF) .. " = " .. str(result8))
assert(result8 == 2 * OVF_TENTH)
print("Good")
print("Test: overflow error")
print("Attempting: tenth of overflow by tenth")
assert(not pcall(function()
fix:div(OVF_TENTH, TENTH)
end))
print("Good")
print("Attempting: tenthousandth of overflow by tenthousandth")
assert(not pcall(function()
fix:div(OVF_TENTHOUSANDTH, TENTHOUSANDTH)
end))
print("Good")
print("Attempting: tenth of overflow by negative hundredth")
assert(not pcall(function()
fix:div(OVF_TENTH, -HUNDREDTH)
end))
print("Good")
--[=[
print("Test: tenthousandth by ten with round policy NEXT_INT")
local result10 = fix:div(TENTHOUSANDTH, NINE+ONE, "NEXT_INT")
print(str(TENTHOUSANDTH) .. " / " .. str(NINE+ONE) .. " = " .. str(result10))
assert(result10 == TENTHOUSANDTH)
print("Good")
print("Test: iterated division")
local N = 2^40
for i = 1, 100 do
N = fix:div(N, ONE)
end
assert(N == 2^40)
print("Attempting: N = 2^40. divide by two until N = 1, checking for 2^i == N")
local I = 40
while N > 1 do
N = fix:div(N, TWO)
I = I - 1
assert(N == 2^I)
end
print("Good")
]=]
print("Test: near-distributivity")
print("Attempting: like denominators, using small random values, with HALF_TO_EVEN policy")
local result11_1 = fix:add(fix:div(RAND1, RAND3, "HALF_TO_EVEN"), fix:div(RAND2, RAND3, "HALF_TO_EVEN"))
local result11_2 = fix:div(fix:add(RAND1, RAND2), RAND3, "HALF_TO_EVEN")
print("( " .. str(RAND1) .. " / " .. str(RAND3) .. " ) + ( " .. str(RAND2) .. " / " .. str(RAND3) .. " ) = " .. str(result11_1))
print("( " .. str(RAND1) .. " + " .. str(RAND2) .. " ) / " .. str(RAND3) .. " = " .. str(result11_2))
print(">>> the distributivity margin is " .. (result11_1 - result11_2))
assert(math.abs(result11_1 - result11_2) < 10)
print("Good")
print("Attempting: like denominators, using big numerator small denominator, with HALF_TO_EVEN policy")
local result12_1 = fix:add(fix:div(BIGRAND1, SAFERAND3, "HALF_TO_EVEN"), fix:div(BIGRAND2, SAFERAND3, "HALF_TO_EVEN"))
local result12_2 = fix:div(fix:add(BIGRAND1, BIGRAND2), SAFERAND3, "HALF_TO_EVEN")
print("( " .. str(BIGRAND1) .. " / " .. str(SAFERAND3) .. " ) + ( " .. str(BIGRAND2) .. " / " .. str(SAFERAND3) .. " ) = " .. str(result12_1))
print("( " .. str(BIGRAND1) .. " + " .. str(BIGRAND2) .. " ) / " .. str(SAFERAND3) .. " = " .. str(result12_2))
print(">>> the distributivity margin is " .. (result12_1 - result12_2))
assert(math.abs(result12_1 - result12_2) < 10)
print("Good")
print("Attempting: unlike denominators, using small random values, HALF_TO_EVEN policy")
local result13_1 = fix:add(fix:div(RAND1, RAND3, "HALF_TO_EVEN"), fix:div(RAND2, RAND4, "HALF_TO_EVEN"))
local result13_2 = fix:div(fix:add(
fix:mult(RAND1, RAND4, "HALF_TO_EVEN"),
fix:mult(RAND2, RAND3, "HALF_TO_EVEN")), fix:mult(RAND3, RAND4, "HALF_TO_EVEN"), "HALF_TO_EVEN")
print("( " .. str(RAND1) .. " / " .. str(RAND3) .. " ) + ( " .. str(RAND2) .. " / " ..str(RAND4) .. " ) = " .. str(result13_1))
print("( ( " .. str(RAND1) .. " * " .. str(RAND4) .. " ) + ( " .. str(RAND2) .. " * " .. str(RAND3) .. " ) ) / ( " .. str(RAND3) .. " * " .. str(RAND4) .. " ) = " .. str(result13_2))
print(">>> the distributivity margin is " .. (result13_1 - result13_2))
assert(math.abs(result13_1 - result13_2) < 15)
print("Good")
print("Test: comparison with native Lua division")
print("Attempting: 2048 random pairs")
local r14_result_largest_error = 0
local r14_result_errormost_val_a, r14_result_errormost_val_b
for i = 1,2048 do
local r14RANDi_a = math.fmod(fix:genRand(randfn), fix.OVERFLOW_VAL)
local r14RANDi_b = math.fmod(fix:genRand(randfn), fix.OVERFLOW_VAL)
local r14_result_1 = fix:div(r14RANDi_a, r14RANDi_b) / ONE
local r14_result_2 = r14RANDi_a / r14RANDi_b
local r14_result_err = math.abs(r14_result_1 - r14_result_2)
if r14_result_largest_error < r14_result_err then
r14_result_largest_error = r14_result_err
r14_result_errormost_val_a = r14RANDi_a
r14_result_errormost_val_b = r14RANDi_b
end
end
print(">>> the largest error is " .. r14_result_largest_error)
print(">>> the expression producing the error is div( " .. str(r14_result_errormost_val_a) .. " , " .. str(r14_result_errormost_val_b) .. " ) = "
.. str(fix:div(r14_result_errormost_val_a, r14_result_errormost_val_b)))
assert(r14_result_largest_error < 1e-3)
print("Good")
end,
Exponential = function()
local fix = FixedPoint:new(test_radius, 6, drp)
print("Fixed point context: " .. fix:describe())
local ZERO = 0
local ONE = fix:convertInt(1)
local TWO = fix:convertInt(2)
local HALF = fix:asValue(0, 5e5)
local SQRT2 = fix.CONSTANT.SQRT2
local INV_SQRT2 = fix:asValue(0, 707107)
local INV_SQRT2_SCALED = fix:asValue(70710678, 118655)
local TENTH = ONE / 10
local THREE = fix:convertInt(3)
local THREE_TENTHS = 3 * TENTH
local TEN = 10 * ONE
local FIVE = 5 * ONE
local E = fix.CONSTANT.E
local INT_RAND = (1 + fix:genRand(randfn) % 25) * fix.RADIX_RHS_VAL
local INT_RAND_SQ = fix:mult(INT_RAND, INT_RAND)
local function str(v)
return fix:toString(v)
end
print("Test: basic sqrt properties")
print("Attempting: square root of 0")
local result_1 = fix:sqrt(ZERO)
print("sqrt( " .. str(ZERO) .. " ) = " .. str(result_1))
assert(result_1 == 0)
print("Good")
print("Attempting: square root of one")
local result_2 = fix:sqrt(fix.RADIX_RHS_VAL)
print("sqrt( " .. str(fix.RADIX_RHS_VAL) .. " ) = " .. str(result_2))
assert(result_2 == fix.RADIX_RHS_VAL)
print("Good")
print("Attempting: square root of a random square")
local result_3 = fix:sqrt(INT_RAND_SQ)
print("sqrt( " .. str(INT_RAND_SQ) .. " ) = " .. str(result_3))
assert(math.abs(result_3 - INT_RAND) < 400)
print("Good")
print("Attempting: square root of a negative value")
local result_4 = fix:sqrt(-INT_RAND_SQ)
print("sqrt( " .. str(-INT_RAND_SQ) .. ") = " .. str(result_4))
assert(result_4 ~= result_4)
print("Good")
print("Attempting: square root of largest value")
local result_5 = fix:sqrt(fix.OVERFLOW_VAL - 1)
print("sqrt( " .. str(fix.OVERFLOW_VAL - 1) .. " ) = " .. str(result_5))
-- test to see if result of re-squaring the root yields result with error within threshold of RADIX_RHS_VAL aka 1
assert(math.abs(fix:mult(result_5, result_5) - (fix.OVERFLOW_VAL - 1)) < fix.RADIX_RHS_VAL)
print("Good")
print("Attempting: square root of tenth of largest value")
local result_6 = fix:sqrt(fix.OVERFLOW_VAL / 10)
print("sqrt( " .. str(fix.OVERFLOW_VAL / 10) .. " ) = " .. str(result_6))
assert(math.abs(fix:mult(result_6, result_6) - (fix.OVERFLOW_VAL / 10)) < fix.RADIX_RHS_VAL)
print("Good")
print("Attempting: square root of 1024 completely random integer values")
local result_7_largest_error = 0
local result_7_errormost_val
for i = 1,1024 do
local RANDi = fix:round(fix:genRand(randfn) % fix.OVERFLOW_VAL)
local result_7i = fix:sqrt(RANDi)
local result_7i_SQERR = math.abs(fix:mult(result_7i, result_7i) - RANDi)
if (result_7i_SQERR > result_7_largest_error) then
result_7_largest_error = result_7i_SQERR
result_7_errormost_val = RANDi
end
end
print(">>> the largest SQRT error is ".. str(result_7_largest_error))
print(">>> the value producing it is sqrt( " .. str(result_7_errormost_val) .. " ) = " .. str(fix:sqrt(result_7_errormost_val)))
assert(result_7_largest_error < fix.RADIX_RHS_VAL)
print("Good")
print("Attempting: square root of 1024 completely random fractional values")
print("NOTE: error is magnified as squares become small, so that it gets proper representation!")
local result_8_largest_error = 0
local result_8_errormost_val
for i = 1,1024 do
local RANDi = fix:genRand(randfn) % fix.RADIX_RHS_VAL
local result_8i = fix:sqrt(RANDi)
local result_8i_SQERR_MAGNIFIED = fix:div(math.abs(fix:mult(result_8i, result_8i) - RANDi), RANDi)
if result_8i_SQERR_MAGNIFIED > result_8_largest_error then
result_8_largest_error = result_8i_SQERR_MAGNIFIED
result_8_errormost_val = RANDi
end
end
print(">>> the largest SQRT error, magnified, is " .. str(result_8_largest_error))
print(">>> the value producing it is sqrt(" .. str(result_8_errormost_val) .. " ) = " .. str(fix:sqrt(result_8_errormost_val)))
assert(result_8_largest_error < fix.RADIX_RHS_VAL)
print("Good")
print("Attempting: square root of 2 almost equals fix.CONSTANT.SQRT2")
local result_8B = fix:sqrt(TWO)
local result_8B_err = math.abs(SQRT2 - result_8B)
print("sqrt( " .. str(TWO) .. " ) = " .. str(result_8B))
print(">>> the error is " .. str(result_8B_err))
assert(result_8B_err < 5)
print("Good")
print("Test: binary exponential")
print("Attempting: ldexp(nil, 0) equals 2^0 (equals one)")
local result_9 = fix:ldexp(nil, 0)
print("ldexp( nil , " .. str(0) .." ) = " .. str(result_9))
assert(result_9 == fix.RADIX_RHS_VAL)
print("Good")
print("Attempting: ldexp(nil, 1) equals 2^1 (equals two)")
local result_10 = fix:ldexp(nil, fix.RADIX_RHS_VAL)
print("ldexp( nil , " .. str(fix.RADIX_RHS_VAL) .. " ) = " .. str(result_10))
assert(result_10 == 2 * fix.RADIX_RHS_VAL)
print("Good")
print("Attempting: ldexp(nil, HALF) almost equals fix.CONSTANT.SQRT2")
local result_11 = fix:ldexp(nil, HALF)
local result_11_err = math.abs(SQRT2 - result_11)
print("ldexp( nil, " .. str(HALF) .. " ) = " .. str(result_11))
print(">>> the error is " .. str(result_11_err))
assert(result_11_err < fix:asValue(0, 000010))
print("Good")
print("Attempting: ldexp(nil, -HALF) almost equals reciprocal fix.CONSTANT.SQRT2")
local result_12 = fix:ldexp(nil, -HALF)
local result_12_err = math.abs(INV_SQRT2 - result_12)
print("ldexp( nil , " .. str(-HALF) .. " ) = " .. str(result_12))
print(">>> the error is " .. str(result_12_err))
assert(result_12_err < fix:asValue(0, 000010))
print("Good")
print("Attempting: multiplying ldexp(nil, TENTH) ten times somewhat equals two")
local result_13_1 = fix:ldexp(nil, ONE / 10)
local result_13_2 = result_13_1
for i = 1,9 do
result_13_2 = fix:mult(result_13_2, result_13_1)
end
local result_13_err = math.abs(result_13_2 - TWO)
print("ldexp( nil , " .. str(ONE / 10) .. " ) = " .. str(result_13_1))
print(">>> the result of multiplying tenfold is " .. str(result_13_2))
print(">>> the error is " .. str(result_13_err))
assert(result_13_err <= fix:asValue(0, 600000))
print("Good")
print("Attempting: three square roots of ldexp(nil, x) = ldexp(nil, x / 8 ) where x is random in [8, 24]")
local r14RAND = 8 * ONE + (fix:genRand(randfn) % (16 * ONE))
local result_14_1 = fix:ldexp(nil, r14RAND)
local result_14_2 = fix:ldexp(nil, fix:div(r14RAND, 8 * ONE))
local result_14_3 = fix:sqrt(fix:sqrt(fix:sqrt(result_14_1)))
local result_14_err = math.abs(result_14_2 - result_14_3)
print("ldexp( nil , " .. str(r14RAND) .. " ) = " .. str(result_14_1))
print(">>> the result of three square roots is " .. str(result_14_3))
print("ldexp( nil , " .. str(fix:div(r14RAND, 8 * ONE)) .. " ) = " .. str(result_14_2))
print(">>> the error is " .. str(result_14_err))
assert(result_14_err < fix:asValue(0, 100000))
print("Good")
print("Test: binary logarithm lg")
print("Attempting: lg(1) == 0")
local result_15 = fix:lg(ONE)
print("lg( " .. str(ONE) .. " ) = " .. str(result_15))
assert(result_15 == 0)
print("Good")
print("Attempting: lg(0) == -inf")
local result_16 = fix:lg(0)
print("lg( " .. str(0) .. " ) = " .. result_16)
assert(result_16 == -math.huge)
print("Good")
print("Attempting: lg(x) is nan if x is negative")
local r17RAND = -(1 + fix:genRand(randfn) % (fix.OVERFLOW_VAL - 1))
local result_17 = fix:lg(r17RAND)
print("lg( " .. str(r17RAND) .. " ) = " .. result_17)
assert(result_17 ~= result_17)
print("Good")
print("Attempting: lg(HALF) == -1")
local result_18 = fix:lg(HALF)
print("lg( " .. str(HALF) .. " ) = " .. str(result_18))
assert(result_18 == -ONE)
print("Good")
print("Attempting: lg(TENTH) is about -3.321928")
local result_19 = fix:lg(ONE / 10)
local result_19_err = math.abs(result_19 + fix:asValue(3, 321928))
print("lg( " .. str(ONE / 10) .. " ) = " .. str(result_19))
print(">>> the error is " .. str(result_19_err))
assert(result_19_err < 500)
print("Good")
print("Attempting: lg(TENTH) + lg(THREE) == lg(THREE_TENTHS)")
local result_20_1 = fix:lg(TENTH)
local result_20_2 = fix:lg(THREE)
local result_20_3 = fix:lg(THREE_TENTHS)
local result_20_4 = result_20_1 + result_20_2
local result_20_err = math.abs(result_20_4 - result_20_3)
print("lg( " .. str(TENTH) .. " ) + lg( " .. str(THREE) .. " ) = " .. str(result_20_4))
print("lg( " .. str(THREE_TENTHS) .. " ) = " .. str(result_20_3))
print(">>> the error is " .. str(result_20_err))
assert(result_20_err < fix:asValue(0, 000050))
print("Good")
print("Attempting: compare lg( x ) of 1024 random whole number values with Lua math.log( x , 2 )")
local result_21_largest_error = 0
local result_21_errormost_val
for i = 1,1024 do
local RANDi = fix:round((fix:genRand(randfn) + 1) % (fix.OVERFLOW_VAL - 1), "NEXT_INT")
local result_21i = fix:lg(RANDi)
local result_21i_2 = math.log(RANDi / fix.RADIX_RHS_VAL, 2)
local result_21i_err = math.abs(result_21i / fix.RADIX_RHS_VAL - result_21i_2)
if result_21i_err > result_21_largest_error then
result_21_largest_error = result_21i_err
result_21_errormost_val = RANDi
end
end
print(">>> the largest error is " .. result_21_largest_error)
print(">>> the value that produced it is lg( " .. str(result_21_errormost_val) .. " ) = " .. str(fix:lg(result_21_errormost_val)))
assert(result_21_largest_error < 1e-3)
print("Good")
print("Attempting: compare lg( x ) of 1024 random values where HALF < x < TWO with Lua math.log( x , 2 )")
local result_22_largest_error = 0
local result_22_errormost_val
for i = 1,1024 do
local RANDi = HALF + (1 + fix:genRand(randfn)) % (TWO - HALF - 1)
local result_22i = fix:lg(RANDi)
local result_22i_2 = math.log(RANDi / ONE, 2)
local result_22i_err = math.abs(result_22i / ONE - result_22i_2)
if result_22i_err > result_22_largest_error then
result_22_largest_error = result_22i_err
result_22_errormost_val = RANDi
end
end
print(">>> the largest error is " .. result_22_largest_error)
print(">>> the value that produced it is lg( " .. str(result_22_errormost_val) .. " ) = " .. str(fix:lg(result_22_errormost_val)))
assert(result_22_largest_error < 1e-3)
print("Good")
print("Test: arbitrary exponentiation")
print("Attempting: pow(1, x) where x is random")
local r23RAND = math.fmod(fix:genRand(randfn), fix.OVERFLOW_VAL)
local result_23 = fix:pow(ONE, r23RAND)
print("pow( " .. str(ONE) .. " , " .. str(r23RAND) .. " ) = " .. str(result_23))
assert(result_23 == ONE)
print("Good")
print("Attempting: pow(x, y) == x^y (where x^y is native Lua exponentiation), for random integers 2 <= abs(x) <= 8, 2 < abs(y) <= 8. (4 checks)")
for i = 1, 4 do
local r23RANDi_X = fix:round(TWO + (fix:genRand(randfn) % (THREE * 2))) * (randfn2(0, 1) == 1 and -1 or 1)
local r23RANDi_Y = fix:round(TWO + (fix:genRand(randfn) % (THREE * 2))) * (randfn2(0, 1) == 1 and -1 or 1)
local result_23i_1 = fix:pow(r23RANDi_X, r23RANDi_Y)
local r23RiX_int = r23RANDi_X / ONE
local r23RiY_int = r23RANDi_Y / ONE
FixedPoint.assert_int("X_"..i, r23RiX_int)
FixedPoint.assert_int("Y_"..i, r23RiY_int)
local result_23i_2 = r23RiX_int ^ r23RiY_int
print("i) pow( " .. str(r23RANDi_X) .. " , " .. str(r23RANDi_Y) .. " ) = " .. str(result_23i_1))
print("ii) " .. r23RiX_int .. " ^ " .. r23RiY_int .. " = " .. result_23i_2)
assert(math.abs(result_23i_1 / ONE - result_23i_2) < 2e-6)
end
print("Good")
print("Attempting: pow(x, y) is about x^y (native Lua exponentiation), for random fractions 1 <= x < 8, 1 <= y < 8. (1024 checks)")
local result_24_largest_error = 0
local result_24_errormost_val_X, result_24_errormost_val_Y
for i = 1, 1024 do
local r24RANDi_X = (ONE + (fix:genRand(randfn) % (THREE * 2 + ONE)))
local r24RANDi_Y = (ONE + (fix:genRand(randfn) % (THREE * 2 + ONE)))
local result_24i_1 = fix:pow(r24RANDi_X, r24RANDi_Y)
local r24RiX_double = r24RANDi_X / ONE
local r24RiY_double = r24RANDi_Y / ONE
local result_24i_2 = r24RiX_double ^ r24RiY_double
local result_24i_err = math.abs(result_24i_1 / ONE - result_24i_2)
if result_24i_err > result_24_largest_error then
result_24_largest_error = result_24i_err
result_24_errormost_val_X = r24RANDi_X
result_24_errormost_val_Y = r24RANDi_Y
end
end
print(">>> the largest error is " .. result_24_largest_error)
print(">>> the pair that produced it is pow( " .. str(result_24_errormost_val_X) .. " , " .. str(result_24_errormost_val_Y) .. " ) = "
.. str(fix:pow(result_24_errormost_val_X, result_24_errormost_val_Y)))
assert(math.abs(result_24_largest_error / ((result_24_errormost_val_X / ONE)^(result_24_errormost_val_Y / ONE))) < 1e-4)
print("Good")
print("Attempting: last test but with -1 >= y > -8")
local result_25_largest_error = 0
local result_25_errormost_val_X, result_25_errormost_val_Y
for i = 1, 1024 do
local r25RANDi_X = (ONE + (fix:genRand(randfn) % (THREE * 2 + ONE)))
local r25RANDi_Y = -(ONE + (fix:genRand(randfn) % (THREE * 2 + ONE)))
local result_25i_1 = fix:pow(r25RANDi_X, r25RANDi_Y)
local r25RiX_double = r25RANDi_X / ONE
local r25RiY_double = r25RANDi_Y / ONE
local result_25i_2 = r25RiX_double ^ r25RiY_double
local result_25i_err = math.abs(result_25i_1 / ONE - result_25i_2)
if result_25i_err > result_25_largest_error then
result_25_largest_error = result_25i_err
result_25_errormost_val_X = r25RANDi_X
result_25_errormost_val_Y = r25RANDi_Y
end
end
print(">>> the largest error is " .. result_25_largest_error)
print(">>> the pair that produced it is pow( " .. str(result_25_errormost_val_X) .. " , " .. str(result_25_errormost_val_Y) .. " ) = "
.. str(fix:pow(result_25_errormost_val_X, result_25_errormost_val_Y)))
assert(math.abs(result_25_largest_error / ((result_25_errormost_val_X / ONE)^(result_25_errormost_val_Y / ONE))) < 1e-4)
print("Good")
print("Test: hypotenuse")
print("Attempting: hypotenuse of 1024 random fixed point value pairs, -" .. str(INV_SQRT2_SCALED) .. " < x, y < " .. str(INV_SQRT2_SCALED))
local result_26_largest_error = 0
local result_26_errscale = 0
local result_26_errormost_val_X, result_26_errormost_val_Y
for i = 1,1024 do
local r26RANDi_X = math.fmod(fix:genRand(randfn), INV_SQRT2_SCALED)
local r26RANDi_Y = math.fmod(fix:genRand(randfn), INV_SQRT2_SCALED)
local result_26i_1 = fix:hypot(r26RANDi_X, r26RANDi_Y)
local result26i_2 = 0
local x = math.abs(r26RANDi_X / ONE)
local y = math.abs(r26RANDi_Y / ONE)
local yx = 0
if x < y then
local temp = y
y = x
x = temp
end
if x ~= 0 then
yx = y / x
result26i_2 = x * math.sqrt(1 + yx * yx)
end
local result26i_err = math.abs(result26i_2 - result_26i_1 / ONE)
if result26i_err > result_26_largest_error then
result_26_largest_error = result26i_err
result_26_errormost_val_X = r26RANDi_X
result_26_errormost_val_Y = r26RANDi_Y
result_26_errscale = math.sqrt(result26i_2)
end
end
print(">>> the largest error is " .. result_26_largest_error)
print(">>> the expression producing the error is hypot( " .. str(result_26_errormost_val_X) .. " , " .. str(result_26_errormost_val_Y) .. " ) = "
.. str(fix:hypot(result_26_errormost_val_X, result_26_errormost_val_Y))
)
assert(result_26_largest_error / result_26_errscale < 1)
print("Good")
end,
Trigonometry = function()
local fix2 = FixedPoint:new(test_radius, 2, drp)
local fix6 = FixedPoint:new(test_radius, 6, drp)
print("TWO Fixed point contexts, default radius but one has radix point 2 and the other 6. Default rounding policy.")
local ZERO = 0
local PI2 = fix2.CONSTANT.PI
local HALFPI2 = fix2:div(PI2, fix2:convertInt(2))
local HALF3PI2 = HALFPI2 * 3
local TAU2 = fix2.CONSTANT.TAU
local DEGR2 = fix2.CONSTANT.DEGR
local DEGH2 = fix2.CONSTANT.DEGR / 2
local RAND1_2 = fix2:genRand(randfn) % fix2.CONSTANT.TAU
local PI6 = fix6.CONSTANT.PI
local TAU6 = fix6.CONSTANT.TAU
local HALFPI6 = fix6:div(TAU6, fix6:convertInt(4))
local HALF3PI6 = fix6:mult(TAU6, 750000)
local DEGR6 = fix6.CONSTANT.DEGR
local DEGH6 = fix6.CONSTANT.DEGR / 2
local RAND1_6 = fix6:genRand(randfn) % fix6.CONSTANT.TAU
local function str2(v)
return fix2:toString(v)
end
local function str6(v)
return fix6:toString(v)
end
print("Test: convert TAU (2PI) rad to deg")
print("Attempting: RADIX POINT 2")
local result1 = fix2:deg(TAU2)
print("deg( " .. str2(TAU2) .. " ) = " .. str2(result1))
assert(result1 == 0)
print("Good")
print("Attempting: RADIX POINT 6")
local result2 = fix6:deg(TAU6)
print("deg( " .. str6(TAU6) .. " ) = " .. str6(result2))
assert(result2 == 0)
print("Good")
print("Test: convert PI rad to deg")
print("Attempting: RADIX POINT 2")
local result3 = fix2:deg(PI2)
print("deg( " .. str2(PI2) .. " ) = " .. str2(result3))
assert(result3 == DEGH2)
print("Good")
print("Attempting: RADIX POINT 6")
local result4 = fix6:deg(PI6)
print("deg( " .. str6(PI6) .. " ) = " .. str6(result4))
assert(result4 == DEGH6)
print("Good")