long-decimal 0.00.16 → 0.00.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. data/README +16 -15
  2. data/Rakefile +1 -1
  3. data/VERSION +1 -1
  4. data/doc/classes/LongDecimal.html +15 -8
  5. data/doc/classes/LongDecimal.src/M000041.html +3 -2
  6. data/doc/classes/LongDecimal.src/M000042.html +1 -1
  7. data/doc/classes/LongDecimal.src/M000043.html +1 -1
  8. data/doc/classes/LongDecimal.src/M000044.html +1 -3
  9. data/doc/classes/LongDecimal.src/M000045.html +1 -1
  10. data/doc/classes/LongDecimal.src/M000046.html +1 -1
  11. data/doc/classes/LongDecimal.src/M000047.html +1 -1
  12. data/doc/classes/LongDecimal.src/M000048.html +1 -1
  13. data/doc/classes/LongDecimal.src/M000049.html +7 -3
  14. data/doc/classes/LongDecimal.src/M000050.html +1 -1
  15. data/doc/classes/LongDecimal.src/M000051.html +1 -1
  16. data/doc/classes/LongDecimal.src/M000052.html +1 -1
  17. data/doc/classes/LongDecimal.src/M000053.html +1 -1
  18. data/doc/classes/LongDecimal.src/M000054.html +1 -1
  19. data/doc/classes/LongDecimal.src/M000055.html +1 -1
  20. data/doc/classes/LongDecimal.src/M000056.html +1 -1
  21. data/doc/classes/LongDecimal.src/M000058.html +1 -1
  22. data/doc/classes/LongDecimal.src/M000059.html +1 -1
  23. data/doc/classes/LongDecimal.src/M000060.html +1 -1
  24. data/doc/classes/LongDecimal.src/M000061.html +1 -1
  25. data/doc/classes/LongDecimal.src/M000062.html +1 -1
  26. data/doc/classes/LongDecimal.src/M000063.html +1 -1
  27. data/doc/classes/LongDecimal.src/M000064.html +1 -1
  28. data/doc/classes/LongDecimal.src/M000065.html +1 -1
  29. data/doc/classes/LongDecimal.src/M000066.html +1 -1
  30. data/doc/classes/LongDecimal.src/M000067.html +1 -1
  31. data/doc/classes/LongDecimal.src/M000068.html +1 -1
  32. data/doc/classes/LongDecimal.src/M000069.html +1 -1
  33. data/doc/classes/LongDecimal.src/M000070.html +1 -1
  34. data/doc/classes/LongDecimal.src/M000071.html +1 -1
  35. data/doc/classes/LongDecimal.src/M000072.html +1 -1
  36. data/doc/classes/LongDecimal.src/M000073.html +1 -1
  37. data/doc/classes/LongDecimal.src/M000074.html +1 -1
  38. data/doc/classes/LongDecimal.src/M000075.html +1 -1
  39. data/doc/classes/LongDecimal.src/M000076.html +1 -1
  40. data/doc/classes/LongDecimal.src/M000077.html +1 -1
  41. data/doc/classes/LongDecimal.src/M000078.html +1 -1
  42. data/doc/classes/LongDecimal.src/M000079.html +1 -1
  43. data/doc/classes/LongDecimal.src/M000080.html +1 -1
  44. data/doc/classes/LongDecimal.src/M000081.html +1 -1
  45. data/doc/classes/LongDecimal.src/M000082.html +1 -1
  46. data/doc/classes/LongDecimal.src/M000083.html +1 -1
  47. data/doc/classes/LongDecimal.src/M000084.html +1 -1
  48. data/doc/classes/LongDecimal.src/M000085.html +1 -1
  49. data/doc/classes/LongDecimal.src/M000086.html +1 -1
  50. data/doc/classes/LongDecimal.src/M000087.html +1 -1
  51. data/doc/classes/LongDecimal.src/M000088.html +1 -1
  52. data/doc/classes/LongDecimal.src/M000089.html +30 -4
  53. data/doc/classes/LongDecimal.src/M000090.html +1 -1
  54. data/doc/classes/LongDecimal.src/M000091.html +1 -1
  55. data/doc/classes/LongDecimal.src/M000094.html +1 -1
  56. data/doc/classes/LongDecimal.src/M000095.html +1 -1
  57. data/doc/classes/LongDecimal.src/M000096.html +1 -1
  58. data/doc/classes/LongDecimal.src/M000097.html +1 -1
  59. data/doc/classes/LongDecimal.src/M000098.html +1 -1
  60. data/doc/classes/LongDecimal.src/M000099.html +1 -1
  61. data/doc/classes/LongDecimalBase.html +58 -57
  62. data/doc/classes/LongDecimalBase.src/M000102.html +4 -4
  63. data/doc/classes/LongDecimalBase.src/M000103.html +4 -4
  64. data/doc/classes/LongDecimalBase.src/M000104.html +4 -4
  65. data/doc/classes/LongDecimalBase.src/M000105.html +4 -4
  66. data/doc/classes/LongDecimalBase.src/M000106.html +4 -4
  67. data/doc/classes/LongDecimalBase.src/M000107.html +18 -0
  68. data/doc/classes/LongDecimalBase.src/M000110.html +9 -5
  69. data/doc/classes/LongDecimalBase.src/M000111.html +5 -4
  70. data/doc/classes/LongDecimalBase.src/M000112.html +18 -0
  71. data/doc/classes/LongDecimalQuot.html +18 -13
  72. data/doc/classes/LongDecimalQuot.src/M000003.html +1 -1
  73. data/doc/classes/LongDecimalQuot.src/M000004.html +1 -1
  74. data/doc/classes/LongDecimalQuot.src/M000005.html +1 -1
  75. data/doc/classes/LongDecimalQuot.src/M000006.html +1 -1
  76. data/doc/classes/LongDecimalQuot.src/M000007.html +1 -1
  77. data/doc/classes/LongDecimalQuot.src/M000008.html +1 -1
  78. data/doc/classes/LongDecimalQuot.src/M000009.html +1 -1
  79. data/doc/classes/LongDecimalQuot.src/M000010.html +1 -1
  80. data/doc/classes/LongDecimalQuot.src/M000011.html +3 -3
  81. data/doc/classes/LongDecimalQuot.src/M000012.html +1 -1
  82. data/doc/classes/LongDecimalQuot.src/M000013.html +1 -1
  83. data/doc/classes/LongDecimalQuot.src/M000014.html +1 -1
  84. data/doc/classes/LongDecimalQuot.src/M000015.html +1 -1
  85. data/doc/classes/LongDecimalQuot.src/M000016.html +1 -1
  86. data/doc/classes/LongDecimalQuot.src/M000017.html +1 -1
  87. data/doc/classes/LongDecimalQuot.src/M000018.html +1 -1
  88. data/doc/classes/LongDecimalQuot.src/M000019.html +1 -1
  89. data/doc/classes/LongDecimalQuot.src/M000020.html +1 -1
  90. data/doc/classes/LongDecimalQuot.src/M000021.html +1 -1
  91. data/doc/classes/LongDecimalQuot.src/M000022.html +1 -1
  92. data/doc/classes/LongDecimalQuot.src/M000023.html +35 -2
  93. data/doc/classes/LongDecimalQuot.src/M000024.html +20 -4
  94. data/doc/classes/LongDecimalQuot.src/M000025.html +1 -1
  95. data/doc/classes/LongDecimalQuot.src/M000026.html +1 -1
  96. data/doc/classes/LongDecimalQuot.src/M000027.html +1 -1
  97. data/doc/classes/LongDecimalQuot.src/M000028.html +1 -1
  98. data/doc/classes/LongDecimalQuot.src/M000029.html +1 -1
  99. data/doc/classes/LongDecimalQuot.src/M000032.html +1 -1
  100. data/doc/classes/LongDecimalQuot.src/M000033.html +1 -1
  101. data/doc/classes/LongDecimalRoundingMode/RoundingModeClass.html +5 -5
  102. data/doc/classes/LongDecimalRoundingMode/RoundingModeClass.src/M000137.html +22 -0
  103. data/doc/classes/LongMath.html +135 -116
  104. data/doc/classes/LongMath.src/M000113.html +5 -4
  105. data/doc/classes/LongMath.src/M000114.html +4 -4
  106. data/doc/classes/LongMath.src/M000115.html +4 -5
  107. data/doc/classes/LongMath.src/M000116.html +5 -4
  108. data/doc/classes/LongMath.src/M000117.html +4 -18
  109. data/doc/classes/LongMath.src/M000118.html +14 -14
  110. data/doc/classes/LongMath.src/M000119.html +18 -5
  111. data/doc/classes/LongMath.src/M000120.html +5 -29
  112. data/doc/classes/LongMath.src/M000121.html +29 -5
  113. data/doc/classes/LongMath.src/M000122.html +5 -56
  114. data/doc/classes/LongMath.src/M000123.html +54 -19
  115. data/doc/classes/LongMath.src/M000124.html +20 -35
  116. data/doc/classes/LongMath.src/M000125.html +31 -27
  117. data/doc/classes/LongMath.src/M000126.html +32 -8
  118. data/doc/classes/LongMath.src/M000127.html +8 -13
  119. data/doc/classes/LongMath.src/M000128.html +13 -82
  120. data/doc/classes/LongMath.src/M000129.html +79 -4
  121. data/doc/classes/LongMath.src/M000130.html +4 -14
  122. data/doc/classes/LongMath.src/M000131.html +4 -14
  123. data/doc/classes/LongMath.src/M000132.html +13 -148
  124. data/doc/classes/LongMath.src/M000133.html +14 -8
  125. data/doc/classes/LongMath.src/M000134.html +127 -3
  126. data/doc/classes/LongMath.src/M000135.html +25 -0
  127. data/doc/classes/LongMath.src/M000136.html +42 -0
  128. data/doc/classes/Numeric.html +5 -2
  129. data/doc/classes/Numeric.src/M000100.html +8 -3
  130. data/doc/classes/Rational.html +158 -0
  131. data/doc/classes/Rational.src/M000101.html +23 -0
  132. data/doc/created.rid +1 -1
  133. data/doc/dot/f_0.dot +9 -0
  134. data/doc/dot/f_0.png +0 -0
  135. data/doc/files/lib/long-decimal_rb.src/M000001.html +1 -1
  136. data/doc/files/lib/long-decimal_rb.src/M000002.html +1 -1
  137. data/doc/fr_class_index.html +1 -0
  138. data/doc/fr_method_index.html +56 -54
  139. data/lib/long-decimal.rb +147 -29
  140. data/test/testlongdecimal.rb +58 -5
  141. data/version.rb +1 -1
  142. metadata +10 -3
  143. data/doc/files/lib/long-decimal_rb.html +0 -187
