numerals 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +149 -5
  3. data/lib/numerals/conversions/bigdecimal.rb +209 -9
  4. data/lib/numerals/conversions/context_conversion.rb +40 -0
  5. data/lib/numerals/conversions/float.rb +106 -71
  6. data/lib/numerals/conversions/flt.rb +115 -44
  7. data/lib/numerals/conversions/integer.rb +32 -3
  8. data/lib/numerals/conversions/rational.rb +27 -3
  9. data/lib/numerals/conversions.rb +74 -33
  10. data/lib/numerals/digits.rb +8 -5
  11. data/lib/numerals/format/base_scaler.rb +160 -0
  12. data/lib/numerals/format/exp_setter.rb +218 -0
  13. data/lib/numerals/format/format.rb +257 -0
  14. data/lib/numerals/format/input.rb +140 -0
  15. data/lib/numerals/format/mode.rb +157 -0
  16. data/lib/numerals/format/notation.rb +51 -0
  17. data/lib/numerals/format/notations/html.rb +53 -0
  18. data/lib/numerals/format/notations/latex.rb +48 -0
  19. data/lib/numerals/format/notations/text.rb +141 -0
  20. data/lib/numerals/format/output.rb +167 -0
  21. data/lib/numerals/format/symbols.rb +565 -0
  22. data/lib/numerals/format/text_parts.rb +35 -0
  23. data/lib/numerals/format.rb +25 -0
  24. data/lib/numerals/formatting_aspect.rb +36 -0
  25. data/lib/numerals/numeral.rb +34 -21
  26. data/lib/numerals/repeat_detector.rb +99 -0
  27. data/lib/numerals/rounding.rb +340 -181
  28. data/lib/numerals/version.rb +1 -1
  29. data/lib/numerals.rb +4 -2
  30. data/numerals.gemspec +1 -1
  31. data/test/test_base_scaler.rb +189 -0
  32. data/test/test_big_conversions.rb +105 -0
  33. data/test/test_digits_definition.rb +23 -28
  34. data/test/test_exp_setter.rb +732 -0
  35. data/test/test_float_conversions.rb +48 -30
  36. data/test/test_flt_conversions.rb +476 -80
  37. data/test/test_format.rb +124 -0
  38. data/test/test_format_input.rb +226 -0
  39. data/test/test_format_mode.rb +124 -0
  40. data/test/test_format_output.rb +789 -0
  41. data/test/test_integer_conversions.rb +22 -22
  42. data/test/test_numeral.rb +35 -0
  43. data/test/test_rational_conversions.rb +28 -28
  44. data/test/test_repeat_detector.rb +72 -0
  45. data/test/test_rounding.rb +158 -0
  46. data/test/test_symbols.rb +32 -0
  47. metadata +38 -5
  48. data/lib/numerals/formatting/digits_definition.rb +0 -75
@@ -5,46 +5,46 @@ include Numerals
5
5
 
6
6
  class TestIntegerConversions < Test::Unit::TestCase # < Minitest::Test
7
7
 
8
- def test_special
9
- assert_raise(ZeroDivisionError){ Conversions.numeral_to_number(Numeral.nan, Integer) }
10
- assert_raise(ZeroDivisionError){ Conversions.numeral_to_number(Numeral.infinity, Integer) }
11
- assert_raise(ZeroDivisionError){ Conversions.numeral_to_number(Numeral.infinity(-1), Integer) }
8
+ def test_read_special
9
+ assert_raise(ZeroDivisionError){ Conversions.read(Numeral.nan, type: Integer) }
10
+ assert_raise(ZeroDivisionError){ Conversions.read(Numeral.infinity, type: Integer) }
11
+ assert_raise(ZeroDivisionError){ Conversions.read(Numeral.infinity(-1), type: Integer) }
12
12
  end
13
13
 
14
- def test_integer_to_numeral
15
- exact = Rounding[:exact]
14
+ def test_write
15
+ exact = Rounding[:short]
16
16
  nine_digits = Rounding[:half_even, precision: 9]
