1

I'm implementing xoshiro256++ and I'm able to generate pseudo-random 64-bit unsigned integers. The next challenge is generating uniform doubles in the unit interval from the PRNGs u64's.

a 64-bit unsigned integer x should be converted to a 64-bit double using the expression

(x >> 11) * 0x1.0p-53

How would one evaluate this in Rust? On trying this I get the compile error:

error: hexadecimal float literal is not supported
  --> src/main.rs:30:56
   |
30 |             f64::from_bits((x >> 11).wrapping_mul(0x1.0p-53))
   |                                                        ^^^^^
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ANoobSwiftly
  • 311
  • 4
  • 13

1 Answers1

2

You can use the hexf crate:

#[macro_use]
extern crate hexf;

fn main() {
    let v = hexf64!("0x1.0p-53");
    println!("{}", v);
}

See also:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Is there a way to use std only? trying to understand what's going on. – ANoobSwiftly Aug 19 '20 at 14:01
  • @ANoobSwiftly how would using std only help you understand what's going on? But sure, just copy all of the source code from the crate and whatever dependencies into your own code. Now you have no extra crates. – Shepmaster Aug 19 '20 at 14:02
  • Would this behave the same as their implementation: ((x >> 11) as f64) * (f64::exp2(-53.0))? – ANoobSwiftly Aug 21 '20 at 08:54