@@ -0,0 +1,23 @@
1
+ <?xml version="1.0" encoding="iso-8859-1"?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+
6
+ <html>
7
+ <head>
8
+ <title>to_ld (Rational)</title>
9
+ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
10
+ <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
11
+ </head>
12
+ <body class="standalone-code">
13
+ <pre><span class="ruby-comment cmt"># File lib/long-decimal.rb, line 1705</span>
14
+ <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_ld</span>(<span class="ruby-identifier">prec</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">mode</span> = <span class="ruby-constant">LongDecimal</span><span class="ruby-operator">::</span><span class="ruby-constant">ROUND_HALF_UP</span>)
15
+ <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">prec</span>.<span class="ruby-identifier">nil?</span>)
16
+ <span class="ruby-keyword kw">return</span> <span class="ruby-constant">LongDecimal</span>(<span class="ruby-keyword kw">self</span>)
17
+ <span class="ruby-keyword kw">else</span>
18
+ <span class="ruby-identifier">l</span> = <span class="ruby-constant">LongDecimalQuot</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-identifier">prec</span>)
19
+ <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">l</span>.<span class="ruby-identifier">round_to_scale</span>(<span class="ruby-identifier">prec</span>, <span class="ruby-identifier">mode</span>)
20
+ <span class="ruby-keyword kw">end</span>
21
+ <span class="ruby-keyword kw">end</span></pre>
22
+ </body>
23
+ </html>
data/doc/created.rid CHANGED
@@ -1 +1 @@
1
- Fri Mar 24 19:17:46 CET 2006
1
+ Tue Mar 28 00:48:52 CEST 2006
data/doc/dot/f_0.dot CHANGED
@@ -93,6 +93,15 @@ digraph TopLevel {
93
93
  label = "Numeric"
94
94
  ]
95
95
 
96
+ Rational [
97
+ fontcolor = black,
98
+ URL = "classes/Rational.html",
99
+ shape = ellipse,
100
+ color = palegoldenrod,
101
+ style = filled,
102
+ label = "Rational"
103
+ ]
104
+
96
105
  LongDecimalBase [
97
106
  fontcolor = black,
98
107
  URL = "classes/LongDecimalBase.html",
data/doc/dot/f_0.png CHANGED
Binary file
@@ -10,7 +10,7 @@
10
10
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
11
11
  </head>
12
12
  <body class="standalone-code">
13
- <pre><span class="ruby-comment cmt"># File lib/long-decimal.rb, line 1578</span>
13
+ <pre><span class="ruby-comment cmt"># File lib/long-decimal.rb, line 1660</span>
14
14
  <span class="ruby-keyword kw">def</span> <span class="ruby-constant">LongDecimal</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span> = <span class="ruby-value">0</span>)
15
15
  <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">b</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">a</span>.<span class="ruby-identifier">kind_of?</span> <span class="ruby-constant">LongDecimal</span>) <span class="ruby-keyword kw">then</span>
