numerals 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,366 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
2
+ include Numerals
3
+ require 'yaml'
4
+
5
+ class TestNumeral < Test::Unit::TestCase # < Minitest::Test
6
+
7
+ def test_numeral_reference_constructors
8
+ # We'll use this forms as reference for comparisons:
9
+ # Numeral[digits, base: ..., point: ... , repeat: ...]
10
+ # Numeral[symbol, sign: ...]
11
+
12
+ n = Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4]
13
+ refute n.special?
14
+ assert_equal Digits[1,2,3,4,5,6, base: 8], n.digits
15
+ assert_equal 0123456, n.digits.value
16
+ assert_equal 8, n.digits.radix
17
+ assert_equal 8, n.radix
18
+ assert_equal 2, n.point
19
+ assert_equal 4, n.repeat
20
+ assert_equal 4, n.repeating_position
21
+ assert_equal +1, n.sign
22
+ assert n.repeating?
23
+
24
+ n = Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: +1]
25
+ refute n.special?
26
+ assert_equal Digits[1,2,3,4,5,6, base: 8], n.digits
27
+ assert_equal 0123456, n.digits.value
28
+ assert_equal 8, n.digits.radix
29
+ assert_equal 8, n.radix
30
+ assert_equal 2, n.point
31
+ assert_equal 4, n.repeat
32
+ assert_equal 4, n.repeating_position
33
+ assert_equal +1, n.sign
34
+ assert n.repeating?
35
+
36
+ n = Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: -1]
37
+ refute n.special?
38
+ assert_equal Digits[1,2,3,4,5,6, base: 8], n.digits
39
+ assert_equal 0123456, n.digits.value
40
+ assert_equal 8, n.digits.radix
41
+ assert_equal 8, n.radix
42
+ assert_equal 2, n.point
43
+ assert_equal 4, n.repeat
44
+ assert_equal 4, n.repeating_position
45
+ assert_equal -1, n.sign
46
+ assert n.repeating?
47
+
48
+ n = Numeral[[1,2,3,4,5,6], point: 2, repeat: 4]
49
+ refute n.special?
50
+ assert_equal Digits[1,2,3,4,5,6, base: 10], n.digits
51
+ assert_equal 123456, n.digits.value
52
+ assert_equal 10, n.digits.radix
53
+ assert_equal 10, n.radix
54
+ assert_equal 2, n.point
55
+ assert_equal 4, n.repeat
56
+ assert_equal 4, n.repeating_position
57
+ assert n.repeating?
58
+
59
+ n = Numeral[[1,2,3,4,5,6], repeat: 4]
60
+ refute n.special?
61
+ assert_equal Digits[1,2,3,4,5,6, base: 10], n.digits
62
+ assert_equal 123456, n.digits.value
63
+ assert_equal 10, n.digits.radix
64
+ assert_equal 10, n.radix
65
+ assert_equal 6, n.point
66
+ assert_equal 4, n.repeat
67
+ assert_equal 4, n.repeating_position
68
+ assert n.repeating?
69
+
70
+ n = Numeral[[1,2,3,4,5,6], point: 2]
71
+ refute n.special?
72
+ assert_equal Digits[1,2,3,4,5,6, base: 10], n.digits
73
+ assert_equal 123456, n.digits.value
74
+ assert_equal 10, n.digits.radix
75
+ assert_equal 10, n.radix
76
+ assert_equal 2, n.point
77
+ assert_equal 6, n.repeat
78
+ assert_equal 6, n.repeating_position
79
+ refute n.repeating?
80
+
81
+ n = Numeral[[1,2,3,4,5,6], point: 2, normalize: :approximate]
82
+ refute n.special?
83
+ assert_equal Digits[1,2,3,4,5,6, base: 10], n.digits
84
+ assert_equal 123456, n.digits.value
85
+ assert_equal 10, n.digits.radix
86
+ assert_equal 10, n.radix
87
+ assert_equal 2, n.point
88
+ assert_nil n.repeat
89
+ assert_equal 6, n.repeating_position
90
+ refute n.repeating?
91
+
92
+ n = Numeral[[1,2,3,4,5,6], point: 2, repeat: 6]
93
+ refute n.special?
94
+ assert_equal Digits[1,2,3,4,5,6, base: 10], n.digits
95
+ assert_equal 123456, n.digits.value
96
+ assert_equal 10, n.digits.radix
97
+ assert_equal 10, n.radix
98
+ assert_equal 2, n.point
99
+ assert_equal 6, n.repeat
100
+ assert_equal 6, n.repeating_position
101
+ refute n.repeating?
102
+
103
+ n = Numeral[:inf, sign: +1]
104
+ assert n.special?
105
+ refute n.nan?
106
+ assert n.infinite?
107
+ assert n.positive_infinite?
108
+ assert_equal :inf, n.special
109
+ assert_equal +1, n.sign
110
+
111
+ n = Numeral[:inf, sign: -1]
112
+ assert n.special?
113
+ assert !n.nan?
114
+ assert n.infinite?
115
+ assert n.negative_infinite?
116
+ assert_equal :inf, n.special
117
+ assert_equal -1, n.sign
118
+
119
+ n = Numeral[:nan]
120
+ assert n.special?
121
+ assert n.nan?
122
+ refute n.infinite?
123
+ refute n.negative_infinite?
124
+ refute n.positive_infinite?
125
+ assert_equal :nan, n.special
126
+ end
127
+
128
+ def test_numeral_equality
129
+ assert_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: +1], Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4]
130
+ assert_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: -1], Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: -1]
131
+ assert_equal Numeral[[1,2,3,4,5,6], base: 10, point: 2, repeat: 4], Numeral[[1,2,3,4,5,6], base: 10, point: 2, repeat: 4]
132
+ assert_equal Numeral[[1,2,3,4,5,6], base: 10, point: 2, repeat: 4], Numeral[[1,2,3,4,5,6], point: 2, repeat: 4]
133
+ assert_equal Numeral[[1,2,3,4,5,6], base: 10, point: 6, repeat: 4], Numeral[[1,2,3,4,5,6], point: 6, repeat: 4]
134
+ assert_equal Numeral[[1,2,3,4,5,6], base: 10, point: 6, repeat: 4], Numeral[[1,2,3,4,5,6], repeat: 4]
135
+ assert_equal Numeral[[1,2,3,4,5,6], base: 10, point: 6], Numeral[[1,2,3,4,5,6], point: 6]
136
+ assert_equal Numeral[:nan], Numeral[:nan]
137
+ assert_equal Numeral[:inf, sign: +1], Numeral[:inf, sign: +1]
138
+ assert_equal Numeral[:inf, sign: -1], Numeral[:inf, sign: -1]
139
+
140
+ refute_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: +1], Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: -1]
141
+ refute_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: -1], Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4, sign: +1]
142
+ refute_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4], Numeral[[1,2,4,3,5,6], base: 8, point: 2, repeat: 4]
143
+ refute_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4], Numeral[[1,2,3,4,5,6], base: 10, point: 2, repeat: 4]
144
+ refute_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4], Numeral[[1,2,3,4,5,6], base: 8, point: 3, repeat: 4]
145
+ refute_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4], Numeral[[1,2,3,4,5,6], base: 8, repeat: 4]
146
+ refute_equal Numeral[[1,2,3,4,5,6], base: 8, point: 2, repeat: 4], Numeral[[1,2,3,4,5,6], base: 8, point: 2]
147
+ refute_equal Numeral[:nan], Numeral[:inf, sign: -1]
148
+ refute_equal Numeral[:inf, sign: +1], Numeral[:inf, sign: -1]
149
+ refute_equal Numeral[:inf, sign: -1], Numeral[:inf, sign: +1]
150
+ refute_equal Numeral[:inf, sign: -1], Numeral[:nan]
151
+ end
152
+
153
+
154
+ def test_special_constructors
155
+ assert_equal Numeral[:nan], Numeral.nan
156
+ assert_equal Numeral[:inf, sign: +1], Numeral[:inf]
157
+ assert_equal Numeral[:inf, sign: +1], Numeral.positive_infinity
158
+ assert_equal Numeral[:inf, sign: -1], Numeral.negative_infinity
159
+ end
160
+
161
+
162
+ def test_numeral_constructors
163
+ assert_equal Numeral[2, 3, 5, 7, 9, point: 2, base: 10], Numeral[2, 3, :point, 5, 7, 9]
164
+ assert_equal Numeral[2, 3, 5, 7, 9, point: 2, repeat: 4], Numeral[2, 3, :point, 5, 7, :repeat, 9]
165
+ assert_equal Numeral[2, 3, 5, 7, 9, point: 2, repeat: 3], Numeral[2, 3, :point, 5, :repeat, 7, 9]
166
+ assert_equal Numeral[2, 3, 5, 7, 9, point: 2, repeat: 2], Numeral[2, 3, :point, :repeat, 5, 7, 9]
167
+ assert_equal Numeral[2, 3, 5, 7, 9, point: 2, repeat: 2], Numeral[2, 3, :repeat, :point, 5, 7, 9]
168
+ assert_equal Numeral[2, 3, 5, 7, 9, point: 3, repeat: 1], Numeral[2, :repeat, 3, 5, :point, 7, 9]
169
+ assert_equal Numeral[2, 3, 5, 7, 9, point: 2, base: 10], Numeral[Digits[2, 3, 5, 7, 9, base: 10], point: 2]
170
+
171
+ assert_equal Numeral[0, point: 1, base: 10], Numeral.zero
172
+ assert_equal Numeral[0, point: 1, base: 8], Numeral.zero(base: 8)
173
+ assert_equal Numeral[1, 1, 5, point: 5, sign: +1, base: 10], Numeral.integer(11500)
174
+ assert_equal Numeral[1, 1, 5, point: 5, sign: -1, base: 10], Numeral.integer(-11500)
175
+ assert_equal Numeral[2, 6, 3, 5, 4, point: 5, sign: +1, base: 8], Numeral.integer(+11500, base: 8)
176
+ assert_equal Numeral[2, 6, 3, 5, 4, point: 5, sign: -1, base: 8], Numeral.integer(-11500, base: 8)
177
+ end
178
+
179
+ def test_quotient_to_numeral
180
+ assert_equal Numeral[2, 3, point: 1, repeat: 1], Numeral.from_quotient(7,3)
181
+ assert_equal Numeral[2, 3, point: 1, repeat: 1], Numeral.from_quotient([7,3])
182
+ assert_equal Numeral[:nan], Numeral.from_quotient(0,0)
183
+ assert_equal Numeral[:inf, sign: +1], Numeral.from_quotient(1,0)
184
+ assert_equal Numeral[:inf, sign: +1], Numeral.from_quotient(10,0)
185
+ assert_equal Numeral[:inf, sign: -1], Numeral.from_quotient(-1,0)
186
+ assert_equal Numeral[:inf, sign: -1], Numeral.from_quotient(-10,0)
187
+ assert_equal Numeral[2,5,4,3,4,2,1,2,6,7,8, point: 3, repeat: 8], Numeral.from_quotient(4234796411, 16650000)
188
+ assert_equal Numeral[3, point: 0, repeat: 0], Numeral.from_quotient(1, 3)
189
+ assert_equal Numeral[3, point: 1, repeat: 0], Numeral.from_quotient(10, 3)
190
+ assert_equal Numeral[3, point: 2, repeat: 0], Numeral.from_quotient(100, 3)
191
+ assert_equal Numeral[3, point: 3, repeat: 0], Numeral.from_quotient(1000, 3)
192
+ assert_equal Numeral[3, point: -1, repeat: 0], Numeral.from_quotient(1, 30)
193
+ assert_equal Numeral[3, point: -2, repeat: 0], Numeral.from_quotient(1, 300)
194
+ assert_equal Numeral[3, point: -3, repeat: 0], Numeral.from_quotient(1, 3000)
195
+ assert_equal Numeral[7,2,1,4, point: 1, repeat: 2, sign: -1], Numeral.from_quotient(-3571, 495)
196
+ assert_equal Numeral[1,2,3,4,5,4,5, point: 1, repeat: 1], Numeral.from_quotient(1234544, 999999)
197
+ assert_equal Numeral[1,2,3,4,5,4,5,2,3,4,5, point: 1, repeat: 9], Numeral.from_quotient(678999879, 550000000)
198
+ assert_equal Numeral[1,2,3,4,5,4,5,2,3,4,5, point: 1, repeat: 9], Numeral.from_quotient(678999879, 550000000)
199
+ end
200
+
201
+ def test_numeral_to_quotient
202
+ assert_equal [7, 3], Numeral[2, 3, point: 1, repeat: 1].to_quotient
203
+ assert_equal [-7, 3], Numeral[2, 3, point: 1, repeat: 1, sign: -1].to_quotient
204
+ assert_equal [0,0], Numeral[:nan].to_quotient
205
+ assert_equal [1,0], Numeral[:inf, sign: +1].to_quotient
206
+ assert_equal [-1,0], Numeral[:inf, sign: -1].to_quotient
207
+ assert_equal [4, 3], Numeral[1, 3, point: 1, repeat: 1].to_quotient
208
+ assert_equal [4234796411, 16650000], Numeral[2,5,4,3,4,2,1,2,6,7,8, point: 3, repeat: 8].to_quotient
209
+ assert_equal [1, 3], Numeral[3, point: 0, repeat: 0].to_quotient
210
+ assert_equal [1, 3], Numeral[0,3, point: 1, repeat: 1].to_quotient
211
+ assert_equal [10, 3], Numeral[3, point: 1, repeat: 0].to_quotient
212
+ assert_equal [100, 3], Numeral[3, point: 2, repeat: 0].to_quotient
213
+ assert_equal [1000, 3], Numeral[3, point: 3, repeat: 0].to_quotient
214
+ assert_equal [1000, 3], Numeral[3,3,3,3, point: 3, repeat: 3].to_quotient
215
+ assert_equal [1, 30], Numeral[3, point: -1, repeat: 0].to_quotient
216
+ assert_equal [1, 300], Numeral[3, point: -2, repeat: 0].to_quotient
217
+ assert_equal [1, 3000], Numeral[3, point: -3, repeat: 0].to_quotient
218
+ assert_equal [1, 3000], Numeral[0,0,0,0,3, point: 1, repeat: 4].to_quotient
219
+ assert_equal [-3571, 495], Numeral[7,2,1,4, point: 1, repeat: 2, sign: -1].to_quotient
220
+ assert_equal [-3571, 495], Numeral[7,2,1,4,1,4, point: 1, repeat: 4, sign: -1].to_quotient
221
+ assert_equal [-3571, 495], Numeral[7,2,1,4,1,4,1,4, point: 1, repeat: 6, sign: -1].to_quotient
222
+ assert_equal [1234544, 999999], Numeral[1,2,3,4,5,4,5, point: 1, repeat: 1].to_quotient
223
+ assert_equal [678999879, 550000000], Numeral[1,2,3,4,5,4,5,2,3,4,5, point: 1, repeat: 9].to_quotient
224
+ assert_equal [-3571, 495], Numeral[7,2,1,4, point: 1, repeat: 2, sign: -1].to_quotient
225
+ assert_equal [-3571, 495], Numeral[7,2,1,4,1,4, point: 1, repeat: 4, sign: -1].to_quotient
226
+ assert_equal [-3571, 495], Numeral[7,2,1,4,1,4,1,4, point: 1, repeat: 6, sign: -1].to_quotient
227
+ assert_equal [1234544, 999999], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5, point: 1, repeat: 7].to_quotient
228
+ assert_equal [678999879, 550000000], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5,4,5, point: 1, repeat: 13].to_quotient
229
+ assert_equal [678999879, 550000000], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5, point: 1, repeat: 11].to_quotient
230
+ assert_equal [678999879, 550000000], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5, point: 1, repeat: 9].to_quotient
231
+
232
+ assert_equal [1, 1], Numeral[9, point: 0, repeat: 0].to_quotient
233
+ assert_equal [1, 5], Numeral[1,9, point: 0, repeat: 1].to_quotient
234
+ assert_equal [1, 10], Numeral[1,9, point: 0, repeat: 1, base: 16].to_quotient
235
+ end
236
+
237
+ def test_numeral_normalization
238
+ # exclude extra repetitions
239
+ assert_equal Numeral[7,2,1,4, point: 1, repeat: 2], Numeral[7,2,1,4,1,4, point: 1, repeat: 4]
240
+ assert_equal Numeral[7,2,1,4, point: 1, repeat: 2], Numeral[7,2,1,4,1,4,1,4, point: 1, repeat: 6]
241
+ assert_equal Numeral[1,2,3,4,5,4,5, point: 1, repeat: 1], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5, point: 1, repeat: 7]
242
+ assert_equal Numeral[1,2,3,4,5,4,5,2,3,4,5, point: 1, repeat: 9], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5,4,5, point: 1, repeat: 13]
243
+ assert_equal Numeral[1,2,3,4,5,4,5,2,3,4,5, point: 1, repeat: 9], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5, point: 1, repeat: 11]
244
+ assert_equal Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5, point: 1, repeat: 9], Numeral[1,2,3,4,5,4,5,2,3,4,5,4,5, point: 1, repeat: 9]
245
+
246
+ assert_equal Numeral.integer(1), Numeral[9, point: 0, repeat: 0]
247
+ assert_equal Numeral[2, point: 0], Numeral[1,9, point: 0, repeat: 1]
248
+
249
+ end
250
+
251
+ def test_quotient_conversion
252
+ [[7,3], [1,3], [10,3], [100,3], [1000,3], [1000000000000, 3], [3,1], [3,1000],
253
+ [1, 30], [1, 300], [1, 3000],
254
+ [117,119], [1,10], [1,100], [100,1]].each do |num, den|
255
+ r = Rational(num, den)
256
+ r = [r.numerator, r.denominator]
257
+ [3,5,7,8,10,16,32,50].each do |base|
258
+ assert_equal r, Numeral.from_quotient(r, base: base).to_quotient
259
+ end
260
+ end
261
+ # TODO: set maximum_digits properly and test:
262
+ # assert_equal Rational(23, 34324241934923424), Numeral.from_quotient(Rational(23, 34324241934923424)).to_quotient
263
+ end
264
+
265
+ def test_expand
266
+ assert_equal Digits[1,2,3,4,5], Numeral[1,2,3,4,5, point: 1].expand(0).digits
267
+ assert_equal 1, Numeral[1,2,3,4,5, point: 1].expand(0).point
268
+ assert_equal Digits[1,2,3,4,5], Numeral[1,2,3,4,5, point: 1].expand(1).digits
269
+ assert_equal Digits[1,2,3,4,5], Numeral[1,2,3,4,5, point: 1].expand(4).digits
270
+ assert_equal Digits[1,2,3,4,5], Numeral[1,2,3,4,5, point: 1].expand(5).digits
271
+ assert_equal Digits[1,2,3,4,5,0], Numeral[1,2,3,4,5, point: 1].expand(6).digits
272
+ assert_equal Digits[1,2,3,4,5,0,0], Numeral[1,2,3,4,5, point: 1].expand(7).digits
273
+ assert_equal Digits[1,2,3,4,5,0,0,0], Numeral[1,2,3,4,5, point: 1].expand(8).digits
274
+ assert_equal Digits[1,2,3,4,5,0,0,0,0], Numeral[1,2,3,4,5, point: 1].expand(9).digits
275
+ assert_equal Digits[1,2,3,4,5,0,0,0,0,0], Numeral[1,2,3,4,5, point: 1].expand(10).digits
276
+ assert_equal Digits[1,2,3,4,5,0,0,0,0,0,0], Numeral[1,2,3,4,5, point: 1].expand(11).digits
277
+ assert_equal Numeral[1,2,3,4,5,0,0,0,0,0,0, point: 1, unnormalized: true], Numeral[1,2,3,4,5, point: 1].expand(11)
278
+ assert_equal Digits[1,2,3,4,5,0,0,0,0,0,0], Numeral[1,2,3,4,5, point: 0].expand(11).digits
279
+ assert_equal Digits[1,2,3,4,5,0,0,0,0,0,0], Numeral[1,2,3,4,5, point: 4].expand(11).digits
280
+ assert_equal Digits[1,2,3,4,5,0,0,0,0,0,0], Numeral[1,2,3,4,5, point: 5].expand(11).digits
281
+ assert_equal Digits[1,2,3,4,5,0,0,0,0,0,0], Numeral[1,2,3,4,5, point: 20].expand(11).digits
282
+ assert_equal Digits[], Numeral[0, point: 1].expand(0).digits
283
+ assert_equal Digits[0], Numeral[0, point: 1].expand(1).digits
284
+ assert_equal Digits[0,0], Numeral[0, point: 1].expand(2).digits
285
+ assert_equal Digits[0,0,0], Numeral[0, point: 1].expand(3).digits
286
+ assert_equal Digits[0,0,0,0], Numeral[0, point: 1].expand(4).digits
287
+ assert_equal Digits[1,2,3,4,5,0,0,0,0,0,0], Numeral[1,2,3,4,5, point: 1, sign: -1].expand(11).digits
288
+ assert_equal Digits[7,2,1,4], Numeral[7,2,1,4,1,4, point: 1, repeat: 4].expand(3).digits
289
+ assert_equal Digits[7,2,1,4], Numeral[7,2,1,4,1,4, point: 1, repeat: 4].expand(4).digits
290
+ assert_equal Digits[7,2,1,4,1], Numeral[7,2,1,4,1,4, point: 1, repeat: 4].expand(5).digits
291
+ assert_equal Digits[7,2,1,4,1,4], Numeral[7,2,1,4,1,4, point: 1, repeat: 4].expand(6).digits
292
+ assert_equal Digits[7,2,1,4,1,4,1], Numeral[7,2,1,4,1,4, point: 1, repeat: 4].expand(7).digits
293
+ assert_equal Numeral[1,2,3,4,5,0,0,0,0, point: 1, unnormalized: true], Numeral[1,2,3,4,5, point: 1].expand(11)
294
+ assert_equal Digits[1,1,1,1], Numeral[0,1, point: 0, repeat: 1].expand(4).digits
295
+ end
296
+
297
+ def test_approximate_numerals
298
+ approx = Numeral[1,2,3,4,5,0,0,0, point: 1, normalize: :approximate]
299
+ assert approx.approximate?
300
+ assert_equal Digits[1,2,3,4,5,0,0,0], approx.digits
301
+ assert_equal 1, approx.point
302
+ assert_nil approx.repeat
303
+ refute approx.repeating?
304
+ exact = approx.exact
305
+ assert exact.exact?
306
+ assert_equal Digits[1,2,3,4,5], exact.digits
307
+ assert_equal 1, exact.point
308
+ assert_equal exact.digits.size, exact.repeat
309
+ refute exact.repeating?
310
+ approx = approx.approximate(approx.digits.size)
311
+ assert approx.approximate?
312
+ assert_equal Digits[1,2,3,4,5,0,0,0], approx.digits
313
+ assert_equal 1, approx.point
314
+ assert_nil approx.repeat
315
+ refute approx.repeating?
316
+
317
+ approx = Numeral[1,2,3,4,5, point: 1].approximate(8)
318
+ assert approx.approximate?
319
+ assert_equal Digits[1,2,3,4,5,0,0,0], approx.digits
320
+ assert_equal 1, approx.point
321
+ assert_nil approx.repeat
322
+ refute approx.repeating?
323
+ exact = approx.exact
324
+ assert exact.exact?
325
+ assert_equal Digits[1,2,3,4,5], exact.digits
326
+ assert_equal 1, exact.point
327
+ assert_equal exact.digits.size, exact.repeat
328
+ refute exact.repeating?
329
+ approx = approx.approximate(approx.digits.size)
330
+ assert approx.approximate?
331
+ assert_equal Digits[1,2,3,4,5,0,0,0], approx.digits
332
+ assert_equal 1, approx.point
333
+ assert_nil approx.repeat
334
+
335
+ exact = Numeral.from_quotient([1,3])
336
+ assert exact.exact?
337
+ assert_equal Digits[3], exact.digits
338
+ assert_equal 0, exact.point
339
+ assert_equal 0, exact.repeat
340
+ assert exact.repeating?
341
+ approx = Numeral.from_quotient([1,3]).approximate(10)
342
+ assert approx.approximate?
343
+ assert_equal Digits[3,3,3,3,3,3,3,3,3,3], approx.digits
344
+ assert_equal 0, approx.point
345
+ assert_nil approx.repeat
346
+ refute approx.repeating?
347
+ exact = approx.exact
348
+ assert exact.exact?
349
+ assert_equal Digits[3,3,3,3,3,3,3,3,3,3], exact.digits
350
+ assert_equal 0, exact.point
351
+ assert_equal exact.digits.size, exact.repeat
352
+ refute exact.repeating?
353
+
354
+ assert Numeral.from_quotient([1,3]).exact?
355
+ assert Numeral[1,2,3,4,5, point: 1].exact?
356
+ exact = Numeral[1,2,3,4,5,0,0,0, point: 1]
357
+ refute exact.approximate?
358
+ assert_equal Digits[1,2,3,4,5], exact.digits
359
+ assert_equal 1, exact.point
360
+ assert_equal exact.digits.size, exact.repeat
361
+ exact = exact.exact
362
+ assert exact.exact?
363
+ assert_equal Digits[1,2,3,4,5], exact.digits
364
+ end
365
+
366
+ end
@@ -0,0 +1,75 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
2
+
3
+ require 'numerals'
4
+ include Numerals
5
+
6
+ class TestRationalConversions < Test::Unit::TestCase # < Minitest::Test
7
+
8
+ def test_special
9
+ assert_raise(ZeroDivisionError){ Conversions.numeral_to_number(Numeral.nan, Rational) }
10
+ assert_raise(ZeroDivisionError){ Conversions.numeral_to_number(Numeral.infinity, Rational) }
11
+ assert_raise(ZeroDivisionError){ Conversions.numeral_to_number(Numeral.infinity(-1), Rational) }
12
+ end
13
+
14
+ def test_rational_to_numeral
15
+
16
+ exact = Rounding[:exact]
17
+ nine_digits = Rounding[:half_even, precision: 9]
18
+
19
+ assert_equal Numeral[3, point: 0, repeat: 0],
20
+ Conversions.number_to_numeral(Rational(1, 3))
21
+ assert_equal Numeral[3, point: 0, repeat: 0],
22
+ Conversions.number_to_numeral(Rational(1, 3), exact)
23
+ assert_equal Numeral[[3]*9, point: 0, normalize: :approximate],
24
+ Conversions.number_to_numeral(Rational(1, 3), nine_digits)
25
+
26
+ assert_equal Numeral[3, point: 0, repeat: 0, sign: -1],
27
+ Conversions.number_to_numeral(Rational(-1, 3))
28
+ assert_equal Numeral[3, point: 0, repeat: 0, sign: -1],
29
+ Conversions.number_to_numeral(Rational(-1, 3), exact)
30
+ assert_equal Numeral[[3]*9, point: 0, sign: -1, normalize: :approximate],
31
+ Conversions.number_to_numeral(Rational(-1, 3), nine_digits)
32
+
33
+ assert_equal Numeral[1, point: 0],
34
+ Conversions.number_to_numeral(Rational(1, 10))
35
+ assert_equal Numeral[1, point: 0],
36
+ Conversions.number_to_numeral(Rational(1, 10), exact)
37
+ assert_equal Numeral[1,0,0,0,0,0,0,0,0, point: 0, normalize: :approximate],
38
+ Conversions.number_to_numeral(Rational(1, 10), nine_digits)
39
+
40
+ assert_equal Numeral[1, point: 0, sign: -1],
41
+ Conversions.number_to_numeral(Rational(-1, 10))
42
+ assert_equal Numeral[1, point: 0, sign: -1],
43
+ Conversions.number_to_numeral(Rational(-1, 10), exact)
44
+ assert_equal Numeral[1,0,0,0,0,0,0,0,0, point: 0, sign: -1, normalize: :approximate],
45
+ Conversions.number_to_numeral(Rational(-1, 10), nine_digits)
46
+
47
+ assert_equal Numeral[4,2, point: 2],
48
+ Conversions.number_to_numeral(Rational(42, 1))
49
+ assert_equal Numeral[4,2, point: 2],
50
+ Conversions.number_to_numeral(Rational(42, 1), exact)
51
+ assert_equal Numeral[4,2,0,0,0,0,0,0,0, point: 2, normalize: :approximate],
52
+ Conversions.number_to_numeral(Rational(42, 1), nine_digits)
53
+
54
+ end
55
+
56
+ def test_numeral_to_rational
57
+
58
+ assert_equal Rational(1, 3),
59
+ Conversions.numeral_to_number(Numeral[3, point: 0, repeat: 0], Rational)
60
+ assert_equal Rational(333_333_333, 1_000_000_000),
61
+ Conversions.numeral_to_number(Numeral[[3]*9, point: 0, normalize: :approximate], Rational)
62
+
63
+ assert_equal Rational(1, 10),
64
+ Conversions.numeral_to_number(Numeral[1, point: 0], Rational)
65
+ assert_equal Rational(1_000_000_000, 10_000_000_000),
66
+ Conversions.numeral_to_number(Numeral[1,0,0,0,0,0,0,0,0, point: 0, normalize: :approximate], Rational)
67
+
68
+ assert_equal Rational(42, 1),
69
+ Conversions.numeral_to_number(Numeral[4, 2, point: 2], Rational)
70
+ assert_equal Rational(42_000_000_000, 1_000_000_000),
71
+ Conversions.numeral_to_number(Numeral[4,2,0,0,0,0,0,0,0, point: 2, normalize: :approximate], Rational)
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,77 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
2
+
3
+ require 'numerals/rounding'
4
+ include Numerals
5
+
6
+ class TestRounding < Test::Unit::TestCase # < Minitest::Test
7
+
8
+ def test_rounding
9
+ r = Rounding[:half_even, places: 0]
10
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3])
11
+ assert_equal Numeral[1,0,2, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3])
12
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3])
13
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3])
14
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3])
15
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3])
16
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3])
17
+
18
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3, sign: -1])
19
+ assert_equal Numeral[1,0,2, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3, sign: -1])
20
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3, sign: -1])
21
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3, sign: -1])
22
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3, sign: -1])
23
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3, sign: -1])
24
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3, sign: -1])
25
+
26
+ r = Rounding[:half_up, places: 0]
27
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3])
28
+ assert_equal Numeral[1,0,2, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3])
29
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3])
30
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3])
31
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3])
32
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3])
33
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3])
34
+
35
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3, sign: -1])
36
+ assert_equal Numeral[1,0,2, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3, sign: -1])
37
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3, sign: -1])
38
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3, sign: -1])
39
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3, sign: -1])
40
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3, sign: -1])
41
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3, sign: -1])
42
+
43
+ r = Rounding[:half_even, precision: 3]
44
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3])
45
+ assert_equal Numeral[1,0,2, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3])
46
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3])
47
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3])
48
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3])
49
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3])
50
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3])
51
+
52
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3, sign: -1])
53
+ assert_equal Numeral[1,0,2, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3, sign: -1])
54
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3, sign: -1])
55
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3, sign: -1])
56
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3, sign: -1])
57
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3, sign: -1])
58
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3, sign: -1])
59
+
60
+ r = Rounding[:half_up, precision: 3]
61
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3])
62
+ assert_equal Numeral[1,0,2, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3])
63
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3])
64
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3])
65
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3])
66
+ assert_equal Numeral[1,0,1, point: 3, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3])
67
+ assert_equal Numeral[1,0,0, point: 3, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3])
68
+
69
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5, point: 3, sign: -1])
70
+ assert_equal Numeral[1,0,2, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,5, point: 3, sign: -1])
71
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,5,0,0,1, point: 3, sign: -1])
72
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,6, point: 3, sign: -1])
73
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9, point: 3, sign: -1])
74
+ assert_equal Numeral[1,0,1, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,1,4,9,9,9, point: 3, sign: -1])
75
+ assert_equal Numeral[1,0,0, point: 3, sign: -1, normalize: :approximate], r.round(Numeral[1,0,0,4,9,9,9,9,9,9,9,9,9,9,9,9, point: 3, sign: -1])
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: numerals
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Javier Goizueta
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: flt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: modalsupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Number formatting and reading.
70
+ email:
71
+ - jgoizueta@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/numerals.rb
82
+ - lib/numerals/conversions.rb
83
+ - lib/numerals/conversions/bigdecimal.rb
84
+ - lib/numerals/conversions/float.rb
85
+ - lib/numerals/conversions/flt.rb
86
+ - lib/numerals/conversions/integer.rb
87
+ - lib/numerals/conversions/rational.rb
88
+ - lib/numerals/digits.rb
89
+ - lib/numerals/formatting/digits_definition.rb
90
+ - lib/numerals/formatting/options.rb
91
+ - lib/numerals/numeral.rb
92
+ - lib/numerals/rounding.rb
93
+ - lib/numerals/support.rb
94
+ - lib/numerals/version.rb
95
+ - numerals.gemspec
96
+ - test/data.yaml
97
+ - test/helper.rb
98
+ - test/test_digits_definition.rb
99
+ - test/test_float_conversions.rb
100
+ - test/test_flt_conversions.rb
101
+ - test/test_integer_conversions.rb
102
+ - test/test_numeral.rb
103
+ - test/test_rational_conversions.rb
104
+ - test/test_rounding.rb
105
+ homepage: https://github.com/jgoizueta/numerals
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.2.2
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Number representation as text.
129
+ test_files:
130
+ - test/data.yaml
131
+ - test/helper.rb
132
+ - test/test_digits_definition.rb
133
+ - test/test_float_conversions.rb
134
+ - test/test_flt_conversions.rb
135
+ - test/test_integer_conversions.rb
136
+ - test/test_numeral.rb
137
+ - test/test_rational_conversions.rb
138
+ - test/test_rounding.rb