[270] | 1 | ##
|
---|
| 2 | # Math Test
|
---|
| 3 |
|
---|
| 4 | ##
|
---|
| 5 | # Performs fuzzy check for equality on methods returning floats
|
---|
| 6 | # on the basis of the Math::TOLERANCE constant.
|
---|
| 7 | def check_float(a, b)
|
---|
| 8 | tolerance = Math::TOLERANCE
|
---|
| 9 | a = a.to_f
|
---|
| 10 | b = b.to_f
|
---|
| 11 | if a.finite? and b.finite?
|
---|
| 12 | (a-b).abs < tolerance
|
---|
| 13 | else
|
---|
| 14 | true
|
---|
| 15 | end
|
---|
| 16 | end
|
---|
| 17 |
|
---|
| 18 | assert('Math.sin 0') do
|
---|
| 19 | check_float(Math.sin(0), 0)
|
---|
| 20 | end
|
---|
| 21 |
|
---|
| 22 | assert('Math.sin PI/2') do
|
---|
| 23 | check_float(Math.sin(Math::PI / 2), 1)
|
---|
| 24 | end
|
---|
| 25 |
|
---|
| 26 | assert('Math.cos 0') do
|
---|
| 27 | check_float(Math.cos(0), 1)
|
---|
| 28 | end
|
---|
| 29 |
|
---|
| 30 | assert('Math.cos PI/2') do
|
---|
| 31 | check_float(Math.cos(Math::PI / 2), 0)
|
---|
| 32 | end
|
---|
| 33 |
|
---|
| 34 | assert('Math.tan 0') do
|
---|
| 35 | check_float(Math.tan(0), 0)
|
---|
| 36 | end
|
---|
| 37 |
|
---|
| 38 | assert('Math.tan PI/4') do
|
---|
| 39 | check_float(Math.tan(Math::PI / 4), 1)
|
---|
| 40 | end
|
---|
| 41 |
|
---|
| 42 | assert('Fundamental trig identities') do
|
---|
| 43 | result = true
|
---|
| 44 | N = 13
|
---|
| 45 | N.times do |i|
|
---|
| 46 | a = Math::PI / N * i
|
---|
| 47 | ca = Math::PI / 2 - a
|
---|
| 48 | s = Math.sin(a)
|
---|
| 49 | c = Math.cos(a)
|
---|
| 50 | t = Math.tan(a)
|
---|
| 51 | result &= check_float(s, Math.cos(ca))
|
---|
| 52 | result &= check_float(t, 1 / Math.tan(ca))
|
---|
| 53 | result &= check_float(s ** 2 + c ** 2, 1)
|
---|
| 54 | result &= check_float(t ** 2 + 1, (1/c) ** 2)
|
---|
| 55 | result &= check_float((1/t) ** 2 + 1, (1/s) ** 2)
|
---|
| 56 | end
|
---|
| 57 | result
|
---|
| 58 | end
|
---|
| 59 |
|
---|
| 60 | assert('Math.erf 0') do
|
---|
| 61 | check_float(Math.erf(0), 0)
|
---|
| 62 | end
|
---|
| 63 |
|
---|
| 64 | assert('Math.exp 0') do
|
---|
| 65 | check_float(Math.exp(0), 1.0)
|
---|
| 66 | end
|
---|
| 67 |
|
---|
| 68 | assert('Math.exp 1') do
|
---|
| 69 | check_float(Math.exp(1), 2.718281828459045)
|
---|
| 70 | end
|
---|
| 71 |
|
---|
| 72 | assert('Math.exp 1.5') do
|
---|
| 73 | check_float(Math.exp(1.5), 4.4816890703380645)
|
---|
| 74 | end
|
---|
| 75 |
|
---|
| 76 | assert('Math.log 1') do
|
---|
| 77 | check_float(Math.log(1), 0)
|
---|
| 78 | end
|
---|
| 79 |
|
---|
| 80 | assert('Math.log E') do
|
---|
| 81 | check_float(Math.log(Math::E), 1.0)
|
---|
| 82 | end
|
---|
| 83 |
|
---|
| 84 | assert('Math.log E**3') do
|
---|
| 85 | check_float(Math.log(Math::E**3), 3.0)
|
---|
| 86 | end
|
---|
| 87 |
|
---|
| 88 | assert('Math.log2 1') do
|
---|
| 89 | check_float(Math.log2(1), 0.0)
|
---|
| 90 | end
|
---|
| 91 |
|
---|
| 92 | assert('Math.log2 2') do
|
---|
| 93 | check_float(Math.log2(2), 1.0)
|
---|
| 94 | end
|
---|
| 95 |
|
---|
| 96 | assert('Math.log10 1') do
|
---|
| 97 | check_float(Math.log10(1), 0.0)
|
---|
| 98 | end
|
---|
| 99 |
|
---|
| 100 | assert('Math.log10 10') do
|
---|
| 101 | check_float(Math.log10(10), 1.0)
|
---|
| 102 | end
|
---|
| 103 |
|
---|
| 104 | assert('Math.log10 10**100') do
|
---|
| 105 | check_float(Math.log10(10**100), 100.0)
|
---|
| 106 | end
|
---|
| 107 |
|
---|
| 108 | assert('Math.sqrt') do
|
---|
| 109 | num = [0.0, 1.0, 2.0, 3.0, 4.0]
|
---|
| 110 | sqr = [0, 1, 4, 9, 16]
|
---|
| 111 | result = true
|
---|
| 112 | sqr.each_with_index do |v,i|
|
---|
| 113 | result &= check_float(Math.sqrt(v), num[i])
|
---|
| 114 | end
|
---|
| 115 | result
|
---|
| 116 | end
|
---|
| 117 |
|
---|
| 118 | assert('Math.cbrt') do
|
---|
| 119 | num = [-2.0, -1.0, 0.0, 1.0, 2.0]
|
---|
| 120 | cub = [-8, -1, 0, 1, 8]
|
---|
| 121 | result = true
|
---|
| 122 | cub.each_with_index do |v,i|
|
---|
| 123 | result &= check_float(Math.cbrt(v), num[i])
|
---|
| 124 | end
|
---|
| 125 | result
|
---|
| 126 | end
|
---|
| 127 |
|
---|
| 128 | assert('Math.hypot') do
|
---|
| 129 | check_float(Math.hypot(3, 4), 5.0)
|
---|
| 130 | end
|
---|
| 131 |
|
---|
| 132 | assert('Math.frexp 1234') do
|
---|
| 133 | n = 1234
|
---|
| 134 | fraction, exponent = Math.frexp(n)
|
---|
| 135 | check_float(Math.ldexp(fraction, exponent), n)
|
---|
| 136 | end
|
---|
| 137 |
|
---|
| 138 | assert('Math.erf 1') do
|
---|
| 139 | check_float(Math.erf(1), 0.842700792949715)
|
---|
| 140 | end
|
---|
| 141 |
|
---|
| 142 | assert('Math.erfc 1') do
|
---|
| 143 | check_float(Math.erfc(1), 0.157299207050285)
|
---|
| 144 | end
|
---|
| 145 |
|
---|
| 146 | assert('Math.erf -1') do
|
---|
| 147 | check_float(Math.erf(-1), -0.8427007929497148)
|
---|
| 148 | end
|
---|
| 149 |
|
---|
| 150 | assert('Math.erfc -1') do
|
---|
| 151 | check_float(Math.erfc(-1), 1.8427007929497148)
|
---|
| 152 | end
|
---|