16
16
  <span class="ruby-identifier">a</span>
@@ -10,7 +10,7 @@
10
10
  <link rel="stylesheet" href="../../.././rdoc-style.css" type="text/css" media="screen" />
11
11
  </head>
12
12
  <body class="standalone-code">
13
- <pre><span class="ruby-comment cmt"># File lib/long-decimal.rb, line 1591</span>
13
+ <pre><span class="ruby-comment cmt"># File lib/long-decimal.rb, line 1673</span>
14
14
  <span class="ruby-keyword kw">def</span> <span class="ruby-constant">LongDecimalQuot</span>(<span class="ruby-identifier">first</span>, <span class="ruby-identifier">second</span>)
15
15
  <span class="ruby-constant">LongDecimalQuot</span>.<span class="ruby-identifier">new!</span>(<span class="ruby-identifier">first</span>, <span class="ruby-identifier">second</span>)
16
16
  <span class="ruby-keyword kw">end</span></pre>
@@ -27,6 +27,7 @@
27
27
  <a href="classes/LongDecimalRoundingMode/RoundingModeClass.html">LongDecimalRoundingMode::RoundingModeClass</a><br />
28
28
  <a href="classes/LongMath.html">LongMath</a><br />
29
29
  <a href="classes/Numeric.html">Numeric</a><br />
30
+ <a href="classes/Rational.html">Rational</a><br />
30
31
  </div>
31
32
  </div>
32
33
  </body>
@@ -20,27 +20,27 @@
20
20
  <div id="index">
21
21
  <h1 class="section-bar">Methods</h1>
22
22
  <div id="index-entries">
23
- <a href="classes/LongDecimal.html#M000072">% (LongDecimal)</a><br />
24
23
  <a href="classes/LongDecimalQuot.html#M000021">% (LongDecimalQuot)</a><br />
24
+ <a href="classes/LongDecimal.html#M000072">% (LongDecimal)</a><br />
25
25
  <a href="classes/LongDecimal.html#M000073">& (LongDecimal)</a><br />
26
26
  <a href="classes/LongDecimal.html#M000065">* (LongDecimal)</a><br />
27
27
  <a href="classes/LongDecimalQuot.html#M000017">* (LongDecimalQuot)</a><br />
28
- <a href="classes/LongDecimal.html#M000070">** (LongDecimal)</a><br />
29
28
  <a href="classes/LongDecimalQuot.html#M000019">** (LongDecimalQuot)</a><br />
30
- <a href="classes/LongDecimalQuot.html#M000015">+ (LongDecimalQuot)</a><br />
29
+ <a href="classes/LongDecimal.html#M000070">** (LongDecimal)</a><br />
31
30
  <a href="classes/LongDecimal.html#M000063">+ (LongDecimal)</a><br />
32
- <a href="classes/LongDecimalBase.html#M000104">+@ (LongDecimalBase)</a><br />
31
+ <a href="classes/LongDecimalQuot.html#M000015">+ (LongDecimalQuot)</a><br />
32
+ <a href="classes/LongDecimalBase.html#M000105">+@ (LongDecimalBase)</a><br />
33
33
  <a href="classes/LongDecimal.html#M000064">- (LongDecimal)</a><br />
34
34
  <a href="classes/LongDecimalQuot.html#M000016">- (LongDecimalQuot)</a><br />
35
- <a href="classes/LongDecimal.html#M000062">-@ (LongDecimal)</a><br />
36
35
  <a href="classes/LongDecimalQuot.html#M000014">-@ (LongDecimalQuot)</a><br />
37
- <a href="classes/LongDecimal.html#M000069">/ (LongDecimal)</a><br />
36
+ <a href="classes/LongDecimal.html#M000062">-@ (LongDecimal)</a><br />
38
37
  <a href="classes/LongDecimalQuot.html#M000018">/ (LongDecimalQuot)</a><br />
38
+ <a href="classes/LongDecimal.html#M000069">/ (LongDecimal)</a><br />
39
39
  <a href="classes/LongDecimal.html#M000077"><< (LongDecimal)</a><br />
40
- <a href="classes/LongDecimalBase.html#M000109"><=> (LongDecimalBase)</a><br />
41
- <a href="classes/LongDecimalRoundingMode/RoundingModeClass.html#M000135"><=> (LongDecimalRoundingMode::RoundingModeClass)</a><br />
42
- <a href="classes/LongDecimalQuot.html#M000025">== (LongDecimalQuot)</a><br />
40
+ <a href="classes/LongDecimalBase.html#M000110"><=> (LongDecimalBase)</a><br />
41
+ <a href="classes/LongDecimalRoundingMode/RoundingModeClass.html#M000137"><=> (LongDecimalRoundingMode::RoundingModeClass)</a><br />
43
42
  <a href="classes/LongDecimal.html#M000094">== (LongDecimal)</a><br />
43
+ <a href="classes/LongDecimalQuot.html#M000025">== (LongDecimalQuot)</a><br />
44
44
  <a href="classes/LongDecimal.html#M000095">=== (LongDecimal)</a><br />
45
45
  <a href="classes/LongDecimal.html#M000078">>> (LongDecimal)</a><br />
46
46
  <a href="files/lib/long-decimal_rb.html#M000001">LongDecimal (lib/long-decimal.rb)</a><br />
@@ -49,20 +49,20 @@
49
49
  <a href="classes/LongDecimal.html#M000075">^ (LongDecimal)</a><br />
50
50
  <a href="classes/LongDecimal.html#M000088">abs (LongDecimal)</a><br />
51
51
  <a href="classes/LongDecimalQuot.html#M000022">abs (LongDecimalQuot)</a><br />
52
- <a href="classes/LongDecimalBase.html#M000108">abs2 (LongDecimalBase)</a><br />
52
+ <a href="classes/LongDecimalBase.html#M000109">abs2 (LongDecimalBase)</a><br />
53
53
  <a href="classes/LongDecimal.html#M000055">anti_equalize_scale (LongDecimal)</a><br />