17
17
 
18
- assert_equal Numeral[1, point: 1], Conversions.number_to_numeral(1)
19
- assert_equal Numeral[1, point: 1], Conversions.number_to_numeral(1, exact)
18
+ assert_equal Numeral[1, point: 1], Conversions.write(1)
19
+ assert_equal Numeral[1, point: 1], Conversions.write(1, rounding: exact)
20
20
  assert_equal Numeral[1, 0, 0, 0, 0, 0, 0, 0, 0, point: 1],
21
- Conversions.number_to_numeral(1, nine_digits)
21
+ Conversions.write(1, rounding: nine_digits)
22
22
 
23
- assert_equal Numeral[1, point: 1, sign: -1], Conversions.number_to_numeral(-1)
24
- assert_equal Numeral[1, point: 1, sign: -1], Conversions.number_to_numeral(-1, exact)
23
+ assert_equal Numeral[1, point: 1, sign: -1], Conversions.write(-1)
24
+ assert_equal Numeral[1, point: 1, sign: -1], Conversions.write(-1, rounding: exact)
25
25
  assert_equal Numeral[1, 0, 0, 0, 0, 0, 0, 0, 0, point: 1, sign: -1],
26
- Conversions.number_to_numeral(-1, nine_digits)
26
+ Conversions.write(-1, rounding: nine_digits)
27
27
 
28
- assert_equal Numeral[4, 2, point: 2], Conversions.number_to_numeral(42)
29
- assert_equal Numeral[4, 2, point: 2], Conversions.number_to_numeral(42, exact)
28
+ assert_equal Numeral[4, 2, point: 2], Conversions.write(42)
29
+ assert_equal Numeral[4, 2, point: 2], Conversions.write(42, rounding: exact)
30
30
  assert_equal Numeral[4, 2, 0, 0, 0, 0, 0, 0, 0, point: 2],
31
- Conversions.number_to_numeral(42, nine_digits)
31
+ Conversions.write(42, rounding: nine_digits)
32
32
  end
33
33
 
34
- def test_numeral_to_rational
34
+ def test_read
35
35
  assert_raise IntegerConversion::InvalidConversion do
36
- Conversions.numeral_to_number(Numeral[3, point: 0, repeat: 0], Integer)
36
+ Conversions.read(Numeral[3, point: 0, repeat: 0], type: Integer)
37
37
  end
38
38
  assert_raise IntegerConversion::InvalidConversion do
39
- Conversions.numeral_to_number(Numeral[[3]*9, point: 0, normalize: :approximate], Integer)
39
+ Conversions.read(Numeral[[3]*9, point: 0, normalize: :approximate], type: Integer)
40
40
  end
41
41
  assert_raise IntegerConversion::InvalidConversion do
42
- Conversions.numeral_to_number(Numeral[1, point: 0], Integer)
42
+ Conversions.read(Numeral[1, point: 0], type: Integer)
43
43
  end
44
- assert_equal 1, Conversions.numeral_to_number(Numeral[1, point: 1], Integer)
45
- assert_equal -1, Conversions.numeral_to_number(Numeral[1, point: 1, sign: -1], Integer)
44
+ assert_equal 1, Conversions.read(Numeral[1, point: 1], type: Integer)
45
+ assert_equal -1, Conversions.read(Numeral[1, point: 1, sign: -1], type: Integer)
46
46
  assert_equal 42,
47
- Conversions.numeral_to_number(Numeral[4, 2, point: 2], Integer)
47
+ Conversions.read(Numeral[4, 2, point: 2], type: Integer)
48
48
  end
49
49
 
50
50
  end
data/test/test_numeral.rb CHANGED
@@ -196,6 +196,8 @@ class TestNumeral < Test::Unit::TestCase # < Minitest::Test
196
196
  assert_equal Numeral[1,2,3,4,5,4,5, point: 1, repeat: 1], Numeral.from_quotient(1234544, 999999)
