Skip to content

Commit 9f6a8b1

Browse files
authored
Merge pull request #2525 from snowgoer540/gregc/floating-point
interp: fix GE/LE operators to handle double-precision comparison
2 parents 02a597e + 014777a commit 9f6a8b1

3 files changed

Lines changed: 12 additions & 13 deletions

File tree

docs/src/gcode/overview.adoc

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,11 +704,10 @@ false, and any non-zero number is equivalent to logical true.
704704

705705
== Equality and floating-point values
706706

707-
The RS274/NGC language only supports floating-point values of finite
708-
precision. Therefore, testing for equality or inequality of two
709-
floating-point values is inherently problematic. The interpreter
710-
solves this problem by considering values equal if their absolute
711-
difference is less than 0.0001 (this value is defined as
707+
Testing for equality or inequality of two
708+
double-precision floating-point values is inherently problematic.
709+
The interpreter solves this problem by considering values equal if
710+
their absolute difference is less than 1e-6 (this value is defined as
712711
'TOLERANCE_EQUAL' in src/emc/rs274ngc/interp_internal.hh).
713712

714713
[[gcode:functions]]

src/emc/rs274ngc/interp_execute.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,20 +158,20 @@ int Interp::execute_binary2(double *left, //!< pointer to the left operan
158158
*left = (*left < *right) ? 1.0 : 0.0;
159159
break;
160160
case EQ:
161-
diff = *left - *right;
162-
diff = (diff < 0) ? -diff : diff;
161+
diff = fabs(*left - *right);
163162
*left = (diff < TOLERANCE_EQUAL) ? 1.0 : 0.0;
164163
break;
165164
case NE:
166-
diff = *left - *right;
167-
diff = (diff < 0) ? -diff : diff;
165+
diff = fabs(*left - *right);
168166
*left = (diff >= TOLERANCE_EQUAL) ? 1.0 : 0.0;
169167
break;
170168
case LE:
171-
*left = (*left <= *right) ? 1.0 : 0.0;
169+
diff = fabs(*left - *right);
170+
*left = ((diff < TOLERANCE_EQUAL) || (*left <= *right)) ? 1.0 : 0.0;
172171
break;
173172
case GE:
174-
*left = (*left >= *right) ? 1.0 : 0.0;
173+
diff = fabs(*left - *right);
174+
*left = ((diff < TOLERANCE_EQUAL) || (*left >= *right)) ? 1.0 : 0.0;
175175
break;
176176
case GT:
177177
*left = (*left > *right) ? 1.0 : 0.0;

src/emc/rs274ngc/interp_internal.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ Tighter tolerance down to a minimum of 1 micron +- also accepted.
8787
#define SPIRAL_RELATIVE_TOLERANCE 0.001
8888

8989
/* angle threshold for concavity for cutter compensation, in radians */
90-
#define TOLERANCE_CONCAVE_CORNER 0.05
91-
#define TOLERANCE_EQUAL 0.0001 /* two numbers compare EQ if the
90+
#define TOLERANCE_CONCAVE_CORNER 0.05
91+
#define TOLERANCE_EQUAL 1e-6 /* two numbers compare EQ if the
9292
difference is less than this */
9393

9494
static inline bool equal(double a, double b)

0 commit comments

Comments
 (0)