54
- <a href="classes/LongMath.html#M000127">calc_iprec_for_exp (LongMath)</a><br />
55
- <a href="classes/LongMath.html#M000125">calc_pi (LongMath)</a><br />
56
- <a href="classes/LongMath.html#M000113">check_is_int (LongMath)</a><br />
57
- <a href="classes/LongMath.html#M000114">check_is_ld (LongMath)</a><br />
58
- <a href="classes/LongMath.html#M000116">check_is_mode (LongMath)</a><br />
59
- <a href="classes/LongMath.html#M000115">check_is_prec (LongMath)</a><br />
60
- <a href="classes/LongMath.html#M000112">check_word_len (LongMath)</a><br />
61
- <a href="classes/LongDecimalQuot.html#M000024">coerce (LongDecimalQuot)</a><br />
54
+ <a href="classes/LongMath.html#M000128">calc_iprec_for_exp (LongMath)</a><br />
55
+ <a href="classes/LongMath.html#M000126">calc_pi (LongMath)</a><br />
56
+ <a href="classes/LongMath.html#M000114">check_is_int (LongMath)</a><br />
57
+ <a href="classes/LongMath.html#M000115">check_is_ld (LongMath)</a><br />
58
+ <a href="classes/LongMath.html#M000117">check_is_mode (LongMath)</a><br />
59
+ <a href="classes/LongMath.html#M000116">check_is_prec (LongMath)</a><br />
60
+ <a href="classes/LongMath.html#M000113">check_word_len (LongMath)</a><br />
62
61
  <a href="classes/LongDecimal.html#M000089">coerce (LongDecimal)</a><br />
63
- <a href="classes/LongDecimalBase.html#M000103">dec (LongDecimalBase)</a><br />
64
- <a href="classes/LongDecimal.html#M000060">dec! (LongDecimal)</a><br />
62
+ <a href="classes/LongDecimalQuot.html#M000024">coerce (LongDecimalQuot)</a><br />
63
+ <a href="classes/LongDecimalBase.html#M000104">dec (LongDecimalBase)</a><br />
65
64
  <a href="classes/LongDecimalQuot.html#M000013">dec! (LongDecimalQuot)</a><br />
65
+ <a href="classes/LongDecimal.html#M000060">dec! (LongDecimal)</a><br />
66
66
  <a href="classes/LongDecimal.html#M000051">denominator (LongDecimal)</a><br />
67
67
  <a href="classes/LongDecimalQuot.html#M000006">denominator (LongDecimalQuot)</a><br />
68
68
  <a href="classes/LongDecimal.html#M000066">divide (LongDecimal)</a><br />
@@ -70,89 +70,91 @@
70
70
  <a href="classes/LongDecimal.html#M000071">divmod (LongDecimal)</a><br />
71
71
  <a href="classes/LongDecimalQuot.html#M000020">divmod (LongDecimalQuot)</a><br />
72
72
  <a href="classes/LongDecimal.html#M000054">equalize_scale (LongDecimal)</a><br />
73
- <a href="classes/LongMath.html#M000126">exp (LongMath)</a><br />
74
- <a href="classes/LongMath.html#M000128">exp_internal (LongMath)</a><br />
75
- <a href="classes/LongMath.html#M000123">gcd_with_high_power (LongMath)</a><br />
76
- <a href="classes/LongDecimal.html#M000098">hash (LongDecimal)</a><br />
73
+ <a href="classes/LongMath.html#M000127">exp (LongMath)</a><br />
74
+ <a href="classes/LongMath.html#M000129">exp_internal (LongMath)</a><br />
75
+ <a href="classes/LongMath.html#M000124">gcd_with_high_power (LongMath)</a><br />
77
76
  <a href="classes/LongDecimalQuot.html#M000032">hash (LongDecimalQuot)</a><br />
78
- <a href="classes/LongDecimalBase.html#M000102">inc (LongDecimalBase)</a><br />
79
- <a href="classes/LongDecimal.html#M000059">inc! (LongDecimal)</a><br />
77
+ <a href="classes/LongDecimal.html#M000098">hash (LongDecimal)</a><br />
78
+ <a href="classes/LongDecimalBase.html#M000103">inc (LongDecimalBase)</a><br />
80
79
  <a href="classes/LongDecimalQuot.html#M000012">inc! (LongDecimalQuot)</a><br />
81
- <a href="classes/LongDecimalQuot.html#M000033">inspect (LongDecimalQuot)</a><br />
80
+ <a href="classes/LongDecimal.html#M000059">inc! (LongDecimal)</a><br />
82
81
  <a href="classes/LongDecimal.html#M000099">inspect (LongDecimal)</a><br />
82
+ <a href="classes/LongDecimalQuot.html#M000033">inspect (LongDecimalQuot)</a><br />
83
83
  <a href="classes/LongDecimal.html#M000053">int_digits10 (LongDecimal)</a><br />
84
84
  <a href="classes/LongDecimal.html#M000052">int_digits2 (LongDecimal)</a><br />
85
- <a href="classes/LongDecimalBase.html#M000107">inverse (LongDecimalBase)</a><br />
85
+ <a href="classes/LongDecimalBase.html#M000108">inverse (LongDecimalBase)</a><br />
86
86
  <a href="classes/LongDecimalQuot.html#M000028">is_int? (LongDecimalQuot)</a><br />
87
87
  <a href="classes/LongDecimal.html#M000090">is_int? (LongDecimal)</a><br />
88
- <a href="classes/LongMath.html#M000129">log (LongMath)</a><br />
89
- <a href="classes/LongMath.html#M000130">log10 (LongMath)</a><br />
90
- <a href="classes/LongMath.html#M000131">log2 (LongMath)</a><br />
91
- <a href="classes/LongMath.html#M000132">log_internal (LongMath)</a><br />
92
- <a href="classes/LongMath.html#M000118">merge_from_words (LongMath)</a><br />
88
+ <a href="classes/LongMath.html#M000131">log (LongMath)</a><br />
89
+ <a href="classes/LongMath.html#M000132">log10 (LongMath)</a><br />
90
+ <a href="classes/LongMath.html#M000133">log2 (LongMath)</a><br />
91
+ <a href="classes/LongMath.html#M000134">log_internal (LongMath)</a><br />
92
+ <a href="classes/LongMath.html#M000119">merge_from_words (LongMath)</a><br />
93
93
  <a href="classes/LongDecimal.html#M000039">minus_one! (LongDecimal)</a><br />
94
94
  <a href="classes/LongDecimal.html#M000081">move_point_left (LongDecimal)</a><br />
95
95
  <a href="classes/LongDecimal.html#M000083">move_point_left_int (LongDecimal)</a><br />
96
96
  <a href="classes/LongDecimal.html#M000082">move_point_right (LongDecimal)</a><br />
97
97
  <a href="classes/LongDecimal.html#M000084">move_point_right_int (LongDecimal)</a><br />
98
- <a href="classes/LongMath.html#M000124">multiplicity_of_factor (LongMath)</a><br />
99
- <a href="classes/LongDecimalQuot.html#M000004">new (LongDecimalQuot)</a><br />
98
+ <a href="classes/LongMath.html#M000125">multiplicity_of_factor (LongMath)</a><br />
100
99
  <a href="classes/LongDecimal.html#M000041">new (LongDecimal)</a><br />
