Skip to main content

Module pekkizen

Module pekkizen 

Source
Expand description

Pekka Pulkkinen’s leading-zeros technique.

A Rust port of Float64_64, Float64_117 and Float64full from Pekka Pulkkinen’s uniFloats wiki. One 64-bit word is interpreted as a uniform fixed-point real in [0 . . 1): the count of leading zeros picks the binade (a geometric distribution), and the remaining bits are shifted into the mantissa.

The three variants differ in how far down the fixed point extends:

  • f64_64 stops at the first word: a uniform real rounded down to the 2⁻⁶⁴ grid, complete (every float reachable) in [2⁻¹² . . 1);
  • f64_117 draws a second word when the first has 12 or more leading zeros (probability 2⁻¹²), extending completeness to [2⁻⁶⁵ . . 1) with a 2⁻¹¹⁷ grid below;
  • f64_full additionally keeps drawing words while they are zero, reaching every float in [0 . . 1), subnormals included.

All variants consume one word per call with probability 1 − 2⁻¹² and, in the typical case, cost only a couple of operations more than division scaling.

Functions§

f64_64
Returns a random f64 distributed as a uniform 64-bit fixed-point real in [0 . . 1) rounded down to the nearest representable value: every float in [2⁻¹² . . 1), and the 2⁵² multiples of 2⁻⁶⁴ below 2⁻¹².
f64_117
Returns a random f64 distributed as a uniform 117-bit fixed-point real in [0 . . 1) rounded down to the nearest representable value: every float in [2⁻⁶⁵ . . 1), and the multiples of 2⁻¹¹⁷ below 2⁻⁶⁵.
f64_full
Returns a random f64 distributed as a uniform real in [0 . . 1) rounded down to the nearest representable value: every float in [0 . . 1) is reachable, subnormals and 0 included, with probability equal to the measure of the reals that round down to it.