| 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. |
| 4 | import math.fractions |
| 5 | import math |
| 6 | |
| 7 | fn test_half() { |
| 8 | float_val := 0.5 |
| 9 | fract_val := fractions.approximate(float_val) |
| 10 | assert fract_val == fractions.fraction(1, 2) |
| 11 | } |
| 12 | |
| 13 | fn 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 | |
| 19 | fn 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 | |
| 25 | fn 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 | |
| 33 | fn 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 | |
| 39 | fn 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 | |
| 45 | fn 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 | |
| 51 | fn 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 | |
| 57 | fn 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 | |
| 63 | fn 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 | |
| 71 | fn test_pi_1_digit() { |
| 72 | assert fractions.approximate_with_eps(math.pi, 5.0e-2) == fractions.fraction(22, 7) |
| 73 | } |
| 74 | |
| 75 | fn test_pi_2_digits() { |
| 76 | assert fractions.approximate_with_eps(math.pi, 5.0e-3) == fractions.fraction(22, 7) |
| 77 | } |
| 78 | |
| 79 | fn test_pi_3_digits() { |
| 80 | assert fractions.approximate_with_eps(math.pi, 5.0e-4) == fractions.fraction(333, 106) |
| 81 | } |
| 82 | |
| 83 | fn test_pi_4_digits() { |
| 84 | assert fractions.approximate_with_eps(math.pi, 5.0e-5) == fractions.fraction(355, 113) |
| 85 | } |
| 86 | |
| 87 | fn test_pi_5_digits() { |
| 88 | assert fractions.approximate_with_eps(math.pi, 5.0e-6) == fractions.fraction(355, 113) |
| 89 | } |
| 90 | |
| 91 | fn test_pi_6_digits() { |
| 92 | assert fractions.approximate_with_eps(math.pi, 5.0e-7) == fractions.fraction(355, 113) |
| 93 | } |
| 94 | |
| 95 | fn test_pi_7_digits() { |
| 96 | assert fractions.approximate_with_eps(math.pi, 5.0e-8) == fractions.fraction(103993, 33102) |
| 97 | } |
| 98 | |
| 99 | fn test_pi_8_digits() { |
| 100 | assert fractions.approximate_with_eps(math.pi, 5.0e-9) == fractions.fraction(103993, 33102) |
| 101 | } |
| 102 | |
| 103 | fn test_pi_9_digits() { |
| 104 | assert fractions.approximate_with_eps(math.pi, 5.0e-10) == fractions.fraction(104348, 33215) |
| 105 | } |
| 106 | |
| 107 | fn test_pi_10_digits() { |
| 108 | assert fractions.approximate_with_eps(math.pi, 5.0e-11) == fractions.fraction(312689, 99532) |
| 109 | } |
| 110 | |
| 111 | fn test_pi_11_digits() { |
| 112 | assert fractions.approximate_with_eps(math.pi, 5.0e-12) == fractions.fraction(1146408, 364913) |
| 113 | } |
| 114 | |
| 115 | fn test_pi_12_digits() { |
| 116 | assert fractions.approximate_with_eps(math.pi, 5.0e-13) == fractions.fraction(4272943, 1360120) |
| 117 | } |
| 118 | |
| 119 | fn test_phi_1_digit() { |
| 120 | assert fractions.approximate_with_eps(math.phi, 5.0e-2) == fractions.fraction(5, 3) |
| 121 | } |
| 122 | |
| 123 | fn test_phi_2_digits() { |
| 124 | assert fractions.approximate_with_eps(math.phi, 5.0e-3) == fractions.fraction(21, 13) |
| 125 | } |
| 126 | |
| 127 | fn test_phi_3_digits() { |
| 128 | assert fractions.approximate_with_eps(math.phi, 5.0e-4) == fractions.fraction(55, 34) |
| 129 | } |
| 130 | |
| 131 | fn test_phi_4_digits() { |
| 132 | assert fractions.approximate_with_eps(math.phi, 5.0e-5) == fractions.fraction(233, 144) |
| 133 | } |
| 134 | |
| 135 | fn test_phi_5_digits() { |
| 136 | assert fractions.approximate_with_eps(math.phi, 5.0e-6) == fractions.fraction(610, 377) |
| 137 | } |
| 138 | |
| 139 | fn test_phi_6_digits() { |
| 140 | assert fractions.approximate_with_eps(math.phi, 5.0e-7) == fractions.fraction(1597, 987) |
| 141 | } |
| 142 | |
| 143 | fn test_phi_7_digits() { |
| 144 | assert fractions.approximate_with_eps(math.phi, 5.0e-8) == fractions.fraction(6765, 4181) |
| 145 | } |
| 146 | |
| 147 | fn test_phi_8_digits() { |
| 148 | assert fractions.approximate_with_eps(math.phi, 5.0e-9) == fractions.fraction(17711, 10946) |
| 149 | } |
| 150 | |
| 151 | fn test_phi_9_digits() { |
| 152 | assert fractions.approximate_with_eps(math.phi, 5.0e-10) == fractions.fraction(75025, 46368) |
| 153 | } |
| 154 | |
| 155 | fn test_phi_10_digits() { |
| 156 | assert fractions.approximate_with_eps(math.phi, 5.0e-11) == fractions.fraction(196418, 121393) |
| 157 | } |
| 158 | |
| 159 | fn test_phi_11_digits() { |
| 160 | assert fractions.approximate_with_eps(math.phi, 5.0e-12) == fractions.fraction(514229, 317811) |
| 161 | } |
| 162 | |
| 163 | fn test_phi_12_digits() { |
| 164 | assert fractions.approximate_with_eps(math.phi, 5.0e-13) == fractions.fraction(2178309, 1346269) |
| 165 | } |
| 166 | |