101
- <a href="classes/LongDecimalQuot.html#M000003">new! (LongDecimalQuot)</a><br />
100
+ <a href="classes/LongDecimalQuot.html#M000004">new (LongDecimalQuot)</a><br />
102
101
  <a href="classes/LongDecimal.html#M000034">new! (LongDecimal)</a><br />
102
+ <a href="classes/LongDecimalQuot.html#M000003">new! (LongDecimalQuot)</a><br />
103
103
  <a href="classes/LongDecimal.html#M000057">next (LongDecimal)</a><br />
104
104
  <a href="classes/LongDecimalQuot.html#M000005">numerator (LongDecimalQuot)</a><br />
105
105
  <a href="classes/LongDecimal.html#M000036">one! (LongDecimal)</a><br />
106
106
  <a href="classes/LongDecimalQuot.html#M000027">one? (LongDecimalQuot)</a><br />
107
107
  <a href="classes/LongDecimal.html#M000097">one? (LongDecimal)</a><br />
108
- <a href="classes/LongMath.html#M000133">power (LongMath)</a><br />
109
- <a href="classes/LongMath.html#M000134">power_internal (LongMath)</a><br />
108
+ <a href="classes/LongMath.html#M000135">power (LongMath)</a><br />
109
+ <a href="classes/LongMath.html#M000136">power_internal (LongMath)</a><br />
110
110
  <a href="classes/LongDecimal.html#M000040">power_of_ten! (LongDecimal)</a><br />
111
111
  <a href="classes/LongDecimal.html#M000058">pred (LongDecimal)</a><br />
112
112
  <a href="classes/LongDecimal.html#M000068">rdiv (LongDecimal)</a><br />
113
- <a href="classes/LongDecimalBase.html#M000106">reciprocal (LongDecimalBase)</a><br />
114
- <a href="classes/LongDecimalQuot.html#M000023">round_to_scale (LongDecimalQuot)</a><br />
113
+ <a href="classes/LongDecimalBase.html#M000107">reciprocal (LongDecimalBase)</a><br />
115
114
  <a href="classes/LongDecimal.html#M000043">round_to_scale (LongDecimal)</a><br />
115
+ <a href="classes/LongDecimalQuot.html#M000023">round_to_scale (LongDecimalQuot)</a><br />
116
116
  <a href="classes/LongDecimal.html#M000042">scale= (LongDecimal)</a><br />
117
- <a href="classes/LongDecimalBase.html#M000111">scale_equal (LongDecimalBase)</a><br />
118
- <a href="classes/LongDecimalBase.html#M000110">scale_ufo (LongDecimalBase)</a><br />
117
+ <a href="classes/LongDecimalBase.html#M000112">scale_equal (LongDecimalBase)</a><br />
118
+ <a href="classes/LongDecimalBase.html#M000111">scale_ufo (LongDecimalBase)</a><br />
119
119
  <a href="classes/LongDecimalQuot.html#M000029">sgn (LongDecimalQuot)</a><br />
120
120
  <a href="classes/LongDecimal.html#M000091">sgn (LongDecimal)</a><br />
121
121
  <a href="classes/LongDecimal.html#M000093">sign (LongDecimal)</a><br />
122
122
  <a href="classes/LongDecimalQuot.html#M000031">sign (LongDecimalQuot)</a><br />
123
- <a href="classes/LongDecimal.html#M000092">signum (LongDecimal)</a><br />
124
123
  <a href="classes/LongDecimalQuot.html#M000030">signum (LongDecimalQuot)</a><br />
124
+ <a href="classes/LongDecimal.html#M000092">signum (LongDecimal)</a><br />
125
125
  <a href="classes/LongDecimal.html#M000080">size (LongDecimal)</a><br />
126
- <a href="classes/LongMath.html#M000117">split_to_words (LongMath)</a><br />
126
+ <a href="classes/LongMath.html#M000118">split_to_words (LongMath)</a><br />
127
127
  <a href="classes/LongDecimal.html#M000086">sqrt (LongDecimal)</a><br />
128
+ <a href="classes/LongMath.html#M000130">sqrt (LongMath)</a><br />
128
129
  <a href="classes/LongDecimal.html#M000087">sqrt_with_remainder (LongDecimal)</a><br />
129
- <a href="classes/LongMath.html#M000119">sqrtb (LongMath)</a><br />
130
- <a href="classes/LongMath.html#M000120">sqrtb_with_remainder (LongMath)</a><br />
131
- <a href="classes/LongMath.html#M000121">sqrtw (LongMath)</a><br />
132
- <a href="classes/LongMath.html#M000122">sqrtw_with_remainder (LongMath)</a><br />
133
- <a href="classes/LongDecimalBase.html#M000105">square (LongDecimalBase)</a><br />
130
+ <a href="classes/LongMath.html#M000120">sqrtb (LongMath)</a><br />
131
+ <a href="classes/LongMath.html#M000121">sqrtb_with_remainder (LongMath)</a><br />
132
+ <a href="classes/LongMath.html#M000122">sqrtw (LongMath)</a><br />
133
+ <a href="classes/LongMath.html#M000123">sqrtw_with_remainder (LongMath)</a><br />
134
134
  <a href="classes/LongDecimal.html#M000085">square (LongDecimal)</a><br />
135
+ <a href="classes/LongDecimalBase.html#M000106">square (LongDecimalBase)</a><br />
135
136
  <a href="classes/LongDecimal.html#M000056">succ (LongDecimal)</a><br />
136
137
  <a href="classes/LongDecimal.html#M000038">ten! (LongDecimal)</a><br />
137
138
  <a href="classes/LongDecimalQuot.html#M000009">to_bd (LongDecimalQuot)</a><br />
138
139
  <a href="classes/LongDecimal.html#M000050">to_bd (LongDecimal)</a><br />
139
- <a href="classes/LongDecimalQuot.html#M000008">to_f (LongDecimalQuot)</a><br />
140
140
  <a href="classes/LongDecimal.html#M000047">to_f (LongDecimal)</a><br />
141
+ <a href="classes/LongDecimalQuot.html#M000008">to_f (LongDecimalQuot)</a><br />
141
142
  <a href="classes/LongDecimalQuot.html#M000010">to_i (LongDecimalQuot)</a><br />
142
143
  <a href="classes/LongDecimal.html#M000048">to_i (LongDecimal)</a><br />
143
- <a href="classes/LongDecimalQuot.html#M000011">to_ld (LongDecimalQuot)</a><br />
144
144
  <a href="classes/LongDecimal.html#M000049">to_ld (LongDecimal)</a><br />
145
145
  <a href="classes/Numeric.html#M000100">to_ld (Numeric)</a><br />
