Best Practice for IF Functions in Equations

When you use the IF function and assign a value to the Global Variable or the value to compare with, you may get an unexpected result because of the floating-point math done by equations.

SOLIDWORKS uses floating-point math to compare values. For example, if you specify a Global Variable of 2.2, SOLIDWORKS might evaluate it as 2.2000001 or 2.1999998. In such cases, the comparison returns 0 (False) instead of 1 (True).

To avoid this issue, use a tolerance value in your comparison equation. In this example, you create a tolerance of 0.0000001.

Under Value/Equations, create the IF statement as follows:

= iif ( abs ("GV" - 2.2) <= "tolerance", 1, 0)

In the comparison IF function, Test with tolerance, the positive absolute value of the Global Variable GV minus itself is less than or equal to the tolerance, so the equation Evaluates to 1, the expected result.
Note that the Test without tolerance unexpectedly Evaluates to 0.

You use the abs() (absolute) function to get the positive value of the subtraction operation to compare to the tolerance. If one or both of the comparators in the equation have a negative value, use the abs() function to obtain the desired logic.