comparing floating points via "==" or "!="

Igor Mozolevsky mozolevsky at gmail.com
Thu Dec 1 00:53:53 UTC 2011


On 1 December 2011 00:48, Igor Mozolevsky <mozolevsky at gmail.com> wrote:
> On 1 December 2011 00:43, Adrian Chadd <adrian at freebsd.org> wrote:
>> Erm, these aren't run-time comparisons though, these are done at compile time.

Ignoring the late-night PEBKAC:


% gcc -E -O2 -
#define NA1(_r) \
	        (((_r) == 6.5)  ? 8 : (((_r) == 13)  ?  9 : (((_r) == 19.5)? 10 : \
	        (((_r) == 26)  ? 11 : (((_r) == 39)  ? 12 : (((_r) == 52)  ? 13 : \
	        (((_r) == 58.5)? 14 : (((_r) == 65)  ? 15 : 0))))))))

NA1(7);


# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"


(((7) == 6.5) ? 8 : (((7) == 13) ? 9 : (((7) == 19.5)? 10 : (((7) ==
26) ? 11 : (((7) == 39) ? 12 : (((7) == 52) ? 13 : (((7) == 58.5)? 14
: (((7) == 65) ? 15 : 0))))))));


It is entirely probable that the compiler might try to optimise that
out if it knows the value, but then you'd be relying on the compiler
knowing how to deal with floating point comparisons...


Cheers,

--
Igor


More information about the freebsd-wireless mailing list