in reply to IF condition with a range

... "If the number is exactly equal, or equal+1 or equal-1".

Your problem statement seems to assume integer operations: "If the number is exactly equal, or exactly equal+1 or exactly equal-1." The tests given by ikegami here are not quite equivalent to your OPed test if the numbers are not integers: (update: ikegami already assumed integers in his reply, but the following examples are still pertinent.) | If the numbers are not integers:

c:\@Work\Perl\monks>perl -wMstrict -le "my $x = 2; my $y = 2.00000000000000123; print qq{$x $y}; ;; if ($x == $y or $x == $y-1 or $x == $y+1) { print 'in exact range'; } if ($y-1 <= $x && $x <= $y+1) { print 'in <= range'; } if (abs($y-$x) <= 1) { print 'in abs range'; } " 2 2 in <= range in abs range
Remember that Perl numbers are essentially floating-point doubles. This may introduce some twists and pitfalls. (Update: Certain combinations of operations may produce a number like 2.00000000000000123 that may appear, in certain circumstances, to be an integer, but isn't. See What Every Computer Scientist Should Know About Floating-Point Arithmetic.) One way to force integer operations is with the integer pragma:
c:\@Work\Perl\monks>perl -wMstrict -le "use integer; ;; my $x = 2; my $y = 2.00000000000000123; print qq{$x $y}; ;; if ($x == $y or $x == $y-1 or $x == $y+1) { print 'in exact range'; } if ($y-1 <= $x && $x <= $y+1) { print 'in <= range'; } if (abs($y-$x) <= 1) { print 'in abs range'; } " 2 2 in exact range in <= range in abs range

Update: Changed both code examples to use the floating-point number 2.00000000000000123 (vice 2.5) for $y.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: IF condition with a range
by Tux (Canon) on Jul 16, 2018 at 15:43 UTC

    There are longdoubles and quadmath. With my perl (built with -Duselongdouble) I get:

    2 2.00000000000000123 in exact range in <= range in abs range

    Enjoy, Have FUN! H.Merijn

      Is this with or without the use of integer?


      Give a man a fish:  <%-{-{-{-<

        with:

        $ cat test.pl use 5.18.2; use warnings; use integer; my $x = 2; my $y = 2.00000000000000123; say qq{$x $y}; if ($x == $y or $x == $y-1 or $x == $y+1) { say "in exact range"; } if ($y-1 <= $x && $x <= $y+1) { say "in <= range"; } if (abs ($y-$x) <= 1) { say "in abs range"; } $ perl -v | grep 5.2 This is perl 5, version 24, subversion 1 (v5.24.1) built for x86_64-li +nux-thread-multi-ld $ perl test.pl 2 2.00000000000000123 in exact range in <= range in abs range

        Same on 5.28.0

        2.0000000000000000123 (two more zeroes), yields

        2 2.00000000000000001 in exact range in <= range in abs range

        Enjoy, Have FUN! H.Merijn
Re^2: IF condition with a range (updated)
by syphilis (Archbishop) on Jul 16, 2018 at 23:54 UTC
    if (abs($y-$x) <= 1)

    It's perhaps worth noting that, with floats, if $x == $y it does not necessarily follow that $x - $y == 0, or that abs($x - $y) <= 1
    If $x and $y are both infinities (of the same sign) then the first condition holds true, while the second and third are false.

    Cheers,
    Rob
Re^2: IF condition with a range
by ikegami (Patriarch) on Jul 16, 2018 at 16:45 UTC

    The tests given by ikegami here are not quite equivalent to your OPed test if the numbers are not integers:

    I've already said as much in my post.

      Oops... I missed that on my first reading.


      Give a man a fish:  <%-{-{-{-<