Search code examples
pythonfloating-pointinteger

In Python, what expression is equivalent to `0x1.0p-53`?


I'm reading that when xoshiro++ is implemented in C to generate uniform doubles in the unit interval, a 64-bit unsigned integer x should be converted to a 64-bit double using the expression (x >> 11) * 0x1.0p-53. What is the corresponding expression if (for perverse reasons) I want to implement this in Python?


Solution

  • (x >> 11) * float.fromhex('0x1p-53')

    fromhex is given as a way to interpret a hexadecimal floating-point literal in this Python documentation.

    You might also use (x >> 11) * 0.00000000000000011102230246251565404236316680908203125. However, I do not know if Python implementations will necessarily convert that literal without error. Python does not have a formal specification, and what the documentation does say about floating-point is weak.