Comparing IEEE floats and doubles for equality

前端 未结 15 2365
南方客
南方客 2020-11-30 06:00

What is the best method for comparing IEEE floats and doubles for equality? I have heard of several methods, but I wanted to see what the community thought.

15条回答
  •  难免孤独
    2020-11-30 06:57

    @DrPizza: I am no performance guru but I would expect fixed point operations to be quicker than floating point operations (in most cases).

    @Craig H: Sure. I'm totally okay with it printing that. If a or b store money then they should be represented in fixed point. I'm struggling to think of a real world example where such logic ought to be allied to floats. Things suitable for floats:

    • weights
    • ranks
    • distances
    • real world values (like from a ADC)

    For all these things, either you much then numbers and simply present the results to the user for human interpretation, or you make a comparative statement (even if such a statement is, "this thing is within 0.001 of this other thing"). A comparative statement like mine is only useful in the context of the algorithm: the "within 0.001" part depends on what physical question you're asking. That my 0.02. Or should I say 2/100ths?

提交回复
热议问题