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,43 +5,46 @@ include Numerals
5
5
 
6
6
  class TestFloatConversions < Test::Unit::TestCase # < Minitest::Test
7
7
 
8
- def test_special
9
- assert_equal Numeral.nan, Conversions.number_to_numeral(Float.context.nan)
10
- assert_equal Numeral.nan, Conversions.number_to_numeral(Float.context.nan, :fixed, Rounding[:exact, base: 2])
11
- assert_equal Numeral.nan, Conversions.number_to_numeral(Float.context.nan, :fixed, Rounding[:exact, base: 10])
12
- assert_equal Numeral.nan, Conversions.number_to_numeral(Float.context.nan, :fixed, Rounding[precision: 10, base: 10])
13
- assert_equal Numeral.nan, Conversions.number_to_numeral(Float.context.nan, :free)
14
-
15
- assert_equal Numeral.infinity, Conversions.number_to_numeral(Float.context.infinity)
16
- assert_equal Numeral.infinity, Conversions.number_to_numeral(Float.context.infinity, :fixed, Rounding[:exact, base: 2])
17
- assert_equal Numeral.infinity, Conversions.number_to_numeral(Float.context.infinity, :fixed, Rounding[:exact, base: 10])
18
- assert_equal Numeral.infinity, Conversions.number_to_numeral(Float.context.infinity, :fixed, Rounding[precision: 10, base: 10])
19
- assert_equal Numeral.infinity, Conversions.number_to_numeral(Float.context.infinity, :free)
20
-
21
- assert_equal Numeral.infinity(-1), Conversions.number_to_numeral(Float.context.infinity(-1))
22
- assert_equal Numeral.infinity(-1), Conversions.number_to_numeral(Float.context.infinity(-1), :fixed, Rounding[:exact, base: 2])
23
- assert_equal Numeral.infinity(-1), Conversions.number_to_numeral(Float.context.infinity(-1), :fixed, Rounding[:exact, base: 10])
24
- assert_equal Numeral.infinity(-1), Conversions.number_to_numeral(Float.context.infinity(-1), :fixed, Rounding[precision: 10, base: 10])
25
- assert_equal Numeral.infinity(-1), Conversions.number_to_numeral(Float.context.infinity(-1), :free)
26
-
27
- assert Conversions.numeral_to_number(Numeral.nan, Float).nan?
28
- assert_equal Float.context.infinity, Conversions.numeral_to_number(Numeral.infinity, Float)
29
- assert_equal Float.context.infinity(-1), Conversions.numeral_to_number(Numeral.infinity(-1), Float)
8
+
9
+ def test_write_special
10
+ assert_equal Numeral.nan, Conversions.write(Float.context.nan)
11
+ assert_equal Numeral.nan, Conversions.write(Float.context.nan, rounding: Rounding[:short, base: 2])
12
+ assert_equal Numeral.nan, Conversions.write(Float.context.nan, rounding: Rounding[:short, base: 10])
13
+ assert_equal Numeral.nan, Conversions.write(Float.context.nan, rounding: Rounding[precision: 10, base: 10])
14
+ assert_equal Numeral.nan, Conversions.write(Float.context.nan)
15
+
16
+ assert_equal Numeral.infinity, Conversions.write(Float.context.infinity)
17
+ assert_equal Numeral.infinity, Conversions.write(Float.context.infinity, rounding: Rounding[:short, base: 2])
18
+ assert_equal Numeral.infinity, Conversions.write(Float.context.infinity, rounding: Rounding[:short, base: 10])
19
+ assert_equal Numeral.infinity, Conversions.write(Float.context.infinity, rounding: Rounding[precision: 10, base: 10])
20
+ assert_equal Numeral.infinity, Conversions.write(Float.context.infinity)
21
+
22
+ assert_equal Numeral.infinity(-1), Conversions.write(Float.context.infinity(-1))
23
+ assert_equal Numeral.infinity(-1), Conversions.write(Float.context.infinity(-1), rounding: Rounding[:short, base: 2])
24
+ assert_equal Numeral.infinity(-1), Conversions.write(Float.context.infinity(-1), rounding: Rounding[:short, base: 10])
25
+ assert_equal Numeral.infinity(-1), Conversions.write(Float.context.infinity(-1), rounding: Rounding[precision: 10, base: 10])
26
+ assert_equal Numeral.infinity(-1), Conversions.write(Float.context.infinity(-1))
27
+ end
28
+
29
+ def test_read_special
30
+ assert Conversions.read(Numeral.nan, type: Float).nan?
31
+ assert_equal Float.context.infinity, Conversions.read(Numeral.infinity, type: Float)
32
+ assert_equal Float.context.infinity(-1), Conversions.read(Numeral.infinity(-1), type: Float)
30
33
  end
