Created
March 31, 2021 15:49
-
-
Save kevinmoran/9a0af144f49c32a441cd51bb520fee96 to your computer and use it in GitHub Desktop.
Converting an integer to a float between 0 and 1 with bithacks (for PRNG)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: "xoshiro / xoroshiro generators and the PRNG shootout" | |
// https://prng.di.unimi.it/ | |
#include <stdint.h> | |
static inline double u64ToDoubleBetween01(uint64_t x) { | |
return (x >> 11) * 0x1.0p-53; | |
} | |
// I came up with the following function for 32-bit floats based on the above, let me know if it's wrong | |
static inline float u32ToFloatBetween01(uint32_t x) { | |
return (x >> 8) * 0x1.0p-24f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment