Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: sub as mathematical function

by ikegami (Pope)
on Sep 04, 2019 at 16:20 UTC ( #11105609=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11105609]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2021-04-17 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?