in reply to sub as mathematical function

Assuming $v is in [0..1)

sub quant { my $v = shift; return int($v*9); }

Replies are listed 'Best First'.
Re^2: sub as mathematical function
by vr (Curate) on Sep 04, 2019 at 21:04 UTC

    Measure (and cost) of returning "exactly the same" result unspecified, a pedant would note it's pure luck and coincidence that for 9 intervals these functions indeed seem to always return the same. For a simple case of 6 intervals:

    ~$ perl -E 'say 4 if 0.8333333333333333 < 5/6' 4 ~$ perl -E 'say int 6 * 0.8333333333333333' 5

    For maybe general case of 100 intervals, playing with Data::Float (and from similar experiment literal number above came from):

    ~$ perl -MData::Float=nextdown -E '$n=100; for(1..$n){say if $_-1 != i +nt $n*nextdown $_/$n}' 5 10 17 20 23 34 40 46 67 68 80 81 92 93