Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

ceil without ceil

by russmann (Initiate)
on Oct 17, 2003 at 20:52 UTC ( #300157=perlmeditation: print w/replies, xml ) Need Help??

This is a comment on the FAQ Does perl have a round function? What about ceil() and floor()? Trig functions?. See also vroom's How do I round a floating point number?

An effective way to "ceil" if you don't have a perl version capable of "ceil" (such as a specialized small Windows distro), you can effectively "ceil" this way: $ceiled_number = int( $float + 0.99999999 );

Replies are listed 'Best First'.
Re: ceil without ceil
by why_bird (Pilgrim) on Mar 13, 2008 at 14:48 UTC
    I think this would be better:
    sub RoundUp { $float=$_[0]; ... ..check that your input is valid here.. ... my $rounded_number=$float; if($float==int($float)){ }elsif($float>0){ $rounded_number=int($float+1); ################################### ## and optionally, depending how ## ## you want to define ceil() on ## ## a number less than 0.. ## }elsif($float<0){ $rounded_number=int($float-1); } ################################### return rounded_number; }
    The code above rounds any number smaller than 9e-9 incorrectly to 0. For a lot of applications I suppose this won't make a difference, but why write a mathematical function that is not correct? It might be copied by someone else who assumes that it will work correctly in every circumstance*.
    *Yes it would be a stupid assumption to make, but you can never tell..
    waits for someone to point out the error in her method
    why_bird
    ........
    Those are my principles. If you don't like them I have others.
    -- Groucho Marx
    .......

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2023-09-27 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?