infix =~=

Documentation for infix =~= assembled from the following types:

language documentation Operators

From Operators

(Operators) infix =~=

multi sub infix:<=~=>(AnyAny)
multi sub infix:<=~=>(Int:DInt:D)
multi sub infix:<=~=>(Num:DNum:D)
multi sub infix:<=~=>(Rational:DRational:D)
multi sub infix:<=~=>(Real:DReal:D)
multi sub infix:<=~=>(Complex:DComplex:D)
multi sub infix:<=~=>(Numeric:DNumeric:D)

The approximately-equal operator , whose ASCII variant is =~=, calculates the relative difference between the left-hand and right-hand sides and returns True if the difference is less than $*TOLERANCE (which defaults to 1e-15). However, if either side is zero then it checks that the absolute difference between the sides is less than $*TOLERANCE. Note that this operator is not arithmetically symmetrical (doesn't do ± Δ):

my $x = 1;
say ($x + $*TOLERANCE=~= $x;   # OUTPUT: «False␤» 
say ($x - $*TOLERANCE=~= $x;   # OUTPUT: «True␤»

The tolerance is supposed to be modifiable via an adverb:

my ($x$y= 4242.1;
say $x =~= $y :tolerance(.1);

However, this is not yet implemented. The same effect can be achieved by assigning to $*TOLERANCE.

{
    my $*TOLERANCE = .1;
    say 11 =~= 10;        # OUTPUT: «True␤» 
}

Note that setting $*TOLERANCE = 0 will cause all comparisons to fail.

{
    my $*TOLERANCE = 0;
    say 1 =~= 1;          # OUTPUT: «False␤» 
}