📘 Compare numbers approximately using Perl 6

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s