infix =~=
Documentation for infix =~=
assembled from the following types:
language documentation Operators
From Operators
(Operators) infix =~=
multi sub infix:<=~=>(Any, Any) multi sub infix:<=~=>(Int:D, Int:D) multi sub infix:<=~=>(Num:D, Num:D) multi sub infix:<=~=>(Rational:D, Rational:D) multi sub infix:<=~=>(Real:D, Real:D) multi sub infix:<=~=>(Complex:D, Complex:D) multi sub infix:<=~=>(Numeric:D, Numeric: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) = 42, 42.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» }