$number wasn't guaranteed to be a whole number (and I guess that makes % fail)
No, Perl is OK with that:
3:03 >perl -MData::Dump -wE "my @array; $array[5.35968 % 2] = 42; dd
+\@array;"
[undef, 42]
3:03 >
But the original loop almost certainly wasn’t doing what you thought it was. With an initial value of 192:
3:03 >perl -wE "my $iterations = 0; my $number = 192; while ($number
+!= 0) { $number /= 2; ++$iterations; } say qq[$iterations iterations]
+;"
1083 iterations
3:06 >
The loop terminates when $number is zero, but (on my Perl, which is 64-bit) this test doesn’t succeed until division by two has reduced $number all the way down to half of 4.94065645841247 × 10-324 — which is a very small number!
Hope that helps,
-
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.
|