146
- <a href="classes/LongDecimalBase.html#M000101">to_r (LongDecimalBase)</a><br />
147
- <a href="classes/LongDecimalQuot.html#M000007">to_s (LongDecimalQuot)</a><br />
146
+ <a href="classes/LongDecimalQuot.html#M000011">to_ld (LongDecimalQuot)</a><br />
147
+ <a href="classes/Rational.html#M000101">to_ld (Rational)</a><br />
148
+ <a href="classes/LongDecimalBase.html#M000102">to_r (LongDecimalBase)</a><br />
148
149
  <a href="classes/LongDecimal.html#M000044">to_s (LongDecimal)</a><br />
150
+ <a href="classes/LongDecimalQuot.html#M000007">to_s (LongDecimalQuot)</a><br />
149
151
  <a href="classes/LongDecimal.html#M000045">to_s_10 (LongDecimal)</a><br />
150
152
  <a href="classes/LongDecimal.html#M000046">to_s_internal (LongDecimal)</a><br />
151
153
  <a href="classes/LongDecimal.html#M000037">two! (LongDecimal)</a><br />
152
154
  <a href="classes/LongDecimal.html#M000061">unit (LongDecimal)</a><br />
153
155
  <a href="classes/LongDecimal.html#M000035">zero! (LongDecimal)</a><br />
154
- <a href="classes/LongDecimal.html#M000096">zero? (LongDecimal)</a><br />
155
156
  <a href="classes/LongDecimalQuot.html#M000026">zero? (LongDecimalQuot)</a><br />
157
+ <a href="classes/LongDecimal.html#M000096">zero? (LongDecimal)</a><br />
156
158
  <a href="classes/LongDecimal.html#M000074">| (LongDecimal)</a><br />
157
159
  <a href="classes/LongDecimal.html#M000076">~ (LongDecimal)</a><br />
158
160
  </div>
data/lib/long-decimal.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  #
2
2
  # long-decimal.rb -- Arbitrary precision decimals with fixed decimal point
3
3
  #
4
- # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/lib/long-decimal.rb,v 1.9 2006/03/24 17:42:07 bk1 Exp $
5
- # CVS-Label: $Name: PRE_ALPHA_0_16 $
4
+ # CVS-ID: $Header: /var/cvs/long-decimal/long-decimal/lib/long-decimal.rb,v 1.14 2006/03/27 22:19:18 bk1 Exp $
5
+ # CVS-Label: $Name: PRE_ALPHA_0_17 $
6
6
  # Author: $Author: bk1 $ (Karl Brodowsky)
7
7
  #
8
8
  require "complex"
@@ -56,13 +56,13 @@ end # LongDecimalRoundingMode
56
56
  # common base class for LongDecimal and LongDecimalQuot
57
57
  #
58
58
  class LongDecimalBase < Numeric
59
- @RCS_ID='-$Id: long-decimal.rb,v 1.9 2006/03/24 17:42:07 bk1 Exp $-'
59
+ @RCS_ID='-$Id: long-decimal.rb,v 1.14 2006/03/27 22:19:18 bk1 Exp $-'
60
60
 
61
61
  include LongDecimalRoundingMode
62
62
 
63
63
  #
64
64
  # convert self into Rational
65
- # this works quite straitforward.
65
+ # this works quite straitforward.
66
66
  # in case of LongDecimal use int_val as numerator and a power of 10
67
67
  # as denominator, which happens to be the way numerator and
68
68
  # denominator are defined
@@ -151,7 +151,7 @@ class LongDecimalBase < Numeric
151
151
  scale_ufo(other).zero?
152
152
  end
153
153
 
154
- end
154
+ end # class LongDecimalBase
155
155
 
156
156
  #
157
157
  # class for holding fixed point long decimal numbers
@@ -159,7 +159,7 @@ end
159
159
  # digits and the other one the position of the decimal point.
160
160
  #
161
161
  class LongDecimal < LongDecimalBase
162
- @RCS_ID='-$Id: long-decimal.rb,v 1.9 2006/03/24 17:42:07 bk1 Exp $-'
162
+ @RCS_ID='-$Id: long-decimal.rb,v 1.14 2006/03/27 22:19:18 bk1 Exp $-'
163
163
 
164
164
  # MINUS_ONE = LongDecimal(-1)
165
165
  # ZERO = LongDecimal(0)
@@ -276,7 +276,6 @@ class LongDecimal < LongDecimalBase
276
276
  denom /= 5 ** mul_5
277
277
  iscale2 = Math.log10(denom).ceil
278
278
  scale += iscale2
279
- # int_val = (x * 10 ** scale).to_i
280
279
  int_val = (x * 10 ** (iscale2+iscale)).to_i
281
280
 
282
281
  else
@@ -321,6 +320,8 @@ class LongDecimal < LongDecimalBase
321
320
  num_frac = ""
322
321
  end
323
322
 
323
+ # handle optional e-part of floating point number represented as
324
+ # string
324
325
  if num_exp.nil? || num_exp.empty? then
325
326
  num_exp = "0"
326
327
  end
@@ -332,7 +333,7 @@ class LongDecimal < LongDecimalBase
332
333
  int_val = -int_val
333
334
  end
334
335
  end
335
- @scale = scale
336
+ @scale = scale
336
337
  @int_val = int_val
337
338
 
338
339
  end # initialize
@@ -405,7 +406,7 @@ class LongDecimal < LongDecimalBase
405
406
  #
406
407
  # convert self into String, which is the decimal representation.
407
408
  # Use trailing zeros, if int_val has them.
408
-
409
+ #
409
410
  # optional parameter shown_scale is the number of digits after the
410
411
  # decimal point. Defaults to the scale of self.
411
412
  # optional parameter mode ist the rounding mode to be applied.
@@ -428,9 +429,7 @@ class LongDecimal < LongDecimalBase
428
429
  raise TypeError, "base must be integer between 2 and 36"
429
430
  end
430
431
  quot = (self.move_point_right(scale) * base ** shown_scale) / 10 ** scale
431
- # p(quot)
432
432
  rounded = quot.round_to_scale(0, mode)
433
- # p(rounded)
434
433
  rounded.to_s_internal(base, shown_scale)
435
434
  end
436
435
  end
@@ -507,9 +506,15 @@ class LongDecimal < LongDecimalBase
507
506
 
508
507
  #
509
508
  # convert self into LongDecimal (returns self)
509
+ # optional first argument gives the precision for the desired result
510
+ # optional second argument gives the rouding mode
510
511
  #
511
- def to_ld
512
- self
512
+ def to_ld(prec = nil, mode = LongDecimal::ROUND_HALF_UP)
513
+ if (prec.nil?)
514
+ return self
515
+ else
516
+ return round_to_scale(prec, mode)
517
+ end
513
518
  end
514
519
 
515
520
  #
@@ -1054,34 +1059,60 @@ class LongDecimal < LongDecimalBase
1054
1059
  # arithmetic operations.
