Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: sub as mathematical function

by holli (Abbot)
on Sep 04, 2019 at 11:38 UTC ( #11105579=note: print w/replies, xml ) Need Help??


in reply to sub as mathematical function

sub quant{ # 14 characters, but I am bad at this, I'm sure it can get shorter int((shift)*8) }
Update:
Hah. I made the same mistake as LanX above. It works up to 8/9, then it fails.

sub quant{ int((shift)*8) } for ( 1 .. 9 ) { print "$_ => ", quant( ($_/9) - 0.0001 ), "\n"; }
1 => 0 2 => 1 3 => 2 4 => 3 5 => 4 6 => 5 7 => 6 8 => 7 9 => 7 // BOOM!
What do we learn? Test all cases :-)


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: sub as mathematical function
by ikegami (Pope) on Sep 04, 2019 at 16:29 UTC

    It also fails for a lot of numbers between the ones you tested (e.g. 0.112 should return 1 but returns 0, 0.23 should return 2 but returns 1, etc).

    The fix is simple: Replace 8 with 9 :)

    Of course, that assumes the input is in range [0..1), but that seems a sure thing.

Log In?
Username:
Password:

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

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

    No recent polls found

    Notices?