Re: Float comparison
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Thu, 23 Apr 2009 01:00:22 +0100
Mark McIntyre <markmcintyre@xxxxxxxxxxxxxxxxxxx> writes:
On 21/04/09 21:38, Eric Sosman wrote:<snip>
Mark McIntyre wrote:
On 21/04/09 11:18, Alessio Ribeca wrote:
Thank you, How can I check for [floating-point] equality ?
You can't. You can only see if two floats are very close to each other.
This bit of nonsense has cropped up more than once in this
thread, so I'm very much afraid people may believe it. But it's
still nonsense!
I disagree.
But strict equality *is* testable in C, if you want to do it,
and don't listen to anyone who tries to tell you otherwise.
And don't listen to anyone who tells you its safe to use == to compare
floats. Really Eric, I'm shocked. Is this you, or a sockpuppet.
I disagree. Ages ago (I say this only to excuse the inevitable errors
that my memory will introduce) I wrote a package for doing interval
arithmetic. A number like 0.1 would be represented by the smallest
representable interval that contains 0.1.
Sometimes the user knows an input number is both perfectly accurate
and representable (for example the power in the inverse square law can
be taken to be 2). There was a notation for this (I forget) that
created an interval of zero width. The arithmetic algorithms were
much faster if this case was given special treatment (usually you need
half the number of operations) which of course was done with an
equality test.
This anchored a couple of things firmly in my mind: that == is
sometimes what you want to test floating point numbers for, and that
floats are exact. There was no error in the end points of the
intervals. They were exact values even though, in most cases, neither
was an entirely accurate representation of the value the programmer
wanted.
--
Ben.
.
- References:
- Float comparison
- From: Alessio Ribeca
- Re: Float comparison
- From: nick_keighley_nospam
- Re: Float comparison
- From: Alessio Ribeca
- Re: Float comparison
- From: Mark McIntyre
- Re: Float comparison
- From: Eric Sosman
- Re: Float comparison
- From: Mark McIntyre
- Float comparison
- Prev by Date: Re: On Call By Value
- Next by Date: Re: Request for comment on my tiny learning project: dfighterdb(mytree)
- Previous by thread: Re: Float comparison
- Next by thread: Re: Float comparison
- Index(es):
Relevant Pages
|