2014-02-17 23:06
浏览 34


The code I'm looking at uses a golang float64 as a counter, does this pose a problem with loss of accuracy at some point? Specifically, are all integers represented in the range covered by float64? If not, where do I start running into problems? When do I run out of contiguous integers?

In case you're wondering, the reason for using a float64 as a counter is because it's part of a []float64 that holds many metrics that are not integers.

图片转代码服务由CSDN问答提供 功能建议

我正在查看的代码使用golang float64作为计数器,这是否会造成准确性下降的问题 在某一点? 具体来说,是否所有整数都在float64覆盖的范围内? 如果没有,我从哪里开始遇到问题? 我什么时候会用完连续的整数?

如果您想知道,使用float64作为计数器的原因是因为它是[] float64的一部分,它包含许多可用于 不是整数。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongtui0650 2014-02-17 23:22

    The golang specification says, "float64 is the set of all IEEE-754 64-bit floating-point numbers." which is commonly known as double precision numbers,

    You can read all of its glory details if interested, but to answer your question, quote,

    " range, from 253 to 254, everything is multiplied by 2..."

    So if you want to use it as counters, i.e. positive integers, you won't have problem until 253=9,007,199,254,740,992.

    点赞 打赏 评论
  • douti9286 2014-02-17 23:22

    Yes, float64 has 52 bits to store your counter. You should be accurate until 253.

    (Side note: the entire JavaScript language uses only Floating Point.)

    点赞 打赏 评论

相关推荐 更多相似问题