Specifically, I'm running into a lot of NaN values when I do some basic math. I thought that I was getting bad values out of the database in my code so I started forcing the "cast" by adding zero or multiplying by 1. One of the values coming out of my db in this code is '109.98'. I can print it out as 109.98 but any time I add, subtract or multiply anything to/by it, the result is always NaN. I thought that perhaps it was being seen in string context but I haven't found a way to force it to float context. My code looks something like this:
my $retail_rate = $self->{'TOUR'}->{'RATE'}{$rateid}{'retail_rate'}; my $coupon = $self->{COUPONAMOUNT}; my $discounted_rate = $retail_rate - $coupon; print "$retail_rate $coupon $discounted_rate\n";
which gets me "109.98 2 NaN" Same if I add a bunch of '+=0' statements, like so:
my $retail_rate = $self->{'TOUR'}->{'RATE'}{$rateid}{'retail_rate'}; my $coupon = $self->{COUPONAMOUNT}; $retail_rate += 0; $coupon += 0; my $discounted_rate = $retail_rate - $coupon; print "$retail_rate $coupon $discounted_rate\n";
It's still "109.98 2 NaN" Even when I don't subtract $coupon, I'm still ending up with NaN values.
my $retail_rate = $self->{'TOUR'}->{'RATE'}{$rateid}{'retail_rate'}; my $coupon = $self->{COUPONAMOUNT}; $retail_rate += 0; $coupon += 0; my $discounted_rate = $retail_rate + 1; print "$retail_rate $coupon $discounted_rate\n";
It's still "109.98 2 NaN" But get this:
my $retail_rate = $self->{'TOUR'}->{'RATE'}{$rateid}{'retail_rate'}; my $coupon = $self->{COUPONAMOUNT}; $retail_rate += 0; $coupon += 0; my $discounted_rate = $retail_rate; $discounted_rate++; print "$retail_rate $coupon $discounted_rate\n";
gives me "109.98 2 110.98." But even then adding onto $discounted_rate sends me back to NaN land. 12 years coding Perl and somehow I've forgotten basic addition. It's time for me to put in an application an Burger King, right?

In reply to Getting different results with $var++ and $var += 1 by splicer

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.