Re: Float comparison



CBFalconer <cbfalconer@xxxxxxxxx> writes:
Keith Thompson wrote:

... snip ...

A stored value of type double cannot represent the real value
1.0+DBL_EPSILON/2.0, any more than a stored value of type int
can represent the value 1.5.

So what? doubles are used to store reals, more or less. ints are
used to store integers. 1.0+DBL_EPSILON/2.0 is a real[1]. 1.5 is
NOT an integer.

[1] but not a storable real, in a double.

It's that "more or less" that bites you, isn't it? Your model makes
some sense if you ignore those pesky details where it falls apart.

ints can only store a small subset of the set of integers. No
integers outside that subset can be stored in an int object. It
happens that the set of int values completely covers a range of
mathematical integers; that's one difference between ints as a subset
of integers and doubles as a subset of reals.

doubles can only store a small subset of the set of reals. No value
that's not in that subset can be stored in a double object.

Have you read C99 5.2.4.2.2 paragraphs 1-2? (In PDF or hard copy;
plain text doesn't show the formula.) Do you understand what it
means?

Please do not post a followup to this article without quoting and
responding to the previous paragraph, starting with "Have you read".

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Sweeney presentation "The Next Mainstream Language"
    ... There is no common supertype for int and real that's also numeric. ... So we're stuck with approximations of reals. ... possibly helped by programmer annotations or checks. ...
    (comp.lang.functional)
  • Re: Sweeney presentation "The Next Mainstream Language"
    ... Specify int to be more specific than real. ... Reals with sufficient precision can work as bounded integers with no ... Annotations need to be verified by the compiler, ... and/or compiler-checked stack allocation. ...
    (comp.lang.functional)
  • Re: Sweeney presentation "The Next Mainstream Language"
    ... Specify int to be more specific than real. ... Reals with sufficient precision can work as bounded integers with no ... That sounds reasonable, but for games programming, you often need ... for stack allocation (if the compiler can't verify safety of stack ...
    (comp.lang.functional)
  • Re: Sweeney presentation "The Next Mainstream Language"
    ... instance (allowing, e.g., effect-free types to use an optimized ... Specify int to be more specific than real. ... integers, but if you use /, sqrt etc., you use reals. ... programming, you don't need to worry overmuch about those. ...
    (comp.lang.functional)
  • Re: Gamma function question
    ... dt/t is the Haar measure ... on the group of positive reals with multiplication ... Gamma= int e^t^x dt/t ... for p = infinity, and I can't imagine how anyone ...
    (sci.math)