197
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
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
+ assert_equal Numeral[1, 1, point: -3, repeat: -2, base: 2],
200
+ Numeral.from_quotient(Rational(1,10), base:2)
199
201
  end
200
202
 
201
203
  def test_numeral_to_quotient
@@ -245,7 +247,18 @@ class TestNumeral < Test::Unit::TestCase # < Minitest::Test
245
247
 
246
248
  assert_equal Numeral.integer(1), Numeral[9, point: 0, repeat: 0]
247
249
  assert_equal Numeral[2, point: 0], Numeral[1,9, point: 0, repeat: 1]
250
+ end
248
251
 
252
+ def test_partial_unneeded_rep_normalization
253
+ numeral = Numeral[1, 0, 1, 1, 1, 0, base: 2, point: 2, repeat: 2]
254
+ assert_equal Numeral[1, 0, 1, 1, 1, base: 2, point: 2, repeat: 1, normalize: false], numeral
255
+ assert_equal Numeral[1,2,3,4,5, point: 1, repeat: 3, normalize: false], Numeral[1,2,3,4,5, point: 1, repeat: 3]
256
+ assert_equal Numeral[1,2,3,4,5, point: 1, repeat: 3, normalize: false], Numeral[1,2,3,4,5,4, point: 1, repeat: 4]
257
+ assert_equal Numeral[1,2,3,4,5, point: 1, repeat: 3, normalize: false], Numeral[1,2,3,4,5,4,5, point: 1, repeat: 5]
258
+ assert_equal Numeral[1,2,3,4,5,6,7,8, point: 1, repeat: 3, normalize: false], Numeral[1,2,3,4,5,6,7,8,4, point: 1, repeat: 4]
259
+ assert_equal Numeral[1,2,3,4,5,6,7,8, point: 1, repeat: 3, normalize: false], Numeral[1,2,3,4,5,6,7,8,4,5, point: 1, repeat: 5]
260
+ assert_equal Numeral[1,2,3,4,5,6,7,8, point: 1, repeat: 3, normalize: false], Numeral[1,2,3,4,5,6,7,8,4,5,6, point: 1, repeat: 6]
261
+ assert_equal Numeral[1,2,3,4,5,6,7,8,4,6, point: 1, repeat: 5, normalize: false], Numeral[1,2,3,4,5,6,7,8,4,6, point: 1, repeat: 5]
249
262
  end
250
263
 
251
264
  def test_quotient_conversion
@@ -363,4 +376,26 @@ class TestNumeral < Test::Unit::TestCase # < Minitest::Test
363
376
  assert_equal Digits[1,2,3,4,5], exact.digits
364
377
  end
365
378
 
379
+ def test_digit_value_at
380
+ n = Numeral[1,2,3, repeat: -10, point: 1]
381
+ (-20...0).each do |i|
382
+ assert_equal 0, n.digit_value_at(i)
383
+ end
384
+ assert_equal 1, n.digit_value_at(0)
385
+ assert_equal 2, n.digit_value_at(1)
386
+ assert_equal 3, n.digit_value_at(2)
387
+ (3...13).each do |i|
388
+ assert_equal 0, n.digit_value_at(i)
389
+ end
390
+ assert_equal 1, n.digit_value_at(13)
391
+ assert_equal 2, n.digit_value_at(14)
392
+ assert_equal 3, n.digit_value_at(15)
393
+ (16...26).each do |i|
394
+ assert_equal 0, n.digit_value_at(i)
395
+ end
396
+ assert_equal 1, n.digit_value_at(26)
397
+ assert_equal 2, n.digit_value_at(27)
398
+ assert_equal 3, n.digit_value_at(28)
399
+ end
400
+
366
401
  end
@@ -5,70 +5,70 @@ include Numerals
5
5
 
6
6
  class TestRationalConversions < Test::Unit::TestCase # < Minitest::Test
