📘 Compare numbers approximately using Perl 6

📘 Compare numbers approximately using Raku

N. B. Perl 6 has been renamed to Raku. Click to read more.

Compare the two non-integer values approximately.

Comparing non-integer numbers, which are represented as floating-point numbers is often a task that requires approximate comparison.

In Perl 6, there is the =~= operator, called the approximately-equal operator, which checks if its operands are close enough to each other.

say 1/1000 =~= 1/1001;         # False
say 1/1E20 =~= 1 / (1E20 + 1); # True

The result of the approximate comparison is True if the difference is less than the value set in the $*TOLERANCE constant, which is equal to 1E-15.

Notice that in Perl 6, a number in a scientific notation is a floating-point number, while other representations, such as 0.5 or 1/2, or <1/2>, or even ½, are the values of the Rat type (Rat stands for rational).

Rational numbers are stored as two integer values, the numerator and the denominator. Computations with such numbers, therefore, do not loose accuracy. Compare the results of the following classical example, with a similar program in other languages:

say 0.1 + 0.2 - 0.3; # 0

Perl 6 prints an exact zero, even if you try to print the result with many digits after the decimal point:

'%.20f'.printf(0.1 + 0.2 - 0.3); # 0.00000000000000000000

This is very nice of Perl 6, isn’t it?

Leave a Reply

Your email address will not be published. Required fields are marked *