1055
1060
  #
1056
1061
  def coerce(other)
1062
+
1057
1063
  if other.kind_of? LongDecimal then
1064
+ # if other is LongDecimal as well, nothing to do
1058
1065
  return other, self
1066
+
1059
1067
  elsif other.kind_of? LongDecimalQuot then
1068
+ # if other is LongDecimalQuot, convert self to LongDecimalQuot
1069
+ # as well
1060
1070
  return other, LongDecimalQuot(self.to_r, scale)
1071
+
1061
1072
  elsif other.kind_of? Rational then
1073
+ # if other is Rational, convert self and other to
1074
+ # LongDecimalQuot. This is well adapted to cover both.
1062
1075
  sc = scale
1063
1076
  o = LongDecimalQuot(other, sc)
1064
1077
  s = LongDecimalQuot(self.to_r, sc)
1065
1078
  return o, s
1079
+
1080
+ # we could use BigDecimal as common type for combining Float and
1081
+ # LongDecimal, but this needs a lot of consideration. For the
1082
+ # time being we assume that we live well enough with converting
1083
+ # Float into LongDecimal
1084
+ # elsif (other.kind_of? Float) && size > 8 then
1085
+ # return coerce(BigDecimal(other.to_s))
1086
+
1066
1087
  elsif (other.kind_of? Integer) || (other.kind_of? Float) then
1088
+ # if other is Integer or Float, convert it to LongDecimal
1067
1089
  other = LongDecimal(other)
1068
1090
  if (other.scale > scale) then
1069
1091
  other = other.round_to_scale(scale, ROUND_HALF_UP)
1070
1092
  end
1071
1093
  return other, self
1094
+
1072
1095
  elsif other.kind_of? BigDecimal then
1096
+ # if other is BigDecimal convert self to BigDecimal
1073
1097
  s, o = other.coerce(self.to_bd)
1074
1098
  return o, s
1099
+
1075
1100
  elsif other.kind_of? Complex then
1076
- # s, o = other.coerce(Complex(self.to_bd, 0))
1101
+ # if other is Complex, convert self to Float and then to
1102
+ # Complex. It need to be observed that this will fail if self
1103
+ # has too many digits before the decimal point to be expressed
1104
+ # as Float.
1077
1105
  s, o = other.coerce(Complex(self.to_f, 0))
1078
1106
  return o, s
1079
- elsif (other.kind_of? Float) && size > 8 then
1080
- return coerce(BigDecimal(other.to_s))
1107
+
1081
1108
  elsif other.kind_of? Numeric then
1109
+ # all other go by expressing self as Float and seeing how it
1110
+ # combines with other.
1082
1111
  s, o = other.coerce(self.to_f)
1083
1112
  return o, s
1113
+
1084
1114
  else
1115
+ # non-numeric types do not work here
1085
1116
  raise TypeError, "unsupported type #{other.inspect} for coerce of LongDecimal"
1086
1117
  end
1087
1118
  end
@@ -1164,7 +1195,7 @@ end # LongDecimal
1164
1195
  #
1165
1196
  class LongDecimalQuot < LongDecimalBase
1166
1197
 
1167
- @RCS_ID='-$Id: long-decimal.rb,v 1.9 2006/03/24 17:42:07 bk1 Exp $-'
1198
+ @RCS_ID='-$Id: long-decimal.rb,v 1.14 2006/03/27 22:19:18 bk1 Exp $-'
1168
1199
 
1169
1200
  #
1170
1201
  # constructor
@@ -1241,7 +1272,7 @@ class LongDecimalQuot < LongDecimalBase
1241
1272
  to_r.to_f
1242
1273
  end
1243
1274
 
1244
- #
1275
+ #
1245
1276
  # conversion to BigDecimal
1246
1277
  #
1247
1278
  def to_bd
@@ -1257,9 +1288,11 @@ class LongDecimalQuot < LongDecimalBase
1257
1288
 
1258
1289
  #
1259
1290
  # conversion to LongDecimal using the internal scale
1291
+ # optional first argument gives the precision for the desired result
1292
+ # optional second argument gives the rouding mode
1260
1293
  #
1261
- def to_ld
1262
- round_to_scale(scale, ROUND_HALF_UP)
1294
+ def to_ld(prec = scale, mode = LongDecimal::ROUND_HALF_UP)
1295
+ round_to_scale(prec, mode)
1263
1296
  end
1264
1297
 
1265
1298
  #
@@ -1421,20 +1454,31 @@ class LongDecimalQuot < LongDecimalBase
1421
1454
  raise TypeError, "new_scale #{new_scale.inspect} must be >= 0" unless new_scale >= 0
1422
1455
  raise TypeError, "mode #{mode.inspect} must be legal rounding mode" unless mode.kind_of? RoundingModeClass
1423
1456
 
1424
- factor = 10**new_scale
1425
1457
  sign_quot = numerator <=> 0
1426
1458
  if sign_quot == 0 then
1459
+ # finish zero without long calculations at once
1427
1460
  return LongDecimal(0, new_scale)
1428
1461
  end
1462
+
1463
+ factor = 10**new_scale
1429
1464
  prod = numerator * factor
1430
1465
  divisor = denominator
1431
1466
  quot, rem = prod.divmod(divisor)
1432
1467
  sign_rem = rem <=> 0
1433
1468
  if (sign_rem == 0)
1469
+ # if self can be expressed without loss as LongDecimal with
1470
+ # new_scale digits after the decimal point, just do it.
1434
1471
  return LongDecimal(quot, new_scale)
1435
1472
  end
1473
+
1474
+ # we do not expect negative signs of remainder. To make sure that
1475
+ # this does not cause problems in further code, we just throw an
1476
+ # exception. This should never happen (and did not happen during
1477
+ # testing).
1436
1478
  raise Error, "signs do not match self=#{self.to_s} f=#{factor} prod=#{prod} divisor=#{divisor} quot=#{quot} rem=#{rem}" if sign_rem <= 0
1479
+
1437
1480
  if (sign_quot < 0) then
1481
+ # handle negative sign of self
1438
1482
  rem -= divisor
1439
1483
  quot += 1
1440
1484
  sign_rem = rem <=> 0
@@ -1442,14 +1486,30 @@ class LongDecimalQuot < LongDecimalBase
1442
1486
  end
1443
1487
 
1444
1488
  if mode == ROUND_UNNECESSARY then
1489
+ # this mode means that rounding should not be necessary. But
1490
+ # the case that no rounding is needed, has already been covered
1491
+ # above, so it is an error, if this mode is required and the
1492
+ # result could not be returned above.
1445
1493
  raise ArgumentError, "mode ROUND_UNNECESSARY not applicable, remainder #{rem.to_s} is not zero"
1446
1494
  end
1447
1495
 
