#!/usr/bin/perl -w

use 5.034;

use Benchmark qw(cmpthese);

my ($fvar, $mvar, $bvar);

my $j;
for my $i (1..1000){
    $j = rand();
    $fvar .= f($j);
    $mvar .= mm($j); # note that m, s and y are REGEX in perl, so beware of cheap namesubs 
    $bvar .= b($j);
}

say $fvar;
say "";
say $mvar;
say "";
say $bvar;
say "";

die "No match" unless ($mvar eq $bvar);   # the 'good' ones

cmpthese(-3, {
		 "function" => sub {
		     srand(12345);
		     for (1 .. 1000){
			 $fvar = f(rand());
		     }
		 },
		 "module2" => sub {
		     srand(12345);
		     for (1 .. 1000){
			 $mvar = mm(rand());
		     }
		 },
		 "bitand1" => sub {
		     srand(12345);
		     for (1 .. 1000){
			 $bvar = b(rand());
		     }
		 },
	     });

exit 2;

sub f {     # binary switch function from 999 (2022)
    my $x = shift;
    $x -= 0.5;
    my $y = abs($x) - $x;
    if ($y != 0) {
	return ($y-$y)/(2*$y);
    }
    return 1;  # rare case
}

sub mm {
    my $r = shift;
    my $x = int( abs($r) * 100_000_000 );
    return ($x % 2);
}

sub b {
    my $r = shift;
    my $x = int( abs($r) * 100_000_000 );
    return 1 if ($x & 1);
    return 0;
}

__END__
  
Note that f() does not the same as the rest, so could be of interest
  
