TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How many floating-point numbers are in the interval [0,1]? (2017)

113 点作者 burntcaramel大约 2 年前

14 条评论

cormacrelf大约 2 年前
For 32 bit floats, you can skip the math and just test all of them. LLVM will vectorise and unroll this nicely.<p><pre><code> fn main() { let start = std::time::Instant::now(); let total = (0..=u32::MAX) .filter(|&amp;x| { let f = f32::from_bits(x); 0. &lt;= f &amp;&amp; f &lt;= 1. }) .count(); println!(&quot;total {total} in {:?}&quot;, start.elapsed()); } total 1065353218 in 1.364751583s </code></pre> Edit: Apparently if you move the sum to its own function it runs in 500ms. A bit temperamental.<p>Edit 2: it&#x27;s the size of the sum accumulator that makes it slow. The version above is like `.fold(0usize, |a, _| a + 1)`. When I moved it to another function, I cast the return value to u32, so LLVM saw basically `.fold(0u32, |a, _| a + 1)` and could use u32 throughout. Godbolt says the usize version ends up with floats in xmm* registers on x86, which fit 4 32-bit floats, but the u32 version ends up with floats in ymm* registers (8 32-bit floats) and similar half-as-wide behaviour on ARM.
评论 #35581135 未加载
评论 #35581510 未加载
评论 #35581173 未加载
评论 #35582060 未加载
评论 #35587370 未加载
评论 #35583855 未加载
评论 #35582564 未加载
dr_dshiv大约 2 年前
&gt; 1,056,964,610. There are 4,294,967,296 possible 32-bit words, so about a quarter of them are in the interval [0,1]. Isn’t that interesting? Of all the float-pointing point numbers your computer can represent, a quarter of them lie in [0,1]. By extension, half of the floating-point numbers are in the interval [-1,1].
评论 #35581194 未加载
评论 #35583093 未加载
an1sotropy大约 2 年前
Others have previously pondered this, and the related issue of how to randomly select floats in [0,1). I found this to be a helpful account:<p><a href="https:&#x2F;&#x2F;mumble.net&#x2F;~campbell&#x2F;2014&#x2F;04&#x2F;28&#x2F;uniform-random-float" rel="nofollow">https:&#x2F;&#x2F;mumble.net&#x2F;~campbell&#x2F;2014&#x2F;04&#x2F;28&#x2F;uniform-random-float</a><p>and there&#x27;s working C code too:<p><a href="http:&#x2F;&#x2F;mumble.net&#x2F;~campbell&#x2F;2014&#x2F;04&#x2F;28&#x2F;random_real.c" rel="nofollow">http:&#x2F;&#x2F;mumble.net&#x2F;~campbell&#x2F;2014&#x2F;04&#x2F;28&#x2F;random_real.c</a>
评论 #35584347 未加载
mike_hock大约 2 年前
float32: 1 for exactly 1.0 (exponent=127,mantissa=0) + 127 * 2^23 for [0,1) (127 distinct exponent values (0 through 126) and any bit pattern in the mantissa; this includes +0 and denormals) + 1 for negative zero = 1065353218 = 0x3f800002 + 2, i.e. two more than the representation of 1.0, because everything up to and including that (as an unsigned integer) represents something in [0,1], plus the missing negative zero.
评论 #35581800 未加载
评论 #35581991 未加载
EthicalSimilar大约 2 年前
I apply the above theory for generating random numbers in JavaScript, based on given inputs (server seed, client seed, and nonce). JavaScript uses double precision floating point numbers, meaning 52 bits for the mantissa.<p><pre><code> const hash = crypto .createHmac(&#x27;SHA256&#x27;, serverSeed) .update(`${ clientSeed }:${ nonce }`) .digest(&#x27;hex&#x27;); const significant = hash.substring(0, 52 &#x2F; 4); const integer = parseInt(significant, 16); const float = integer &#x2F; (2 * 52); return { float, hash, };</code></pre>
macintux大约 2 年前
Presumably an offshoot of the discussion around floating point gotchas in gaming: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35539595" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=35539595</a>
nighthawk454大约 2 年前
Tangentially, it may be interesting to think how this differs for other float formats. Such as: Google Brain&#x27;s bfloat16, Nvidia&#x27;s TensorFloat
yarg大约 2 年前
Relevant: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;43153699&#x2F;how-can-smallest-floating-point-number-be-2-126-not-2-128" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;43153699&#x2F;how-can-smalles...</a>
dang大约 2 年前
Discussed at the time:<p><i>How many floating-point numbers are in the interval [0,1]?</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13759458" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=13759458</a> - Feb 2017 (153 comments)
pestatije大约 2 年前
So is it 1,056,964,610 (TFA)<p>or is it 1,065,353,218 (comments) and why the difference?
评论 #35584413 未加载
eimrine大约 2 年前
Can I see a graph to know how many of them are in any interval?
评论 #35582020 未加载
评论 #35584172 未加载
garbagecoder大约 2 年前
As many as you want depending on how many bits. Floats are made to approximate real numbers. There is an uncountably infinite number of real numbers on any open interval of real numbers. Since (0,1)\in[0,1] there you go. Just keep adding bits.
评论 #35583583 未加载
sampo大约 2 年前
About 1&#x2F;4 of all of them.
tand22大约 2 年前
A grey link already for me xD