v2 / vlib / math / fractions / approximations_test.v
165 lines · 131 sloc · 4.53 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4import math.fractions
5import math
6
7fn test_half() {
8 float_val := 0.5
9 fract_val := fractions.approximate(float_val)
10 assert fract_val == fractions.fraction(1, 2)
11}
12
13fn test_third() {
14 float_val := 1.0 / 3.0
15 fract_val := fractions.approximate(float_val)
16 assert fract_val == fractions.fraction(1, 3)
17}
18
19fn test_minus_one_twelfth() {
20 float_val := -1.0 / 12.0
21 fract_val := fractions.approximate(float_val)
22 assert fract_val == fractions.fraction(-1, 12)
23}
24
25fn test_zero() {
26 float_val := 0.0
27 println('Pre')
28 fract_val := fractions.approximate(float_val)
29 println('Post')
30 assert fract_val == fractions.fraction(0, 1)
31}
32
33fn test_minus_one() {
34 float_val := -1.0
35 fract_val := fractions.approximate(float_val)
36 assert fract_val == fractions.fraction(-1, 1)
37}
38
39fn test_thirty_three() {
40 float_val := 33.0
41 fract_val := fractions.approximate(float_val)
42 assert fract_val == fractions.fraction(33, 1)
43}
44
45fn test_millionth() {
46 float_val := 1.0 / 1000000.0
47 fract_val := fractions.approximate(float_val)
48 assert fract_val == fractions.fraction(1, 1000000)
49}
50
51fn test_minus_27_by_57() {
52 float_val := -27.0 / 57.0
53 fract_val := fractions.approximate(float_val)
54 assert fract_val == fractions.fraction(-27, 57)
55}
56
57fn test_29_by_104() {
58 float_val := 29.0 / 104.0
59 fract_val := fractions.approximate(float_val)
60 assert fract_val == fractions.fraction(29, 104)
61}
62
63fn test_140710_232() {
64 float_val := 140710.232
65 fract_val := fractions.approximate(float_val)
66 // Approximation will match perfectly for upto 3 places after the decimal
67 // The result will be within default_eps of original value
68 assert fract_val.f64() == float_val
69}
70
71fn test_pi_1_digit() {
72 assert fractions.approximate_with_eps(math.pi, 5.0e-2) == fractions.fraction(22, 7)
73}
74
75fn test_pi_2_digits() {
76 assert fractions.approximate_with_eps(math.pi, 5.0e-3) == fractions.fraction(22, 7)
77}
78
79fn test_pi_3_digits() {
80 assert fractions.approximate_with_eps(math.pi, 5.0e-4) == fractions.fraction(333, 106)
81}
82
83fn test_pi_4_digits() {
84 assert fractions.approximate_with_eps(math.pi, 5.0e-5) == fractions.fraction(355, 113)
85}
86
87fn test_pi_5_digits() {
88 assert fractions.approximate_with_eps(math.pi, 5.0e-6) == fractions.fraction(355, 113)
89}
90
91fn test_pi_6_digits() {
92 assert fractions.approximate_with_eps(math.pi, 5.0e-7) == fractions.fraction(355, 113)
93}
94
95fn test_pi_7_digits() {
96 assert fractions.approximate_with_eps(math.pi, 5.0e-8) == fractions.fraction(103993, 33102)
97}
98
99fn test_pi_8_digits() {
100 assert fractions.approximate_with_eps(math.pi, 5.0e-9) == fractions.fraction(103993, 33102)
101}
102
103fn test_pi_9_digits() {
104 assert fractions.approximate_with_eps(math.pi, 5.0e-10) == fractions.fraction(104348, 33215)
105}
106
107fn test_pi_10_digits() {
108 assert fractions.approximate_with_eps(math.pi, 5.0e-11) == fractions.fraction(312689, 99532)
109}
110
111fn test_pi_11_digits() {
112 assert fractions.approximate_with_eps(math.pi, 5.0e-12) == fractions.fraction(1146408, 364913)
113}
114
115fn test_pi_12_digits() {
116 assert fractions.approximate_with_eps(math.pi, 5.0e-13) == fractions.fraction(4272943, 1360120)
117}
118
119fn test_phi_1_digit() {
120 assert fractions.approximate_with_eps(math.phi, 5.0e-2) == fractions.fraction(5, 3)
121}
122
123fn test_phi_2_digits() {
124 assert fractions.approximate_with_eps(math.phi, 5.0e-3) == fractions.fraction(21, 13)
125}
126
127fn test_phi_3_digits() {
128 assert fractions.approximate_with_eps(math.phi, 5.0e-4) == fractions.fraction(55, 34)
129}
130
131fn test_phi_4_digits() {
132 assert fractions.approximate_with_eps(math.phi, 5.0e-5) == fractions.fraction(233, 144)
133}
134
135fn test_phi_5_digits() {
136 assert fractions.approximate_with_eps(math.phi, 5.0e-6) == fractions.fraction(610, 377)
137}
138
139fn test_phi_6_digits() {
140 assert fractions.approximate_with_eps(math.phi, 5.0e-7) == fractions.fraction(1597, 987)
141}
142
143fn test_phi_7_digits() {
144 assert fractions.approximate_with_eps(math.phi, 5.0e-8) == fractions.fraction(6765, 4181)
145}
146
147fn test_phi_8_digits() {
148 assert fractions.approximate_with_eps(math.phi, 5.0e-9) == fractions.fraction(17711, 10946)
149}
150
151fn test_phi_9_digits() {
152 assert fractions.approximate_with_eps(math.phi, 5.0e-10) == fractions.fraction(75025, 46368)
153}
154
155fn test_phi_10_digits() {
156 assert fractions.approximate_with_eps(math.phi, 5.0e-11) == fractions.fraction(196418, 121393)
157}
158
159fn test_phi_11_digits() {
160 assert fractions.approximate_with_eps(math.phi, 5.0e-12) == fractions.fraction(514229, 317811)
161}
162
163fn test_phi_12_digits() {
164 assert fractions.approximate_with_eps(math.phi, 5.0e-13) == fractions.fraction(2178309, 1346269)
165}
166