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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

When a float does not change when incrementing

6 点作者 mihvoi超过 10 年前

2 条评论

colanderman超过 10 年前
Note also the reason 16777221 is rounded down, but 16777223 is rounded up: IEEE floating point, in &quot;round to nearest&quot; mode (likely the default here), rounds values exactly in-between two representable values to the nearest number whose least-significant mantissa bit is 0. This is done in order to avoid the statistical bias of, for example, always rounding such values up (as we are taught in elementary school).<p>Since at this magnitude, the least-significant mantissa bit falls in the 2s place, odd numbers fall exactly between two valid representations, and are therefore rounded to multiples of 4.
mihvoi超过 10 年前
Using the &quot;float&quot; or &quot;double&quot; data types could lead to unexpected results if done without enough precautions. For example incrementing a &quot;float&quot; variable could leave the value unchanged, for relatively small values — like 20 millions. Do you think that these dangers are well understood by the average software developer?