douyi4912 2014-02-17 23:06
浏览 42
已采纳

将浮点数用于计数器-有问题吗?

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.

  • 写回答

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,

    http://en.wikipedia.org/wiki/Double-precision_floating-point_format

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

    "...next 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