我怎样才能做出“工作”?有理数的重复十进制表示?

发布于 2024-10-20 13:07:17 字数 4882 浏览 2 评论 0原文

我已经弄清楚如何使用 OverBar 显示重复小数的重复部分。

repeatingDecimal 实际上并不用作重复小数。我想对其进行变体,使其看起来都像重复的小数。


问题

如何制作工作重复十进制表示(可能使用Interpretation[])?


背景

如果我胡言乱语,请原谅。这是我的第一个问题,我想弄清楚我的想法。

下面将“绘制”一个循环小数。

repeatingDecimal[q2_] :=
 Module[{a},
  a[{{nr__Integer}, pt_}] := 
   StringJoin[
    Map[ToString, 
     If[pt > -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 > -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]]]

因此

repeatingDecimal[7/31]

正确显示重复小数(此处显示为图片,以便出现 OverBar)。

显示重复小数

仔细一看,它实际上只是一个冒名顶替者,一个重复小数的图像......

In[]:= repeatingDecimal[7/31]//FullForm
Out[]:= Row[List[".",OverBar["225806451612903"]]]

当然,它的行为不像数字:

% + 24/31

fraction plus Repeating Decimal

我想要添加到产量:1


编辑:A RepeatingDecimal Leonid 的清理版本

展示了如何将 Format 包装在例程中,并为重复小数的加法和乘法提供向上值。非常有帮助!我需要一些时间才能适应数值的上下波动。

下面的内容本质上是 Mr.Wizard 建议的代码的简化版本。我将 OverBar 设置在每个重复数字上方以允许换行。 (行上方的单个 OverBar 看起来更整洁,但在达到右侧屏幕边距时无法中断。)

ClearAll[repeatingDecimal]

repeatingDecimal[n_Integer | n_Real] := n

Format[repeatingDecimal[q_Rational]] := Row @ Flatten[
   {IntegerPart@q, ".", RealDigits@FractionalPart@q} /.
    {{nr___Integer, r_List: {}}, pt_} :> {Table[0, {-pt}], nr, OverBar /@ r}
  ]

repeatingDecimal[q_] + x_ ^:= q + x
repeatingDecimal[q_] * x_ ^:= q * x
repeatingDecimal[q_] ^ x_ ^:= q ^ x

下表显示了 repeatingDecimal 的一些输出:

n1 = 1; n2 = 15; ClearAll[i, k, r];
TableForm[Table[repeatingDecimal[i/j], {i, n1, n2}, {j, n1, n2}], 
TableHeadings -> {None, Table[("r")/k, {k, n1, n2}]}]

在此处输入图像描述


检查解决方案:使用重复小数进行运算

现在让我们检查重复小数的加法和乘法:

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]]

重复小数加法和乘法

因此,重复小数的加法和乘法可以按需要进行。 Power 似乎也可以正常工作。

请注意,168/961 占据小数点右侧 465 位。之后,它开始重复。结果与 N[168/961, 465] 的结果匹配,除了 OverBar 之外,尽管换行发生在不同的位置。而且,正如所预料的,这与以下内容相符:

digits = RealDigits[168/961]
Length[digits[[1, 1]]]

465digits


Format[] 包装器对N[] 对重复小数求和的行为

Mr.Wizard 建议 Format 包装器对于整数和实数的情况是多余的。

让我们考虑一下以下两个加法

repeatingDecimal[7/31] + repeatingDecimal[24/31]
N@repeatingDecimal[7/31] + N@repeatingDecimal[24/31]

在四种不同情况下的表现:

情况 1:当 Format 包裹 围绕实数和整数及向上值的重复小数时的结果是 ON

Case 1

正如预期的那样,第一个加法产生一个整数,第二个加法产生一个小数。


Case 2: Results when Format NOT wrapped around repeatingDecimals for Reals and Integers but up values are ON

Case 2

Reals 和 Integers 的 Format 包装器不会影响手头的添加。


Case 3: Results when Format wrapped around repeatingDecimals for Reals and Integers but up values are OFF

Case 3

如果 upvalues 为 OFF,Format 将阻止添加发生。


Case 4: Results when Format NOT wrapped around repeatingDecimals for Reals and Integers and up values are OFF

Case 4

