http://qs1969.pair.com?node_id=300157

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
    .......