Brainfuck的绝对数量

发布于 2025-01-31 12:17:28 字数 194 浏览 2 评论 0 原文

您将如何在Brainfuck中获得绝对的数字? 我最初以为将数字平方( [ - >+>+<<<] +>+<<]>> [ - <<<+>>><<<<< - ] ))和平方生根会起作用,但我无法正常想想一种通向平方根的方法。

How would you go about getting the absolute of a number in brainfuck?
I originally thought squaring the number ([->+>+<<]>>[-<<+>>]<<[>[->+>+<<]>>[-<<+>>]<<<-]) and square rooting it would work, but I can't think of a way to square root.

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

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

发布评论

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

评论(2

过潦 2025-02-07 12:17:28

计算数字绝对的一种方法是确定其标志。如果您知道您的程序如何表示负数,则会有一个 /u/u/u/danielcristofani 一个数字,你可以

将数字加倍,看看它在过程中是否变为零,例如使用内存布局 0 0 x 0 ,这应该有效,生产 0 f 0 0 0 0 其中<代码> f 是符号flag [&gt; ++ [&lt;]&lt; [[ - ]+&lt;+&lt;]&gt;&gt; - ]&gt; [ - ]&gt; [ - ]&lt; < /代码>


如有必要,然后应应用 x = -x 算法,例如其包装版本

  temp0 [ - ]
x [temp0-x-]
temp0 [x-temp0+]
 

One way to compute the absolute of a number is to identify its sign. If you know how your program represents negative numbers, there is a reddit answer by /u/danielcristofani that explains that, in order to check for the sign of a number, you can

double the number and see if it becomes zero in the process, e.g. with memory layout 0 0 x 0, this should work, producing 0 f 0 0 where f is the sign flag [>++[<]<[[-]+<+<]>>-]>[-]<

If necessary, you should then apply an x = -x algorithm, for example its wrapping version:

temp0[-]
x[temp0-x-]
temp0[x-temp0+]
空心空情空意 2025-02-07 12:17:28

我们可以做得更好。从0 x 0 0开始,从x处的指针开始,

[<++[>->]>[[<-->-]>+>]<<]<[>>+<<--]

结果在0 0 | x | 0用指针在四个的最左侧单元格。这假设数字在通常的两个补体表示中,其中255 = -1和254 = -2,依此类推(如果单元格为字节)。这反过来假设在处理数字之前,该程序一直在谨慎地不超过127或下流,超过了-128。可行,但根据这些数字所做的事情,最好从一开始分开表示标志。

We can do a bit better than that. Beginning with 0 x 0 0 with the pointer at x,

[<++[>->]>[[<-->-]>+>]<<]<[>>+<<--]

results in 0 0 |x| 0 with the pointer at the leftmost cell of the four. This assumes the number was in the usual two's complement representation, where 255 = -1 and 254 = -2 and so on (if cells are bytes). Which in turn assumes that in handling numbers up to this point, the program has been careful not to overflow past 127 or underflow past -128. Doable, but depending what's being done with these numbers it might be better to represent the sign separately from the beginning.

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