7
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) }
8
+ def test_read_special
9
+ assert_raise(ZeroDivisionError){ Conversions.read(Numeral.nan, type: Rational) }
10
+ assert_raise(ZeroDivisionError){ Conversions.read(Numeral.infinity, type: Rational) }
11
+ assert_raise(ZeroDivisionError){ Conversions.read(Numeral.infinity(-1), type: Rational) }
12
12
  end
13
13
 
14
- def test_rational_to_numeral
14
+ def test_write
15
15
 
16
- exact = Rounding[:exact]
16
+ exact = Rounding[:short]
17
17
  nine_digits = Rounding[:half_even, precision: 9]
18
18
 
19
19
  assert_equal Numeral[3, point: 0, repeat: 0],
20
- Conversions.number_to_numeral(Rational(1, 3))
20
+ Conversions.write(Rational(1, 3))
21
21
  assert_equal Numeral[3, point: 0, repeat: 0],
22
- Conversions.number_to_numeral(Rational(1, 3), exact)
22
+ Conversions.write(Rational(1, 3), rounding: exact)
23
23
  assert_equal Numeral[[3]*9, point: 0, normalize: :approximate],
24
- Conversions.number_to_numeral(Rational(1, 3), nine_digits)
24
+ Conversions.write(Rational(1, 3), rounding: nine_digits)
25
25
 
26
26
  assert_equal Numeral[3, point: 0, repeat: 0, sign: -1],
27
- Conversions.number_to_numeral(Rational(-1, 3))
27
+ Conversions.write(Rational(-1, 3))
28
28
  assert_equal Numeral[3, point: 0, repeat: 0, sign: -1],
29
- Conversions.number_to_numeral(Rational(-1, 3), exact)
29
+ Conversions.write(Rational(-1, 3), rounding: exact)
30
30
  assert_equal Numeral[[3]*9, point: 0, sign: -1, normalize: :approximate],
31
- Conversions.number_to_numeral(Rational(-1, 3), nine_digits)
31
+ Conversions.write(Rational(-1, 3), rounding: nine_digits)
32
32
 
33
33
  assert_equal Numeral[1, point: 0],
34
- Conversions.number_to_numeral(Rational(1, 10))
34
+ Conversions.write(Rational(1, 10))
35
35
  assert_equal Numeral[1, point: 0],
36
- Conversions.number_to_numeral(Rational(1, 10), exact)
36
+ Conversions.write(Rational(1, 10), rounding: exact)
37
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)
38
+ Conversions.write(Rational(1, 10), rounding: nine_digits)
39
39
 
40
40
  assert_equal Numeral[1, point: 0, sign: -1],
41
- Conversions.number_to_numeral(Rational(-1, 10))
41
+ Conversions.write(Rational(-1, 10))
42
42
  assert_equal Numeral[1, point: 0, sign: -1],
43
- Conversions.number_to_numeral(Rational(-1, 10), exact)
43
+ Conversions.write(Rational(-1, 10), rounding: exact)
44
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)
45
+ Conversions.write(Rational(-1, 10), rounding: nine_digits)
46
46
 
47
47
  assert_equal Numeral[4,2, point: 2],
48
- Conversions.number_to_numeral(Rational(42, 1))
48
+ Conversions.write(Rational(42, 1))
49
49
  assert_equal Numeral[4,2, point: 2],
50
- Conversions.number_to_numeral(Rational(42, 1), exact)
50
+ Conversions.write(Rational(42, 1), rounding: exact)
51
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)
52
+ Conversions.write(Rational(42, 1), rounding: nine_digits)
53
53
 
54
54
  end
55
55
 
56
- def test_numeral_to_rational
56
+ def test_read
57
57
 
58
58
  assert_equal Rational(1, 3),
59
- Conversions.numeral_to_number(Numeral[3, point: 0, repeat: 0], Rational)
59
+ Conversions.read(Numeral[3, point: 0, repeat: 0], type: Rational)
60
60
  assert_equal Rational(333_333_333, 1_000_000_000),
61
- Conversions.numeral_to_number(Numeral[[3]*9, point: 0, normalize: :approximate], Rational)
61
+ Conversions.read(Numeral[[3]*9, point: 0, normalize: :approximate], type: Rational)
62
62
 