1448
1496
  if (mode == ROUND_CEILING)
1497
+ # ROUND_CEILING goes to the closest allowed number >= self, even
1498
+ # for negative numbers. Since sign is handled separately, it is
1499
+ # more conveniant to use ROUND_UP or ROUND_DOWN depending on the
1500
+ # sign.
1449
1501
  mode = (sign_quot > 0) ? ROUND_UP : ROUND_DOWN
1502
+
1450
1503
  elsif (mode == ROUND_FLOOR)
1504
+ # ROUND_FLOOR goes to the closest allowed number <= self, even
1505
+ # for negative numbers. Since sign is handled separately, it is
1506
+ # more conveniant to use ROUND_UP or ROUND_DOWN depending on the
1507
+ # sign.
1451
1508
  mode = (sign_quot < 0) ? ROUND_UP : ROUND_DOWN
1509
+
1452
1510
  else
1511
+ # handle the ROUND_HALF_... stuff and find the adequate ROUND_UP
1512
+ # or ROUND_DOWN to use
1453
1513
  abs_rem = rem.abs
1454
1514
  half = (abs_rem << 1) <=> denominator
1455
1515
  if (mode == ROUND_HALF_UP || mode == ROUND_HALF_DOWN || mode == ROUND_HALF_EVEN) then
@@ -1472,11 +1532,17 @@ class LongDecimalQuot < LongDecimalBase
1472
1532
  end
1473
1533
 
1474
1534
  if mode == ROUND_UP
1535
+ # since the case where we can express the result exactly without
1536
+ # loss has already been handled above, ROUND_UP can be handled
1537
+ # correctly by adding one unit.
1475
1538
  quot += sign_quot
1476
1539
  end
1540
+
1541
+ # put together result
1477
1542
  new_int_val = quot
1478
1543
  LongDecimal(new_int_val, new_scale)
1479
- end
1544
+
1545
+ end # round_to_scale
1480
1546
 
1481
1547
  #
1482
1548
  # prepare binary operation of other with LongDecimalQuot
@@ -1488,26 +1554,42 @@ class LongDecimalQuot < LongDecimalBase
1488
1554
  # to BigDecimal or Float.
1489
1555
  #
1490
1556
  def coerce(other)
1557
+
1491
1558
  if other.kind_of? LongDecimal then
1559
+ # convert LongDecimal to LongDecimalQuot
1492
1560
  return LongDecimalQuot(other.to_r, other.scale), self
1561
+
1493
1562
  elsif other.kind_of? LongDecimalQuot then
1563
+ # nothing to convert, if both are already LongDecimalQuot
1494
1564
  return other, self
1495
- elsif other.kind_of? Rational then
1565
+
1566
+ elsif (other.kind_of? Rational) || (other.kind_of? Integer) then
1567
+ # convert Rational or Integer to LongDecimalQuot. The only
1568
+ # missing part, scale, is just taken from self
1496
1569
  s = scale
1497
1570
  return LongDecimalQuot(other, s), self
1498
- elsif (other.kind_of? Integer) then
1499
- return LongDecimalQuot(other.to_r, scale), self
1571
+
1500
1572
  elsif other.kind_of? Float then
1573
+ # convert Float to LongDecimalQuot via LongDecimal
1501
1574
  return LongDecimalQuot(other.to_ld.to_r, scale), self
1575
+
1502
1576
  elsif other.kind_of? BigDecimal then
1577
+ # for BigDecimal, convert self to BigDecimal as well
1503
1578
  s, o = other.coerce(self.to_bd)
1579
+
1504
1580
  elsif other.kind_of? Numeric then
1581
+ # for all other numeric types convert self to Float. This may
1582
+ # not work, if numerator and denominator have too many digits to
1583
+ # be expressed as Float and it may cause loss of information.
1505
1584
  s, o = other.coerce(self.to_f)
1506
1585
  return o, s
1586
+
1507
1587
  else
1588
+ # non-numeric types do not work at all
1508
1589
  raise TypeError, "unsupported type #{other.inspect} for coerce of LongDecimalQuot"
1509
1590
  end
1510
- end
1591
+
1592
+ end # coerce
1511
1593
 
1512
1594
  #
1513
1595
  # compare two numbers for equality.
@@ -1597,13 +1679,40 @@ class Numeric
1597
1679
 
1598
1680
  #
1599
1681
  # convert self to LongDecimal
1682
+ # optional first argument gives the precision for the desired result
1683
+ # optional second argument gives the rouding mode
1600
1684
  #
1601
- def to_ld
1602
- LongDecimal(self)
1685
+ def to_ld(prec = nil, mode = LongDecimal::ROUND_HALF_UP)
1686
+ l = LongDecimal(self)
1687
+ if (prec.nil?)
1688
+ return l
1689
+ else
1690
+ return l.round_to_scale(prec, mode)
1691
+ end
1603
1692
  end
1604
1693
 
1605
1694
  end # Numeric
1606
1695
 
1696
+ class Rational
1697
+
1698
+ #
1699
+ # convert self to LongDecimal.
1700
+ # Special handling of Rational to avoid loosing information in the
1701
+ # first step that would be needed for the second step
1702
+ # optional first argument gives the precision for the desired result
1703
+ # optional second argument gives the rouding mode
1704
+ #
1705
+ def to_ld(prec = nil, mode = LongDecimal::ROUND_HALF_UP)
1706
+ if (prec.nil?)
1707
+ return LongDecimal(self)
1708
+ else
1709
+ l = LongDecimalQuot(self, prec)
1710
+ return l.round_to_scale(prec, mode)
1711
+ end
1712
+ end
1713
+
1714
+ end # Rational
1715
+
1607
1716
  #
1608
1717
  # LongMath provides some helper functions to support LongDecimal and
1609
1718
  # LongDecimalQuot, mostly operating on integers. They are used
@@ -1706,7 +1815,6 @@ module LongMath
1706
1815
  end
1707
1816
 
1708
1817
  #
1709
-
1710
1818
  # calculate the square root of an integer x using bitwise algorithm
1711
1819
  # the result is rounded to an integer y such that
1712
1820
  # y**2�<=�x�<�(y+1)**2
@@ -2067,6 +2175,16 @@ module LongMath
2067
2175
  y
2068
2176
  end
2069
2177
 
2178
+ #
2179
+ # calculate approximation of sqrt of a LongDecimal.
2180
+ #
2181
+ def LongMath.sqrt(x, prec, rounding_mode)
2182
+ check_is_ld(x, "x")
2183
+ check_is_prec(prec, "prec")
2184
+ check_is_mode(mode, "mode")
2185
+ x.sqrt(prec, mode)
2186
+ end
2187
+
2070
2188
  #
2071
2189
  # calculate the natural logarithm function of x to the given precision as
2072
2190
  # LongDecimal.