Absolucy / Absolucy/nanorand-rs

Generated floats are not uniformly distributed

未關閉
#30 4 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
bug good first issue
主要語言
Rust
星號
251
分支
24
PR 合併指標
30 天內沒有已合併 PR

描述

Hej 👋

The documentation does not say anything about the distribution of the values, though one would assume that primitive values ought to be uniformly distributed. This is not the case for f32 and f64 based on https://github.com/Absolucy/nanorand-rs/blob/5e4ea401102cb3e1a519fed84778bc110d792e56/nanorand/src/gen.rs#L97 and https://github.com/Absolucy/nanorand-rs/blob/5e4ea401102cb3e1a519fed84778bc110d792e56/nanorand/src/gen.rs#L103

Floats above a certain value lose prevision, so using, say, `u32::MAX` and `u32::MAX - 42` as the numerator both yield `1.0`, which leads to a bias of the generated floats towards 1.0. This playground illustrates it a bit more: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=465eb2dcf33851195dde9f3a6601ea22

If you want to fix that, a solution would be to use the "safe" value provided by `::MANTISSA_DIGITS` as the max instead of `::MAX`. I could ope a PR for that if you like.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。