63
63
  assert_equal Rational(1, 10),
64
- Conversions.numeral_to_number(Numeral[1, point: 0], Rational)
64
+ Conversions.read(Numeral[1, point: 0], type: Rational)
65
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)
66
+ Conversions.read(Numeral[1,0,0,0,0,0,0,0,0, point: 0, normalize: :approximate], type: Rational)
67
67
 
68
68
  assert_equal Rational(42, 1),
69
- Conversions.numeral_to_number(Numeral[4, 2, point: 2], Rational)
69
+ Conversions.read(Numeral[4, 2, point: 2], type: Rational)
70
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)
71
+ Conversions.read(Numeral[4,2,0,0,0,0,0,0,0, point: 2, normalize: :approximate], type: Rational)
72
72
 
73
73
  end
74
74
 
@@ -0,0 +1,72 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
2
+
3
+ require 'numerals'
4
+ include Numerals
5
+
6
+ class TestRepeatDetector < Test::Unit::TestCase # < Minitest::Test
7
+
8
+ def test_repeat_detector
9
+
10
+ assert_equal(
11
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8], nil],
12
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8])
13
+ )
14
+
15
+ assert_equal(
16
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8], 8],
17
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8])
18
+ )
19
+
20
+ assert_equal(
21
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8], nil],
22
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8], 2)
23
+ )
24
+
25
+ assert_equal(
26
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8], 8],
27
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8], 2)
28
+ )
29
+
30
+ assert_equal(
31
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8], 8],
32
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8])
33
+ )
34
+
35
+ assert_equal(
36
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8], nil],
37
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8], 3)
38
+ )
39
+
40
+ assert_equal(
41
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8], 8],
42
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8,6,7,8], 3)
43
+ )
44
+
45
+ assert_equal(
46
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8], 8],
47
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8,6,7,8], 2)
48
+ )
49
+
50
+ assert_equal(
51
+ [[2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8], 8],
52
+ RepeatDetector.detect([2, 5, 4, 3, 4, 2, 1, 2, 6, 7, 8, 6, 7, 8, 6, 7, 8,6,7,8])
53
+ )
54
+
55
+ assert_equal(
56
+ [[3], 0],
57
+ RepeatDetector.detect([3, 3], 1)
58
+ )
59
+
60
+ assert_equal(
61
+ [[3, 3], nil],
62
+ RepeatDetector.detect([3, 3], 2)
63
+ )
64
+
65
+ assert_equal(
66
+ [[3], 0],
67
+ RepeatDetector.detect([3, 3, 3], 2)
68
+ )
69
+
70
+ end
71
+
72
+ end
@@ -74,4 +74,162 @@ class TestRounding < Test::Unit::TestCase # < Minitest::Test
74
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
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
76
  end