31
34
 
32
- def test_exact
33
- assert_equal Numeral[1,point:1], Conversions.number_to_numeral(1.0, :fixed, Rounding[:exact, base: 10])
34
- assert_equal Numeral[1,point:1, sign: -1], Conversions.number_to_numeral(-1.0, :fixed, Rounding[:exact, base: 10])
35
+ def test_write_read_exact
36
+ assert_equal Numeral[1,point:1], Conversions.write(1.0, rounding: Rounding[:short, base: 10])
37
+ assert_equal Numeral[1,point:1, sign: -1], Conversions.write(-1.0, rounding: Rounding[:short, base: 10])
35
38
 
36
- assert_equal Numeral[1,point:1, base: 2], Conversions.number_to_numeral(1.0, :fixed, Rounding[:exact, base: 2])
37
- assert_equal Numeral[1,point:1, sign: -1, base: 2], Conversions.number_to_numeral(-1.0, :fixed, Rounding[:exact, base: 2])
39
+ assert_equal Numeral[1,point:1, base: 2], Conversions.write(1.0, rounding: Rounding[:short, base: 2])
40
+ assert_equal Numeral[1,point:1, sign: -1, base: 2], Conversions.write(-1.0, rounding: Rounding[:short, base: 2])
38
41
 
39
42
  [0.1, 0.01, 0.001, 1/3.0, 10/3.0, 100/3.0, Math::PI, 0.5, 123.0, 123.45, 1.23E32, 1.23E-32].each do |x|
40
43
  [x, -x].each do |y|
41
44
  numeral = exact_decimal(y)
42
- tmp = Conversions.number_to_numeral(y, :fixed, Rounding[:exact, base: 10])
43
- assert_equal numeral, Conversions.number_to_numeral(y, :fixed, Rounding[:exact, base: 10]), "#{y} to base 10 exact numeral"
44
- assert_equal y, Conversions.numeral_to_number(numeral, Float), "#{x} base 10 numeral to float"
45
+ tmp = Conversions.write(y, rounding: Rounding[:short, base: 10])
46
+ assert_equal numeral, Conversions.write(y, exact: true, rounding: Rounding[:short, base: 10]), "#{y} to base 10 exact numeral"
47
+ assert_equal y, Conversions.read(numeral, type: Float), "#{x} base 10 numeral to float"
45
48
  end
46
49
  end
47
50
  end
@@ -55,4 +58,19 @@ class TestFloatConversions < Test::Unit::TestCase # < Minitest::Test
55
58
  }
56
59
  end
57
60
 
61
+ def test_type_parameters
62
+ c = Conversions[Float, input_rounding: :down]
63
+ assert_equal Rounding[:down, base: Float::RADIX], c.input_rounding
64
+ c = Conversions[Float, input_rounding: :half_even]
65
+ assert_equal Rounding[:half_even, base: Float::RADIX], c.input_rounding
66
+
67
+ c = Conversions[Float, input_rounding: Rounding[:half_up, precision: 7, base: 3]]
68
+ assert_equal Rounding[:half_up, base: Float::RADIX], c.input_rounding
69
+
70
+ c = Conversions[Float.context, input_rounding: :context]
71
+ assert_equal Float.context.radix, c.input_rounding.base
72
+ assert_equal Float.context.rounding, c.input_rounding.mode
73
+ assert_equal Float.context.precision, c.input_rounding.precision
74
+ end
75
+
58
76
  end