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
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import math.fractions
import math
fn test_half() {
float_val := 0.5
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(1, 2))
}
fn test_third() {
float_val := 1.0 / 3.0
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(1, 3))
}
fn test_minus_one_twelfth() {
float_val := -1.0 / 12.0
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(-1, 12))
}
fn test_zero() {
float_val := 0.0
println('Pre')
fract_val := fractions.approximate(float_val)
println('Post')
assert fract_val.equals(fractions.fraction(0, 1))
}
fn test_minus_one() {
float_val := -1.0
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(-1, 1))
}
fn test_thirty_three() {
float_val := 33.0
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(33, 1))
}
fn test_millionth() {
float_val := 1.0 / 1000000.0
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(1, 1000000))
}
fn test_minus_27_by_57() {
float_val := -27.0 / 57.0
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(-27, 57))
}
fn test_29_by_104() {
float_val := 29.0 / 104.0
fract_val := fractions.approximate(float_val)
assert fract_val.equals(fractions.fraction(29, 104))
}
fn test_140710_232() {
float_val := 140710.232
fract_val := fractions.approximate(float_val)
// Approximation will match perfectly for upto 3 places after the decimal
// The result will be within default_eps of original value
assert fract_val.f64() == float_val
}
fn test_pi_1_digit() {
assert fractions.approximate_with_eps(math.pi, 5.0e-2).equals(fractions.fraction(22,
7))
}
fn test_pi_2_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-3).equals(fractions.fraction(22,
7))
}
fn test_pi_3_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-4).equals(fractions.fraction(333,
106))
}
fn test_pi_4_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-5).equals(fractions.fraction(355,
113))
}
fn test_pi_5_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-6).equals(fractions.fraction(355,
113))
}
fn test_pi_6_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-7).equals(fractions.fraction(355,
113))
}
fn test_pi_7_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-8).equals(fractions.fraction(103993,
33102))
}
fn test_pi_8_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-9).equals(fractions.fraction(103993,
33102))
}
fn test_pi_9_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-10).equals(fractions.fraction(104348,
33215))
}
fn test_pi_10_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-11).equals(fractions.fraction(312689,
99532))
}
fn test_pi_11_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-12).equals(fractions.fraction(1146408,
364913))
}
fn test_pi_12_digits() {
assert fractions.approximate_with_eps(math.pi, 5.0e-13).equals(fractions.fraction(4272943,
1360120))
}
fn test_phi_1_digit() {
assert fractions.approximate_with_eps(math.phi, 5.0e-2).equals(fractions.fraction(5,
3))
}
fn test_phi_2_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-3).equals(fractions.fraction(21,
13))
}
fn test_phi_3_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-4).equals(fractions.fraction(55,
34))
}
fn test_phi_4_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-5).equals(fractions.fraction(233,
144))
}
fn test_phi_5_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-6).equals(fractions.fraction(610,
377))
}
fn test_phi_6_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-7).equals(fractions.fraction(1597,
987))
}
fn test_phi_7_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-8).equals(fractions.fraction(6765,
4181))
}
fn test_phi_8_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-9).equals(fractions.fraction(17711,
10946))
}
fn test_phi_9_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-10).equals(fractions.fraction(75025,
46368))
}
fn test_phi_10_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-11).equals(fractions.fraction(196418,
121393))
}
fn test_phi_11_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-12).equals(fractions.fraction(514229,
317811))
}
fn test_phi_12_digits() {
assert fractions.approximate_with_eps(math.phi, 5.0e-13).equals(fractions.fraction(2178309,
1346269))
}
|