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
.......
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|