RE: [Algorithms] Floating point comparison.
Brought to you by:
vexxed72
From: Stefan B. <ste...@te...> - 2001-06-29 23:45:55
|
> While mucking about in the debugger I noticed that comparing two floating > point numbers for equality actually generates FPU instructions rather than > just performing an integer comparison. > <...> > So the fact that the compiler feels motivated to use FPU instructions > either means it's lame (unlikely) or there are floating point numbers with > differing bit patterns that are equal. Other than +0 and -0 I can't think > of any. Well I don't have the CPU manuals at hand, but I would imagine a comparing a NAN with another NAN should always yield a false result, and likewise for INF? As an aside, I would recommend not explicitly hard-coding float-as-int comparisons in your code when you optimise... on some architectures (MIPS etc) float comparisons are quite fast, and by using integer comparisons you might actually end up slowing things down unless you're just comparing variables that you know are stored in memory (i.e. not results of recent computations). Personally I use some simple inline functions for quick compares. Cheers, Stef -- Stefan Boberg, R&D Manager, Team17 Software Ltd. bo...@te... |