Documentation Center

  • Trial Software
  • Product Updates

~=, _approx

Approximate equality

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

x ~= y
_approx(x, y)

Description

x ~= y symbolizes approximate equality.

x ~= y is equivalent to the function call _approx(x, y).

The operator ~= returns a symbolic expression representing an approximate equality for numerical values x and y. The calls bool(x ~= y) and is(x ~= y) check whether |float((x - y)/x)| < 10^(-DIGITS) is satisfied, provided x ≠ 0 and y ≠ 0. Thus, TRUE is returned if x and y coincide within the relative numerical precision set by DIGITS. For x = 0, the criterion is |float(y)| < 10^(-DIGITS). For y = 0, the criterion is |float(x)| < 10^(-DIGITS). If either x or y contains a symbolic object that cannot be converted to a real or complex floating point number, the functions bool and is return the value UNKNOWN.

Approximate equalities have two operands: the left hand side and the right hand side. One may use lhs and rhs to extract these operands.

    Note:   a ~= b is not equivalent to a - b ~= 0.

Examples

Example 1

In the following, note the difference between syntactical and numerical equality. The numbers 1.5 and coincide numerically. However, 1.5 is of domain type DOM_FLOAT, whereas is of domain type DOM_RAT. Consequently, they are not regarded as equal in the following syntactical test:

1.5 = 3/2; bool(%)

If floating-point numbers are involved, one should rather use the operator ~= instead of =. The functions bool and is test whether the floating-point approximations coincide up to the relative precision given by DIGITS:

1.5 ~= 3/2; 
bool(1.5 ~= 3/2);
is(1.5 ~= 3/2);

The following expressions coincide syntactically:

_equal(1/x, diff(ln(x),x)); bool(%)

The Boolean operator not converts equalities and inequalities:

not a = b, not a <> b

Example 2

The examples below demonstrate how = and <> deal with non-mathematical objects and data structures:

if "text" = "t"."e"."x"."t" then "yes" else "no" end

bool(table(a = PI) <> table(a = sqrt(2)))

Example 3

We demonstrate the difference between the syntactical test via bool and the semantical test via testeq:

bool(1 = x/(x + y) + y/(x + y)), testeq(1 = x/(x + y) + y/(x + y))

Example 4

Equations and inequalities are typical input objects for system functions such as solve:

solve(x^2 - 2*x = -1, x)

solve(x^2 - 2*x <> -1, x)

Parameters

x, y

Arbitrary MuPAD® objects

Return Values

Expression of type "_approx".

See Also

MuPAD Functions

Was this topic helpful?