77
+
78
+ def test_extended_rounding
79
+ # Rounding past the precision of a number
80
+ r = Rounding[:half_up, precision: 5]
81
+ assert_equal Numeral[1, 2, 3, 0, 0, point: 1, normalize: :approximate], r.round(Numeral[1, 2, 3, point: 1, normalize: :exact])
82
+ # currently, approximate numerals cannot be extended
83
+ assert_equal Numeral[1, 2, 3, point: 1, normalize: :approximate], r.round(Numeral[1, 2, 3, point: 1, normalize: :approximate])
84
+ end
85
+
86
+ def test_constructor
87
+ assert_equal Rounding[:short], Rounding[:simplify]
88
+ r = Rounding[:short]
89
+ assert r.free?
90
+ refute r.fixed?
91
+ assert r.simplifying?
92
+ assert r.short?
93
+ refute r.preserving?
94
+ refute r.full?
95
+ assert_equal :short, r.precision
96
+ assert_equal 10, r.base
97
+ assert_nil r.places
98
+
99
+ r = Rounding[:short, base: 2]
100
+ assert r.free?
101
+ refute r.fixed?
102
+ assert r.simplifying?
103
+ refute r.preserving?
104
+ assert_equal :short, r.precision
105
+ assert_equal 2, r.base
106
+ assert_nil r.places
107
+
108
+ assert_equal Rounding[:free], Rounding[:preserve]
109
+ r = Rounding[:free]
110
+ assert r.free?
111
+ refute r.fixed?
112
+ refute r.simplifying?
113
+ assert r.preserving?
114
+ assert_equal :free, r.precision
115
+ assert_equal 10, r.base
116
+ assert_nil r.places
117
+
118
+ r = Rounding[:free, base: 2]
119
+ assert r.free?
120
+ refute r.fixed?
121
+ refute r.simplifying?
122
+ assert r.preserving?
123
+ assert_equal :free, r.precision
124
+ assert_equal 2, r.base
125
+ assert_nil r.places
126
+
127
+ r = Rounding[precision: 5]
128
+ refute r.free?
129
+ assert r.fixed?
130
+ refute r.simplifying?
131
+ refute r.preserving?
132
+ assert r.relative?
133
+ refute r.absolute?
134
+ assert_equal :half_even, r.mode
135
+ assert_equal 10, r.base
136
+ assert_equal 5, r.precision
137
+ assert_nil r.places
138
+
139
+ r = Rounding[precision: 5, base: 2]
140
+ refute r.free?
141
+ assert r.fixed?
142
+ refute r.simplifying?
143
+ refute r.preserving?
144
+ assert r.relative?
145
+ refute r.absolute?
146
+ assert_equal :half_even, r.mode
147
+ assert_equal 2, r.base
148
+ assert_equal 5, r.precision
149
+ assert_nil r.places
150
+
151
+ r = Rounding[:down, precision: 5]
152
+ refute r.free?
153
+ assert r.fixed?
154
+ refute r.simplifying?
155
+ refute r.preserving?
156
+ assert r.relative?
157
+ refute r.absolute?
158
+ assert_equal :down, r.mode
159
+ assert_equal 10, r.base
160
+ assert_equal 5, r.precision
161
+ assert_nil r.places
162
+
163
+ r = Rounding[:down, precision: 5, base: 2]
164
+ refute r.free?
165
+ assert r.fixed?
166
+ refute r.simplifying?
167
+ refute r.preserving?
168
+ assert r.relative?
169
+ refute r.absolute?
170
+ assert_equal :down, r.mode
171
+ assert_equal 2, r.base
172
+ assert_equal 5, r.precision
173
+ assert_nil r.places
174
+
175
+ r = Rounding[mode: :down, precision: 5]
176
+ refute r.free?
177
+ assert r.fixed?
178
+ refute r.simplifying?
179
+ refute r.preserving?
180
+ assert r.relative?
181
+ refute r.absolute?
182
+ assert_equal :down, r.mode
183
+ assert_equal 10, r.base
184
+ assert_equal 5, r.precision
185
+ assert_nil r.places
186
+
187
+ r = Rounding[places: 5]
188
+ refute r.free?
189
+ assert r.fixed?
190
+ refute r.simplifying?
191
+ refute r.preserving?
192
+ refute r.relative?
193
+ assert r.absolute?
194
+ assert_equal :half_even, r.mode
195
+ assert_equal 10, r.base
196
+ assert_nil r.precision
197
+ assert_equal 5, r.places
198
+ end
199
+
200
+ def test_copy
201
+ r1 = Rounding[:short]
202
+ r2 = Rounding[r1]
203
+ assert_equal :short, r2.precision
204
+ r2.precision = :free
205
+ assert_equal :short, r1.precision
206
+ assert_equal :free, r2.precision
207
+
208
+ r2 = r1[:free]
209
+ assert_equal :short, r1.precision
210
+ assert_equal :free, r2.precision
211
+
212
+ r1 = Rounding[:short, :half_up, base: 2]
213
+ r2 = r1[precision: 10]
214
+ assert_equal 2, r2.base
215
+ assert_equal 10, r2.precision
216
+ assert_equal :short, r1.precision
217
+ assert_equal :half_up, r1.mode
218
+ assert_equal :half_up, r2.mode
219
+
220
+ r1 = Rounding[:short, base: 2]
221
+ r2 = r1.set(precision: 10)
222
+ assert_equal 2, r2.base
223
+ assert_equal 10, r2.precision
224
+ assert_equal :short, r1.precision
225
+ end
226
+
227
+ def test_mutators
228
+ r1 = Rounding[:free]
229
+ r2 = r1.set!(:short)
230
+ assert_equal :short, r1.precision
231
+ assert_equal :short, r2.precision
232
+ assert_equal r1.object_id, r2.object_id
233
+ end
234
+
77
235
  end
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__),'helper.rb'))
2
+
3
+ require 'numerals/rounding'
4
+ include Numerals
5
+
6
+ class TestSymbols < Test::Unit::TestCase # < Minitest::Test
7
+
8
+ def test_symbols
9
+ s = Format::Symbols[show_plus: false]
10
+ s2 = s[uppercase: true]
11
+ assert_equal true, s2.uppercase
12
+ assert_equal false, s.uppercase
13
+ assert_equal false, s.show_plus
14
+ assert_equal false, s2.show_plus
15
+ s3 = s2[show_plus: true]
16
+ assert_equal true, s3.show_plus
17
+ assert_equal false, s.show_plus
18
+ assert_equal false, s2.show_plus
19
+ assert_equal s3, s2.set_plus(true)
20
+ s4 = s3[plus: ' ']
21
+ assert_equal s4, s3.set_plus(' ')
22
+ s5 = s4[show_exponent_plus: true]
23
+ assert_equal s5, s4.set_plus(true, :exp)
24
+ s6 = s5[show_exponent_plus: true, plus: ' ']
25
+ assert_equal s6, s5.set_plus(' ', :exp)
26
+ s7 = s6[show_exponent_plus: true, show_plus: true]
27
+ assert_equal s6, s5.set_plus(:all)
28
+ s7 = s6[show_exponent_plus: true, show_plus: true, plus: ' ']
29
+ assert_equal s7, s6.set_plus(' ', :all)
30
+ end
31
+
32
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numerals
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Goizueta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-02 00:00:00.000000000 Z
11
+ date: 2015-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: flt
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.3.4
19
+ version: 1.4.6
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.3.4
26
+ version: 1.4.6
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: modalsupport
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -81,27 +81,51 @@ files:
81
81
  - lib/numerals.rb
