0

As is well known, any real in [0,1] can be written as a binary expansion in base 1/2:

x = b1 * 1/2^1 + b2 * 1/2^2 + ...

I would like an efficient way to get bi for a given x and index i, and I don't think there's any built-in way to do that in Mathematica. IntegerDigits and RealDigits don't seem to be able to help, and none of the related functions are pertinent.

The obvious solution is to do the manual conversion, but I was hoping to avoid that. Am I missing something?

EDIT: for future reference, what I was looking for can be done this way,

BinaryExpansionBit[p, j] := RealDigits[p, 2, 1, -j][[1]][[1]]

where

BinaryExpansionBit[x, i]

gives the bi I was talking about.

4

2 回答 2

4

我看不出 RealDigits 有什么问题。

rd=RealDigits[0.1,2]

给出了一个很好的二进制展开:

(* out: 
    {{1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
   0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 
  1, 0, 0, 1, 1, 0, 1, 0}, -3} 
*)

测试:

rd[[1]].Table[1/2^(n - rd[[2]]), {n, Length[rd[[1]]]}]

(* out: 3602879701896397 / 36028797018963968, which is 0.1*)

的输出的第二个元素RealDigit告诉您第一个元素相对于小数点的位置。所以,对于一个真正的 r,0<r<1你的bi = rd[[1,i-rd[[2]]].

于 2011-07-23T22:00:58.393 回答
1

这取决于您所说的“高效”是什么意思。Mathematica 可以很容易地转换为二进制,正如这个Wolfram Alpha 示例所示。

否则,您正在寻找的是整数部分的奇偶校验x * 2^i

于 2011-07-23T19:56:50.207 回答