Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Rounding values upwards on an arbitrary basis

by BrowserUk (Patriarch)
on Jul 02, 2003 at 21:35 UTC ( [id://270974]=note: print w/replies, xml ) Need Help??


in reply to Rounding values upwards on an arbitrary basis

My contribution to the fray.

Update:Removed spurious hash left over from an earlier attempt.

#! perl -slw use strict; sub categorise { use POSIX qw[log10]; my $n = shift; die 'Argument less than 1' unless $n > 0; return 5 if $n <= 5; return 10 if $n <=10; return 5 * int( $n / 5 + 1 ) if $n <= 100; my $scale = int( log10( $n ) ); return (0+"1e$scale") * 0.25 * ( 1 + int( ( $n / (0+"1e$scale") ) / 0.25 ) ); } my $t = 2.4; for( 1 .. 20 ) { $t *= 1.55; printf "%5d : %5d\n", int($t), categorise( int( $t ) ); } =pod comment Expose for a more thorough and randomised test. printf "%5d : %5d\n", int($_), categorise( int( $_ ) ) for 1..15, ( map{ 10* ($_ + rand) } 2 .. 9 ), ( map{ 100* ($_ + rand) } 1 .. 9 ), ( map{ 1000* ($_ + rand) } 1 .. 9 ); =cut __END__ P:\>270920 3 : 5 5 : 5 8 : 10 13 : 15 21 : 25 33 : 35 51 : 55 79 : 80 123 : 125 192 : 200 297 : 300 461 : 475 715 : 725 1108 : 1250 1718 : 1750 2663 : 2750 4129 : 4250 6400 : 6500 9920 : 10000 15376 : 17500

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found