berExp static method

bool berExp(
  1. double x,
  2. double ccs,
  3. RandomBytes rb
)

Return a single bit, equal to 1 with probability ~ ccs * exp(-x).

Both x and ccs MUST be positive. Consumes exactly one byte from rb per loop iteration (at most 8 bytes).

Implementation

static bool berExp(double x, double ccs, RandomBytes rb) {
  // Truncate toward zero, matching Python's int(float).
  var s = (x * _iln2).toInt();
  final r = x - s * _ln2;
  if (s > 63) s = 63;
  final z = (approxExp(r, ccs) - BigInt.one) >> s;

  var w = 0;
  for (var i = 56; i >= 0; i -= 8) {
    final p = rb(1)[0];
    w = p - ((z >> i) & _byteMask).toInt();
    if (w != 0) break;
  }
  return w < 0;
}