My code is doing something I really can't understand. I'm performing some linear interpolation, and test the result against the number 75. I can get two different results depending on where the &adjust_hue subroutine is called. Here's the code
use strict; use constant HUE_RANGE => 255; my @HUES = ( 0, 25, 50, 75, 120, 168, 195, 240, + 268, 315); # 0.0 0.1 0.2 0.3 0.4 0.5 + 0.6 0.7 0.8 0.9 1.0 &adjust_hue(0.3); my @hues = (0 .. 4); $_ *= 0.1 for @hues; for (@hues) { &adjust_hue($_); } sub adjust_hue { my ($scale) = @_; my @hues = @HUES; push @hues, HUE_RANGE unless $hues[-1] == HUE_RANGE; # warn 'hues ', Dumper \@hues; # warn "n hues ", (scalar @hues), "\n"; my $n_unique_hues = -1 + scalar @hues; my $scaled_index = $scale*$n_unique_hues; my $index = int $scaled_index; my $adjusted_hue = $hues[$index] + ($scaled_index - $index)*($hues +[$index + 1] - $hues[$index]); # $adjusted_hue = eval $adjusted_hue; # This shouldn't be necessary +. Somehow this sub was producing strings instead of numbers. Later, t +his strings would fail comparisons. if ($adjusted_hue == 75) { warn " adjusted_hue ($adjusted_hue) equals 75\n"; } else { warn " adjusted_hue ($adjusted_hue) does not equal 75\n"; } warn "scale $scale, scaled_index $scaled_index, index $index, adju +sted_hue $adjusted_hue\n\n"; $adjusted_hue; }
The output is the following for me:
adjusted_hue (75) equals 75 scale 0.3, scaled_index 3, index 3, adjusted_hue 75 adjusted_hue (0) does not equal 75 scale 0, scaled_index 0, index 0, adjusted_hue 0 adjusted_hue (25) does not equal 75 scale 0.1, scaled_index 1, index 1, adjusted_hue 25 adjusted_hue (50) does not equal 75 scale 0.2, scaled_index 2, index 2, adjusted_hue 50 adjusted_hue (75) does not equal 75 scale 0.3, scaled_index 3, index 3, adjusted_hue 75 adjusted_hue (120) does not equal 75 scale 0.4, scaled_index 4, index 4, adjusted_hue 120
I really don't understand why the comparison to 75 is true in one case with the parameter 0.3 and false also with the parameter 0.3 Thanks, John

In reply to Why is this comparison failing? by johnrcomeau

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.