near.comp
1 component near "Determine whether two values are roughly equal."; 2 pin in float in1_; 3 pin in float in2_; 4 param rw float scale=1; 5 param rw float difference=0; 6 pin out bit out """ 7 \\fBout\\fR is true if \\fBin1\\fR and \\fBin2\\fR are within a factor of 8 \\fBscale\\fR (i.e., for in1 positive, in1/scale <= in2 <= in1*scale), OR 9 if their absolute difference is no greater than \\fBdifference\\fR (i.e., 10 |in1-in2| <= difference). \\fBout\\fR is false otherwise."""; 11 function _; 12 license "GPL"; 13 ;; 14 #include "rtapi_math.h" 15 FUNCTION(_) { 16 double in1 = in1_, in2 = in2_; 17 if(in1 < 0) { 18 in1 = -in1; 19 in2 = -in2; 20 } 21 if((scale > 1 && in1/scale <= in2 && in2 <= in1*scale) || fabs(in1-in2) <= difference) 22 out = 1; 23 else 24 out = 0; 25 }