Skip to content

Commit 886318f

Browse files
Merge pull request #604 from MasterDuke17/add_docs_for_≅
Add some documentation for the =~=/≅ operator
2 parents be9b155 + f9ebaba commit 886318f

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

doc/Language/operators.pod

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ tightest to loosest:
2929
X Junctive or | ^
3030
L Named unary temp let
3131
N Structural infix but does <=> leg cmp .. ..^ ^.. ^..^
32-
C Chaining infix != == < <= > >= eq ne lt le gt ge ~~ === eqv !eqv
32+
C Chaining infix != == < <= > >= eq ne lt le gt ge ~~ === eqv !eqv =~=
3333
X Tight and &&
3434
X Tight or || ^^ // min max
3535
R Conditional ?? !! ff fff
@@ -1392,6 +1392,45 @@ Here is an excerpt of built-in smart-matching functionality:
13921392
13931393
=end table
13941394
1395+
=head2 infix C«=~=»
1396+
1397+
proto sub infix:<=~=>($, $) returns Bool:D is assoc:<chain>
1398+
multi sub infix:<=~=>(Any, Any)
1399+
multi sub infix:<=~=>(Int:D, Int:D)
1400+
multi sub infix:<=~=>(Num:D, Num:D)
1401+
multi sub infix:<=~=>(Rational:D, Rational:D)
1402+
multi sub infix:<=~=>(Real:D, Real:D)
1403+
multi sub infix:<=~=>(Complex:D, Complex:D)
1404+
multi sub infix:<=~=>(Numeric:D, Numeric:D)
1405+
1406+
The X<approximately-equal operator>. Calculates the relative difference between
1407+
the left-hand and right-hand sides and returns C<True> if the difference is
1408+
less than $*TOLERANCE (which defaults to 1e-15). However, if either side is zero
1409+
then it checks that the absolute difference between the sides is less than $*TOLERANCE.
1410+
Note that this operator is not arithmetically symetrical (doesn't do ± Δ):
1411+
1412+
say ($x + $*TOLERANCE) =~= $x; # True
1413+
say ($x - $*TOLERANCE) =~= $x; # False
1414+
1415+
The tolerance is supposed to be modifiable via an adverb:
1416+
1417+
say $x =~= $y :tolerance(.1);
1418+
1419+
however, this is not yet implemented. The same effect can be achieved by
1420+
assigning to $*TOLERANCE.
1421+
1422+
{
1423+
my $*TOLERANCE = .1;
1424+
say 11 =~= 10; # True
1425+
}
1426+
1427+
Note that setting $*TOLERANCE = 0 will cause all comparisons to fail.
1428+
1429+
{
1430+
my $*TOLERANCE = 0;
1431+
say 1 =~= 1; # False
1432+
}
1433+
13951434
=head1 Tight AND Precedence
13961435
13971436
=head2 infix C«&&»

0 commit comments

Comments
 (0)