Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: backwards if and while looping

by Zed_Lopez (Chaplain)
on Oct 01, 2004 at 23:18 UTC ( #395780=note: print w/replies, xml ) Need Help??

in reply to Re: backwards if and while looping
in thread backwards if and while looping

There's another case to account for... if $a or $b isn't integer, and their fractional parts differ (i.e. ($a - int $a) != ($b - int $b)) and $a < $b, then

$a = $b unless $error or $a >= $b;

(or similar examples in the thread) has a different result than the OP's code.

I think it muddies the water to test $error every time through the loop, as some of these solutions do, when you only need to test it once. If $a or $b might be non-integer, here's one way to do it:

if ($a < $b and !$error) { $a = int $b + ($a - int $a); $a++ if $a < $b; }

I left out doing the floating point comparisons correctly...

Updated: Fixed a dumb error in the code.

Replies are listed 'Best First'.
Re^3: backwards if and while looping
by !1 (Hermit) on Oct 02, 2004 at 00:28 UTC
    Yeah, but who uses real numbers? ;-P

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (None)
    As of 2021-10-19 00:33 GMT
    Find Nodes?
      Voting Booth?
      My first memorable Perl project was:

      Results (76 votes). Check out past polls.