如何在mathematica中显示不同基数的重复小数

发布于 2024-12-19 10:16:06 字数 338 浏览 1 评论 0原文

我读了这个伟大的问题< /a> 关于显示和对它们进行简单的算术,但我想知道鉴于此(或只是从头开始),如何在给定不同的基数时显示然后进一步类似地对它们进行算术?

例如,

(1/3)_2=0.01表示二进制形式的分数1/3是重复二进制数字01

谢谢。

I read this great question about showing and doing simple arithmetic on them, but I am wondering given this (or simply starting from scratch), how to show and then further similarly do arithmetic on them when given a different base?

For example,

(1/3)_2=0.01 means the fraction 1/3 in binary form is repeating the binary digits 01.

Thank you.

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

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

发布评论

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

评论(3

久而酒知 2024-12-26 10:16:06

这是一个尝试。巫师先生做了很多繁重的工作,特别是在保底算术方面。

rd[n_] := rd[n, 10]
rd[rd[n_, _], b_] := rd[n, b]

Format[rd[n_Integer | n_Real, base_]] := BaseForm[n, base]

Format[rd[q_Rational, base_]] :=
  Subscript[Row @ Flatten[{
     IntegerString[IntegerPart@q, base], ".",
     RealDigits[FractionalPart@q, base] /.
      {{nr___, r_List:{}}, pt_} :> {0~Table~{-pt}, nr, OverBar /@ r}
   }], base /. 10 -> ""]

可以使用以下方法实现保留基数算术:

Scan[
  (#[rd[q1_, b1_], rd[q2_, b2_] | tail___] ^:=
     rd[ #[q1, q2, tail], If[b1 === b2, b1, 10] ]) &,
  {Plus, Times, Power}
]

检查以多个基数进行的重复小数的转换是否有效。还检查加法、乘法和除法的例程:

Grid[{{"n", "value", "decimal", "rd[n,10]", "rd[n,2]", "rd[n,3]",  "rd[n,7]"}, 
 {"a", a = 14/3, N[a], rd[a, 10], rd[a, 2], rd[a, 3],  rd[a, 7]}, 
 {"b", b = 2/5, N[b], rd[b, 10], rd[b, 2], rd[b, 3], rd[b, 7]}, 
 {"c", c = 1/2, N[c], rd[c, 10], rd[c, 2], rd[c, 3], rd[c, 7]}, 
 {"a + b", a + b, N[a + b], rd[a, 10] + rd[b, 10], 
    rd[a, 2] + rd[b, 2], rd[a, 3] + rd[b, 3], rd[a, 7] + rd[b, 7]}, 
 {"a + b + c", a + b + c, N[a + b + c], 
   rd[a, 10] + rd[b, 10] + rd[c, 10], rd[a, 2] + rd[b, 2] + rd[c, 2], 
   rd[a, 3] + rd[b, 3] + rd[c, 3], 
   rd[a, 7] + rd[b, 7] + rd[c, 7]}, 
 {"a \[Times] b ", a*b, N[a*b], 
    rd[a, 10]*rd[b, 10], rd[a, 2]*rd[b, 2], rd[a, 3]*rd[b, 3], 
    rd[a, 7]*rd[b, 7]}, {"a \[Times] b \[Times] c ", a*b*c, N[a*b*c], 
    rd[a, 10]*rd[b, 10]*rd[c, 10], rd[a, 2]*rd[b, 2]*rd[c, 2], 
    rd[a, 3]*rd[b, 3]*rd[c, 3], rd[a, 7]*rd[b, 7]*rd[c, 7]}, 
 {"a / b", 
    a/b, N[a/b], rd[a, 10]/rd[b, 10], rd[a, 2]/rd[b, 2], 
    rd[a, 3]/rd[b, 3], rd[a, 7]/rd[b, 7]}}, Dividers -> All]

summary table


编辑

最新的改进(信用再次致Mr.Wizard)支持嵌套:

ClearAll[f, x, y]
f := (x/(x + 3 + 2 y) + y)/7 x; f
f // FullForm
x = 14/3; y = 1/3; f
BaseForm[N[f], 10]
x = rd[14/3, 10]; y = rd[1/3, 10]; f
x = rd[14/3, 2]; y = rd[1/3, 2]; f
x = rd[14/3, 5]; y = rd[1/3, 5]; f

nesting

Here's an attempt. Mr.Wizard did much of the heavy lifting, especially in the base-preserving arithmetic.

rd[n_] := rd[n, 10]
rd[rd[n_, _], b_] := rd[n, b]

Format[rd[n_Integer | n_Real, base_]] := BaseForm[n, base]

Format[rd[q_Rational, base_]] :=
  Subscript[Row @ Flatten[{
     IntegerString[IntegerPart@q, base], ".",
     RealDigits[FractionalPart@q, base] /.
      {{nr___, r_List:{}}, pt_} :> {0~Table~{-pt}, nr, OverBar /@ r}
   }], base /. 10 -> ""]

Base-preserving arithmetic can be implemented using this:

Scan[
  (#[rd[q1_, b1_], rd[q2_, b2_] | tail___] ^:=
     rd[ #[q1, q2, tail], If[b1 === b2, b1, 10] ]) &,
  {Plus, Times, Power}
]

Checking to see that conversions to repeating decimals in several bases work. Also checking routines for adding, multiplying, and dividing:

Grid[{{"n", "value", "decimal", "rd[n,10]", "rd[n,2]", "rd[n,3]",  "rd[n,7]"}, 
 {"a", a = 14/3, N[a], rd[a, 10], rd[a, 2], rd[a, 3],  rd[a, 7]}, 
 {"b", b = 2/5, N[b], rd[b, 10], rd[b, 2], rd[b, 3], rd[b, 7]}, 
 {"c", c = 1/2, N[c], rd[c, 10], rd[c, 2], rd[c, 3], rd[c, 7]}, 
 {"a + b", a + b, N[a + b], rd[a, 10] + rd[b, 10], 
    rd[a, 2] + rd[b, 2], rd[a, 3] + rd[b, 3], rd[a, 7] + rd[b, 7]}, 
 {"a + b + c", a + b + c, N[a + b + c], 
   rd[a, 10] + rd[b, 10] + rd[c, 10], rd[a, 2] + rd[b, 2] + rd[c, 2], 
   rd[a, 3] + rd[b, 3] + rd[c, 3], 
   rd[a, 7] + rd[b, 7] + rd[c, 7]}, 
 {"a \[Times] b ", a*b, N[a*b], 
    rd[a, 10]*rd[b, 10], rd[a, 2]*rd[b, 2], rd[a, 3]*rd[b, 3], 
    rd[a, 7]*rd[b, 7]}, {"a \[Times] b \[Times] c ", a*b*c, N[a*b*c], 
    rd[a, 10]*rd[b, 10]*rd[c, 10], rd[a, 2]*rd[b, 2]*rd[c, 2], 
    rd[a, 3]*rd[b, 3]*rd[c, 3], rd[a, 7]*rd[b, 7]*rd[c, 7]}, 
 {"a / b", 
    a/b, N[a/b], rd[a, 10]/rd[b, 10], rd[a, 2]/rd[b, 2], 
    rd[a, 3]/rd[b, 3], rd[a, 7]/rd[b, 7]}}, Dividers -> All]

summary table


Edit

The latest refinements (credit, once again, to Mr.Wizard) support nesting:

ClearAll[f, x, y]
f := (x/(x + 3 + 2 y) + y)/7 x; f
f // FullForm
x = 14/3; y = 1/3; f
BaseForm[N[f], 10]
x = rd[14/3, 10]; y = rd[1/3, 10]; f
x = rd[14/3, 2]; y = rd[1/3, 2]; f
x = rd[14/3, 5]; y = rd[1/3, 5]; f

nesting

私野 2024-12-26 10:16:06

简单:BaseForm[1./12, 3] 将向您显示以 3 为基数的 1/12(1 后面的小数点是为了确保近似值)作为重复小数。

额外:将 x 基数转换为 10 基数更加简单 x^^

Simple: BaseForm[1./12, 3] will show you 1/12 (the decimal point after the 1 is to ensure approximation) in base 3 as a repeating decimal.

Extra: Converting base x to base ten is even simpler x^^<NUMBER>

森罗 2024-12-26 10:16:06

'RealDigits' 能够处理各种基数,因此

RealDigits[1/3, 2]

{{{1, 0}}, -1}

请参阅文档 关于您可能获得的精确输出格式。它可能相当复杂。

'RealDigits' is able to handle all kinds of bases, so for instance

RealDigits[1/3, 2]

{{{1, 0}}, -1}

Refer to the documentation about the precise output format you may get. It can be rather complex.

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