Group Abstract Group Abstract

Message Boards Message Boards

Output repeating decimal numbers with an overline?

Posted 9 years ago

I would like to output repeating decimal numbers with an overline over the repeating digits. For instance, 13/7 becomes 1.857142, but with a line over the bolded digits instead of bold. I've been trying to write such a function myself using RealDigits, but so far I'm making slow progress. I know it can be done because Wolfram Alpha has such an output. Has anyone tackled this problem yet? It would save me a bunch of time if you'd share your solutions. I'm using the Wolfram Development Platform in the Desktop environment, if that makes any difference.

Thanks in advance,

Mark Greenberg

POSTED BY: Mark Greenberg
12 Replies

The output from RealDigits is complicated to use. Here is what I came up with:

normalizeDigitSequence =
  {{{beforeRecurring___, {recurring__}}, c_?NonPositive} :>
    {{0, beforeRecurring, {recurring}}, c + 1},
   {{beforeRecurring___, {recurring__}}, c_?Positive} /; 
     Length[{beforeRecurring}] < c :>
    {{beforeRecurring, First[{recurring}],
      RotateLeft[{recurring}]}, c}};
addOverlineToRepeating =
  {{beforeRecurring___, {recurring___}}, c_?Positive} /; 
    Length[{beforeRecurring}] >= c :>
   Row[Append[Insert[{beforeRecurring}, ".", c + 1],
     Overscript[Row[{recurring}], _]]];
RealDigits[13/7] //. normalizeDigitSequence /. addOverlineToRepeating

This is meant for display only. It does not evaluate to a number.

POSTED BY: Gianluca Gorni

The recurring digit sequence seems complete to me. From RealDigits[130/7] you get {{1, {8, 5, 7, 1, 4, 2}}, 2}, which is equivalent to {{1, 8, {5, 7, 1, 4, 2, 8}}, 2}. The sequence lends its first digit to the pre-recurring sequence and gets it back at the end.

As for the underscore, yes, now that I think of it it is indeed unexpected that Overscript[3, Blank[]] should work. I really meant to write underscore, not Blank[].

POSTED BY: Gianluca Gorni

What is wrong with 130/7? I simply arranged for the ovlerline non to cross over the decimal point. I was not aware of OverBar, which is indeed shorter. However, Overscript is regularly documented. Curiously,

OverBar[x] === Overscript[x, _]

gives False, but if you evaluate

{OverBar[x], Overscript[x, _]}

and look at the underlying BoxData in the output cell, you will see that they are represented identically as OverscriptBox["x", "_"]. If you copy and paste one of them into an expression that expects the other, you may run into mysterious trouble.

POSTED BY: Gianluca Gorni

I am glad that you found my code instructive. I regret my laziness in leaving the uninformative letter c_ when I could have written the more revealing positionOfDecimalPoint_. It took me some time to figure out the output of RealDigits when applied to rational numbers. The documentation on that has room for improvement.

POSTED BY: Gianluca Gorni
Posted 9 years ago
POSTED BY: Mark Greenberg
Posted 9 years ago

The sequence lends its first digit to the pre-recurring sequence and gets it back at the end.

Sorry, I hadn't noticed that. I take my words back.

POSTED BY: Alexey Popkov
Posted 9 years ago

What is wrong with 130/7? I simply arranged for the ovlerline non to cross over the decimal point.

That's the problem: for 130/7 the recurring digit sequence is incomplete, hence the output is wrong.

POSTED BY: Alexey Popkov

I know Overscript is documented, not that the second character can be a blank (not a string of an underscore)! Or did I miss something?

POSTED BY: Sander Huisman
Posted 9 years ago

This code gives wrong result, for example, for 130/7.

POSTED BY: Alexey Popkov
Posted 9 years ago

Instead of using the undocumented (?) Overscript with a Blank as a second argument, one can also use OverBar.

POSTED BY: Sander Huisman
Posted 9 years ago

Thank you, Gianluca. It works perfectly!

I love getting solutions from the community. Usually they employ techniques that I can learn from, as does this one. I'll have to pull it apart to see how it works. Perhaps I I'll write a step-by-step explanation then, for the benefit of others like me who want to learn more about the mysteries of the Wolfram language.

Thanks again,

Mark

POSTED BY: Mark Greenberg
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard