如何在 Brainfuck 中获得数字的绝对值?
我最初认为对数字( [->+>+<<]>>[-<<+>>]<<[>[->+>+<<]>>[-<<+>>]<<<-]
)进行平方并求平方根会起作用,但我想不出一种求平方根的方法。
最佳答案
计算数字绝对值的一种方法是确定其符号。如果您知道程序如何表示负数,则有 reddit answer通过 /u/danielcristofani这说明,为了检查数字的符号,您可以
double the number and see if it becomes zero in the process, e.g. with memory layout
0 0 x 0
, this should work, producing0 f 0 0
wheref
is the sign flag[>++[<]<[[-]+<+<]>>-]>[-]<
如有必要,您应该申请 x = -x
算法,例如its wrapping version :
temp0[-] x[temp0-x-] temp0[x-temp0+]
关于brainfuck - Brainfuck中数字的绝对值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72329245/