How can I make a “working” repeating decimal representation of a rational number?

后端 未结 2 1875
无人及你
无人及你 2020-12-15 09:35

I\'ve figured out how to display the repeating part of a repeating decimal using OverBar.

repeatingDecimal doesn\'t actually work as a repeating decimal

2条回答
  •  没有蜡笔的小新
    2020-12-15 10:00

    Here is a possible refactoring of your updated code. I think it works this time (fingers crossed). If you do not need the color highlighting, you can leave off ~Style~ and the rest of that line.

    ClearAll[repeatingDecimal];
    
    Format[repeatingDecimal[n_Integer | n_Real]] := n;
    
    Format[repeatingDecimal[q_Rational]] :=
     Row[{IntegerPart@q, ".", RealDigits@FractionalPart@q}] /.
      {{ nr___Integer, r_List:{} }, pt_} :>
       Row@Join[
          "0" ~Table~ {-pt},
          {nr},
          If[r === {}, {}, {OverBar@Row@r}]
          ] ~Style~ If[r === {}, Blue, If[{nr} === {}, Red, Gray]]
    
    repeatingDecimal /:
      (h : Plus | Times)[left___, repeatingDecimal[q_], right___] :=
        h[left, q, right];
    

    I will leave this older version here for reference, but I am now making edits to the Question community wiki.

提交回复
热议问题