82
82
  - lib/numerals/conversions.rb
83
83
  - lib/numerals/conversions/bigdecimal.rb
84
+ - lib/numerals/conversions/context_conversion.rb
84
85
  - lib/numerals/conversions/float.rb
85
86
  - lib/numerals/conversions/flt.rb
86
87
  - lib/numerals/conversions/integer.rb
87
88
  - lib/numerals/conversions/rational.rb
88
89
  - lib/numerals/digits.rb
89
- - lib/numerals/formatting/digits_definition.rb
90
+ - lib/numerals/format.rb
91
+ - lib/numerals/format/base_scaler.rb
92
+ - lib/numerals/format/exp_setter.rb
93
+ - lib/numerals/format/format.rb
94
+ - lib/numerals/format/input.rb
95
+ - lib/numerals/format/mode.rb
96
+ - lib/numerals/format/notation.rb
97
+ - lib/numerals/format/notations/html.rb
98
+ - lib/numerals/format/notations/latex.rb
99
+ - lib/numerals/format/notations/text.rb
100
+ - lib/numerals/format/output.rb
101
+ - lib/numerals/format/symbols.rb
102
+ - lib/numerals/format/text_parts.rb
90
103
  - lib/numerals/formatting/options.rb
104
+ - lib/numerals/formatting_aspect.rb
91
105
  - lib/numerals/numeral.rb
106
+ - lib/numerals/repeat_detector.rb
92
107
  - lib/numerals/rounding.rb
93
108
  - lib/numerals/support.rb
