neo1491:

Unless I'm grossly misunderstanding your question, you should give your function a better name, as "round" implies a well-known set of behaviors: round up, round down, round nearest. With your extra requirements, a future maintainer could easily be led astray by the name.

From your description, you want a number to be rounded down to the nearest integer with the additional requirements of:

(1) never returning a number greater than 16, and
(2) never returning 0.

So RoundDownSpecial might be a better name.

Also, you didn't specify what should be returned instead of 0 in the cases that normal rounding down would return 0. Other requirements are also unspecified, so you could get an entire zoo of behaviors. My candidate for your zoo:

#!/usr/bin/perl -w use strict; use warnings; sub RoundDownSpecial_bizarro { my $num = shift; my $den = shift; return 7 unless $den+0.0; return 8 unless $num+0.0; my $tmp = int(($num+0.0) / $den); return 16 if $tmp > 16; return 16 if $tmp < 1; return int($tmp); } while (<DATA>) { chomp; my ($n, $d) = split /,/; print join(" ",$n, $d, RoundDownSpecial_bizarro($n, $d)), "\n" +; } __DATA__ 0, 1 10, 0 100, 1 1, 100 2, 1 8, 1 15, 1 16, 1 17, 1
...roboticus

Mechanical Pedant

UPDATE: Apparently I'm not quite pedantic enough this morning. I missed some parenthesis around the '$num+0.0' clause. Thanks for the catch, FunkyMonk!


In reply to Re: Round Down Routine by roboticus
in thread Round Down Routine by neo1491

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.