如果 upvalues 为 OFF 并且 Format` 对于实数和整数不包裹重复小数,则第二次添加按预期工作。

更有理由删除实数和整数的格式包装器。


大家对案例3和案例4的不同结果有什么看法吗?

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. I'd like to make a variation of it that looks and behaves like a repeating decimal.


Question

How could I make a working repeating decimal representation (possibly using Interpretation[])?


Background

Please excuse me if I ramble. This is my first question and I wanted to be clear about what I have in mind.

The following will "draw" a repeating decimal.

repeatingDecimal[q2_] :=
 Module[{a},
  a[{{nr__Integer}, pt_}] := 
   StringJoin[
    Map[ToString, 
     If[pt > -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 > -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]]]

So

repeatingDecimal[7/31]

displays a repeating decimal properly (shown here as a picture so that the OverBar appears).

Repeating decimal displayed

Looking under the hood, it's really just an imposter, an image of a repeating decimal ...

In[]:= repeatingDecimal[7/31]//FullForm
Out[]:= Row[List[".",OverBar["225806451612903"]]]

Of course, it doesn't behave like a number:

% + 24/31

fraction plus repeating decimal

I'd like the addition to yield: 1


Edit: A cleaned up version of repeatingDecimal

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.

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.)

ClearAll[repeatingDecimal]

repeatingDecimal[n_Integer | n_Real] := n

Format[repeatingDecimal[q_Rational]] := Row @ Flatten[
   {IntegerPart@q, ".", RealDigits@FractionalPart@q} /.
    {{nr___Integer, r_List: {}}, pt_} :> {Table[0, {-pt}], nr, OverBar /@ r}
  ]

repeatingDecimal[q_] + x_ ^:= q + x
repeatingDecimal[q_] * x_ ^:= q * x
repeatingDecimal[q_] ^ x_ ^:= q ^ x

The table below shows some output from repeatingDecimal:

n1 = 1; n2 = 15; ClearAll[i, k, r];
TableForm[Table[repeatingDecimal[i/j], {i, n1, n2}, {j, n1, n2}], 
TableHeadings -> {None, Table[("r")/k, {k, n1, n2}]}]

enter image description here


Checking the solution: Operating with repeating decimals

Let's now check the addition and multiplication of repeating decimals:

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]]

repeating decimal addition and multiplication

So addition and multiplication of repeating decimals work as desired. Power also appears to work properly.

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 N[168/961, 465], except for the OverBar, although line-breaks occur at different places. And, as is to be expected, this jibes with the following:

digits = RealDigits[168/961]
Length[digits[[1, 1]]]

465 digits


Some effects of the Format[] wrapper on the behavior of N[] in summing repeated decimals

Mr.Wizard suggested that the Format wrapper is superfluous for the cases of Integers and Reals.

Let's consider how the following two additions

repeatingDecimal[7/31] + repeatingDecimal[24/31]
N@repeatingDecimal[7/31] + N@repeatingDecimal[24/31]

behave in four different cases:

Case 1: Results when Format wrapped around repeatingDecimals for Reals and Integers and up values are ON

Case 1

As expected, the first addition yields an integer, the second a decimal.


Case 2: Results when Format NOT wrapped around repeatingDecimals for Reals and Integers but up values are ON

Case 2

The Format wrapper around Reals and Integers doesn't affect the additions at hand.


Case 3: Results when Format wrapped around repeatingDecimals for Reals and Integers but up values are OFF

Case 3

If upvalues are OFF, Format prevents addition from happening.


Case 4: Results when Format NOT wrapped around repeatingDecimals for Reals and Integers and up values are OFF

Case 4

If upvalues are OFF and Format` NOT wrapped around repeatingDecimals for Reals and Integers , the second addition works as expected.

All the more reason to remove the Format wrapper for the case of reals and integers.


Anyone have any remarks about the different outcomes in Cases 3 and 4?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2024-10-27 13:07:17

您不应该指定 repeatingDecimal DownVaues,而应该指定 FormatValues

ClearAll[repeatingDecimal];
Format[repeatingDecimal[q2_]] := 
Module[{a}, 
 a[{{nr__Integer}, pt_}] := 
 StringJoin[
  Map[ToString, 
   If[pt > -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 > -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]]]

然后,您也可以指定 UpValues >,与常见函数集成,例如:

repeatingDecimal /: Plus[left___, repeatingDecimal[q_], right___] := left + q + right;
repeatingDecimal /: Times[left___, repeatingDecimal[q_], right___] :=  left * q * right;

然后,例如,

In[146]:= repeatingDecimal[7/31]+24/31

Out[146]= 1

您可以将此方法扩展到您可能想要使用 repeatingDecimal 的其他常见函数。

You shouldn't have given your repeatingDecimal DownVaues, but rather, FormatValues:

ClearAll[repeatingDecimal];
Format[repeatingDecimal[q2_]] := 
Module[{a}, 
 a[{{nr__Integer}, pt_}] := 
 StringJoin[
  Map[ToString, 
   If[pt > -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 > -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]]]

Then, you can give it also UpValues, to integrate with common functions, for example:

repeatingDecimal /: Plus[left___, repeatingDecimal[q_], right___] := left + q + right;
repeatingDecimal /: Times[left___, repeatingDecimal[q_], right___] :=  left * q * right;

Then, for example,

In[146]:= repeatingDecimal[7/31]+24/31

Out[146]= 1

You can extend this approach to other common functions which you may want to work with repeatingDecimal.

泛泛之交 2024-10-27 13:07:17

这是更新后的代码的可能重构。我认为这次有效(手指交叉)。如果不需要颜色突出显示,则可以省略 ~Style~ 和该行的其余部分。

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];

我将把这个旧版本留在这里供参考,但我现在正在对问题社区 wiki 进行编辑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文