94
109
  - lib/numerals/version.rb
95
110
  - numerals.gemspec
96
111
  - test/data.yaml
97
112
  - test/helper.rb
113
+ - test/test_base_scaler.rb
114
+ - test/test_big_conversions.rb
98
115
  - test/test_digits_definition.rb
116
+ - test/test_exp_setter.rb
99
117
  - test/test_float_conversions.rb
100
118
  - test/test_flt_conversions.rb
119
+ - test/test_format.rb
120
+ - test/test_format_input.rb
121
+ - test/test_format_mode.rb
122
+ - test/test_format_output.rb
101
123
  - test/test_integer_conversions.rb
102
124
  - test/test_numeral.rb
103
125
  - test/test_rational_conversions.rb
126
+ - test/test_repeat_detector.rb
104
127
  - test/test_rounding.rb
128
+ - test/test_symbols.rb
105
129
  homepage: https://github.com/jgoizueta/numerals
106
130
  licenses:
107
131
  - MIT
@@ -129,10 +153,19 @@ summary: Number representation as text.
129
153
  test_files:
130
154
  - test/data.yaml
131
155
  - test/helper.rb
156
+ - test/test_base_scaler.rb
157
+ - test/test_big_conversions.rb
132
158
  - test/test_digits_definition.rb
159
+ - test/test_exp_setter.rb
133
160
  - test/test_float_conversions.rb
134
161
  - test/test_flt_conversions.rb
162
+ - test/test_format.rb
163
+ - test/test_format_input.rb
164
+ - test/test_format_mode.rb
165
+ - test/test_format_output.rb
135
166
  - test/test_integer_conversions.rb
136
167
  - test/test_numeral.rb
137
168
  - test/test_rational_conversions.rb
169
+ - test/test_repeat_detector.rb
138
170
  - test/test_rounding.rb
171
+ - test/test_symbols.rb
@@ -1,75 +0,0 @@
1
- module Numerals
2
-
3
- # Digits definition (symbols used as digits)
4
- class DigitsDefinition
5
- include ModalSupport::StateEquivalent
6
- include ModalSupport::BracketConstructor
7
-
8
- DEFAULT_DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9
- DEFAULT_BASE = 10
10
-
11
- def initialize(*args)
12
- if String === args.first
13
- digits = args.shift
14
- end
15
- options = args.shift || {}
16
- raise NumeralError, "Invalid DigitsDefinition" unless args.empty? && Hash === options
17
- digits ||= options[:digits]
18
- base = options[:base]
19
- if base
20
- if digits
21
- raise NumeralError, "Inconsistent DigitsDefinition" unless digits.size == base
22
- end
23
- elsif digits
24
- base = digits.size
25
- else
26
- base = DEFAULT_BASE
27
- end
28
- digits ||= DEFAULT_DIGITS[0, base]
29
-
30
- @radix = base
31
- @digits = digits
32
- @case_sensitive = options[:case_sensitive]
33
- @downcase = options[:downcase] || (@digits.downcase == @digits)
34
- @digits = @digits.downcase if @downcase
35
- end
36
-
37
- def is_digit?(digit)
38
- digit = set_case(digit)
39
- @digits.include?(digit)
40
- end
41
-
42
- def digit_value(digit)
43
- digit = set_case(digit)
44
- @digits.index(digit)
45
- end
46
-
47
- def digit_char(v)
48
- v >= 0 && v < @radix ? @digits[v] : nil
49
- end
50
-
51
- def radix
52
- @radix
53
- end
54
-
55
- def valid?
56
- @digits.none?{|x| x.nil? || x<0 || x>=@radix}
57
- end
58
-
59
- private
60
-
61
- def set_case(digit_char)
62
- if digit_char
63
- unless @case_sensitive
64
- if @downcase
65
- digit_char = digit_char.downcase
66
- else
67
- digit_char = digit_char.upcase
68
- end
69
- end
70
- end
71
- digit_char
72
- end
73
- end
74
-
75
- end