我该如何进行“工作"?重复一个有理数的十进制表示? [英] How can I make a "working" repeating decimal representation of a rational number?

查看:71
本文介绍了我该如何进行“工作"?重复一个有理数的十进制表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

repeatingDecimal实际上不能用作重复的小数.我想对其进行变形,使其看起来像并表现,就像重复的小数.

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.

如何使工作重复小数表示(可能使用Interpretation[])?

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

请原谅我.这是我的第一个问题,我想清楚自己的想法.

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

所以

repeatingDecimal[7/31]

正确显示小数点(此处显示为图片,以便出现OverBar).

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

在引擎盖下,它实际上只是一个冒名顶替者,一个重复小数的图像...

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

我想增加产量:1

Leonid显示了如何将Format包裹在例程周围,以及如何提供递增值以添加和乘以重复的小数.很有帮助!我要花一些时间来适应上下值.

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.

以下基本上是由Wizard先生建议的简化代码版本.我将OverBar设置在每个重复的数字上方以允许换行. (在Row上方的单个OverBar看起来比较整齐,但是在达到正确的屏幕边距时不会中断.)

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

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

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

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

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

因此重复小数的加法和乘法可以按需工作. Power似乎也可以正常工作.

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

请注意,168/961在小数点右边占465位.之后,它开始重复.结果与N[168/961, 465]的结果匹配,但OverBar除外,尽管换行发生在不同的位置.而且,可以预料的是,这与以下内容有关:

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

Wizard先生建议,在整数和实数的情况下,格式包装器是多余的.

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:

情况1 :Format 包装围绕重复实数和整数的小数以及上限值为 ON

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

按预期,第一次加法会产生一个整数,第二个会产生一个十进制.

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


情况2 :当Format 不包含围绕实数和整数的重复小数但向上的值是 ON


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

围绕Reals和Integers的Format包装器不会影响当前的添加.

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


情况3 :Format 包装围绕实数和整数的重复小数但上限值为 OFF


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

如果upvalues为OFF,则Format防止加法发生.

If upvalues are OFF, Format prevents addition from happening.


情况4 :Format 不包含的情况下的结果重复实数和整数的小数和上限值为 OFF


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

如果upvalues为OFF并且Format NOT包裹,围绕Reals和Integers的repeatingDecimals,则第二个加法将按预期工作.

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.

有人对案例3和案例4的不同结局有何评论?

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

推荐答案

您不应该给出repeatingDecimal DownVaues,而应该给出FormatValues:

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

然后,您还可以给它UpValues以便与常见功能集成,例如:

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;

然后,例如

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

Out[146]= 1

您可以将此方法扩展到可能要使用repeatingDecimal的其他常用功能.

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

这篇关于我该如何进行“工作"?重复一个有理数的十进制表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