Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make a "working" repeating decimal representation of a rational number?
    primarykey
    data
    text
    <p>I've figured out how to display the repeating part of a repeating decimal using OverBar.</p> <p><code>repeatingDecimal</code> doesn't actually work as a repeating decimal. I'd like to make a variation of it that looks <em>and behaves</em> like a repeating decimal.</p> <hr> <h3>Question</h3> <p>How could I make a <em>working</em> repeating decimal representation (possibly using <code>Interpretation[]</code>)?</p> <hr> <h3>Background</h3> <p>Please excuse me if I ramble. This is my first question and I wanted to be clear about what I have in mind.</p> <p>The following will "draw" a repeating decimal.</p> <pre><code>repeatingDecimal[q2_] := Module[{a}, a[{{nr__Integer}, pt_}] := StringJoin[ Map[ToString, If[pt &gt; -1, Insert[{nr}, ".", pt + 1], Join[{"."}, Table["0", {Abs[pt]}], {nr}]]]]; (* repeating only *) a[{{{r__Integer}}, pt_}] := Row[{".", OverBar@StringJoin[Map[ToString, {r}]]}]; (* One or more non-repeating; more than one repeating digit KEEP IN THIS ORDER!! *) a[{{nr__, {r__}}, pt_}] := Row[{StringJoin[ Map[ToString, If[pt &gt; -1, Insert[{nr}, ".", pt + 1], Join[{"."}, Table["0", {Abs[pt]}], {nr}]]]], OverBar@StringJoin[Map[ToString, {r}]]}]; (* One or more non-repeating; one repeating digit *) a[{{nr__, r_Integer}, pt_}] := Row[{StringJoin[Map[ToString, {nr}]], ".", OverBar@StringJoin[Map[ToString, r]]}]; a[RealDigits[q2]]] </code></pre> <p>So</p> <pre><code>repeatingDecimal[7/31] </code></pre> <p><strong>displays</strong> a repeating decimal properly (shown here as a picture so that the OverBar appears).</p> <p><img src="https://i.stack.imgur.com/sQJik.png" alt="Repeating decimal displayed"></p> <p>Looking under the hood, it's really just an imposter, an image of a repeating decimal ...</p> <pre><code>In[]:= repeatingDecimal[7/31]//FullForm Out[]:= Row[List[".",OverBar["225806451612903"]]] </code></pre> <p>Of course, it doesn't behave like a number:</p> <pre><code>% + 24/31 </code></pre> <p><img src="https://i.stack.imgur.com/fVznm.png" alt="fraction plus repeating decimal"></p> <p>I'd like the addition to yield: 1</p> <hr> <h3>Edit: A cleaned up version of repeatingDecimal</h3> <p>Leonid showed how to wrap Format around the routine and to supply up-values for adding and multiplying repeated decimals. Very helpful! It will take some time for me to be comfortable with up and down values.</p> <p>What follows below is essentially the streamlined version of the code suggested by Mr.Wizard. I set the OverBar above each repeating digit to allow line-breaking. (A single OverBar above Row looks tidier but cannot break when the right screen-margin is reached.) </p> <pre><code>ClearAll[repeatingDecimal] repeatingDecimal[n_Integer | n_Real] := n Format[repeatingDecimal[q_Rational]] := Row @ Flatten[ {IntegerPart@q, ".", RealDigits@FractionalPart@q} /. {{nr___Integer, r_List: {}}, pt_} :&gt; {Table[0, {-pt}], nr, OverBar /@ r} ] repeatingDecimal[q_] + x_ ^:= q + x repeatingDecimal[q_] * x_ ^:= q * x repeatingDecimal[q_] ^ x_ ^:= q ^ x </code></pre> <p>The table below shows some output from <code>repeatingDecimal</code>:</p> <pre><code>n1 = 1; n2 = 15; ClearAll[i, k, r]; TableForm[Table[repeatingDecimal[i/j], {i, n1, n2}, {j, n1, n2}], TableHeadings -&gt; {None, Table[("r")/k, {k, n1, n2}]}] </code></pre> <p><img src="https://i.stack.imgur.com/3rPsO.png" alt="enter image description here"></p> <hr> <h3>Checking the solution: Operating with repeating decimals</h3> <p>Let's now check the addition and multiplication of repeating decimals:</p> <pre><code>a = repeatingDecimal[7/31]; b = repeatingDecimal[24/31]; Print["a = ", a] Print["b = ", b] Print["a + b = ", a, " + ", b, " = ", a + b] Print["7/31 \[Times] 24/31 = " , (7/31)* (24/31)] Print["a\[Times]b = ", a*b, " = \n", repeatingDecimal[a*b]] Print[N[168/961, 465]] </code></pre> <p><img src="https://i.stack.imgur.com/HzlTp.png" alt="repeating decimal addition and multiplication"></p> <p>So addition and multiplication of repeating decimals work as desired. <code>Power</code> also appears to work properly.</p> <p>Notice that 168/961 occupies 465 places to the right of the decimal point. After that, it starts to repeat. The results match those of <code>N[168/961, 465]</code>, except for the <code>OverBar</code>, although line-breaks occur at different places. And, as is to be expected, this jibes with the following:</p> <pre><code>digits = RealDigits[168/961] Length[digits[[1, 1]]] </code></pre> <p><img src="https://i.stack.imgur.com/XKaB3.png" alt="465 digits"></p> <hr> <h3>Some effects of the Format[] wrapper on the behavior of N[] in summing repeated decimals</h3> <p>Mr.Wizard suggested that the <strong>Format</strong> wrapper is superfluous for the cases of Integers and Reals.</p> <p>Let's consider how the following two additions</p> <pre><code>repeatingDecimal[7/31] + repeatingDecimal[24/31] N@repeatingDecimal[7/31] + N@repeatingDecimal[24/31] </code></pre> <p>behave in four different cases:</p> <p><em>Case 1</em>: Results when <code>Format</code> <strong>wrapped</strong> around repeatingDecimals for Reals and Integers and up values are <strong>ON</strong></p> <p><img src="https://i.stack.imgur.com/kDebI.png" alt="Case 1"></p> <p>As expected, the first addition yields an integer, the second a decimal.</p> <p><hr> <em>Case 2</em>: Results when <code>Format</code> <strong>NOT wrapped</strong> around repeatingDecimals for Reals and Integers but up values are <strong>ON</strong></p> <p><img src="https://i.stack.imgur.com/26dQX.png" alt="Case 2"></p> <p>The <code>Format</code> wrapper around Reals and Integers doesn't affect the additions at hand.</p> <p><hr> <em>Case 3</em>: Results when <code>Format</code> <strong>wrapped</strong> around repeatingDecimals for Reals and Integers but up values are <strong>OFF</strong></p> <p><img src="https://i.stack.imgur.com/xHXBJ.png" alt="Case 3"></p> <p>If upvalues are OFF, <code>Format</code> prevents addition from happening.</p> <p><hr> <em>Case 4</em>: Results when <code>Format</code> <strong>NOT wrapped</strong> around repeatingDecimals for Reals and Integers and up values are <strong>OFF</strong></p> <p><img src="https://i.stack.imgur.com/D5z4d.png" alt="Case 4"></p> <p>If upvalues are OFF and Format` <strong>NOT wrapped</strong> around repeatingDecimals for Reals and Integers , the second addition works as expected.</p> <p>All the more reason to remove the Format wrapper for the case of reals and integers.</p> <hr> <p>Anyone have any remarks about the different outcomes in Cases 3 and 4?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload