Date: 2023-06-23 21:42 (UTC)
simont: A picture of me in 2016 (Default)
From: [personal profile] simont
A floating point number has three parts

"No, four parts: one small village still holds out against the invaders." (Presumably the NaNs)

the C standard says that [using a union] is undefined behaviour

I thought that was fixed? C99 TC3 introduced a footnote in ยง6.5.2.3 which says
If the member used to access the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.
which says to me that you take responsibility for knowing how your integers match up to your float on the target platform, but once you've got that right, the compiler isn't allowed to just throw your code away.

return((double)(pcg64_random(rng) >> 11) * 0x1.0p-53);

Why isn't it better to just return pcg64_random(rng) * 0x1.0p-64? Then even if the high 11 bits of the value are zero, you still get a full mantissa's worth of randomness.

Even better, go back to the bit-hacking approach, and set all 52 mantissa bits to random values, and set the exponent to 0x3fe - __builtin_clz64(pcg64_random())? Then your exponent can go all the way down to -64, and you still have a full mantissa even at the low end.

Ideally you'd want solution that would deliver a full mantissa of random bits no matter what the exponent, even in the ultra-improbable event that it's -1023 or something! That's in the spirit of IEEE 754, which normally takes the view that you should return the answer that arises from taking the true mathematical value of the result and rounding it to the nearest value in the target precision. In the case where the true value is random this takes a bit of interpretation, but I think "generate FP bit patterns with the same probability distribution as you'd get from rounding genuinely random real numbers to double" is surely the Platonically-right interpretation. And there's no part of that definition that would agree that below some arbitrary exponent threshold all mantissa bits are zero :-P

But if you want to go further than an exponent of -64 then I think you need a lot more random input data...
This account has disabled anonymous posting.
(will be screened if not on Access List)
(will be screened if not on Access List)
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org

June 2025

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated 2025-06-09 10:24
Powered by Dreamwidth Studios