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

Alexander Best arundel at freebsd.org
Thu Dec 1 00:39:39 UTC 2011


On Thu Dec  1 11, Igor Mozolevsky wrote:
> On 1 December 2011 00:11, Alexander Best <arundel at freebsd.org> wrote:
> 
> > i sorry. since sys/dev/ath/ath_rate/sample/tx_schedules.h also handles a lot
> > of integer values, i thought you referred to abs(3), because you saw also
> > some issues with the integer computations in that file. so i thought you were
> > referring to yet another issue in that file.
> 
> Oh, I see! No, I was referring to a generic |a-b| expression, not
> abs(3) specifically. Retrospectively, using |a-b| would've been better
> :-)

so wrapping fabs(3) inside a macro, like in the following example might work.

cheers.
alex

ps: just a 60 second hack. ;)

> 
> Cheers,
> 
> --
> Igor
-------------- next part --------------
Index: sys/dev/ath/ath_rate/sample/tx_schedules.h
===================================================================
--- sys/dev/ath/ath_rate/sample/tx_schedules.h	(revision 228164)
+++ sys/dev/ath/ath_rate/sample/tx_schedules.h	(working copy)
@@ -40,6 +40,11 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#define TOLERANCE 0.00001
+
+#define F(_r1, _r2)\
+	((fabs(_r1-_r2) < TOLLERANCE) ? 1 : 0)
+
 #define A(_r) \
     (((_r) == 6)   ? 0 : (((_r) == 9)   ? 1 : (((_r) == 12)  ? 2 : \
     (((_r) == 18)  ? 3 : (((_r) == 24)  ? 4 : (((_r) == 36)  ? 5 : \
@@ -56,7 +61,7 @@
 };
 
 #define NA1(_r) \
-	(((_r) == 6.5)  ? 8 : (((_r) == 13)  ?  9 : (((_r) == 19.5)? 10 : \
+	((F(_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))))))))
 #define NA2(_r) \


More information about the freebsd-wireless mailing list