Skip to content

Commit

Permalink
near: fix for negative inputs (SF#3092250)
Browse files Browse the repository at this point in the history
The formula used for 'within a factor of' was only correct for
positive numbers.
  • Loading branch information
jepler committed Oct 25, 2010
1 parent b2f01c3 commit 1b5432c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/hal/components/near.comp
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
component near "Determine whether two values are roughly equal.";
pin in float in1;
pin in float in2;
pin in float in1_;
pin in float in2_;
param rw float scale=1;
param rw float difference=0;
pin out bit out "true if in1/scale <= in2 <= in1*scale, OR |in1-in2| <= difference";
pin out bit out """
\\fBout\\fR is true if \\fBin1\\fR and \\fBin2\\fR are within a factor of
\\fBscale\\fR (i.e., for in1 positive, in1/scale <= in2 <= in1*scale), OR
if their absolute difference is no greater than \\fBdifference\\fR (i.e.,
|in1-in2| <= difference). \\fBout\\fR is false otherwise.""";
function _;
license "GPL";
;;
#include "rtapi_math.h"
FUNCTION(_) {
double in1 = in1_, in2 = in2_;
if(in1 < 0) {
in1 = -in1;
in2 = -in2;
}
if((scale > 1 && in1/scale <= in2 && in2 <= in1*scale) || fabs(in1-in2) <= difference)
out = 1;
else
Expand Down

0 comments on commit 1b5432c

Please sign in to comment.