This weird thing just happened in my code. All of a sudden, a simple addition with reasonably uncomplicated values (4253.95 + 0.9) result in something that looks like a float approximation. Like this:

4253.95 + 0.9 = 4254.84999999999

If I try to reproduce it, it comes out correct with 4254.85, so that's no help, except I can see that the internals of the vars are different (see below)

I can work around/force the correct hehaviour if I stringify the numerical values before doing the addition.

I thought Devel::Peek might discover something strange about this, but I'm not sure how to interpret the output of this piece of code:

my $self = shift; my ($amount) = @_; my $amountOld = $self->amountAccount; my $amountNew = $amountOld + $amount; $self->amountAccount($amountNew); if(length($amountOld) < 8 && length($amountNew) >= 8) { use Devel::Peek; print("\n\n"), Dump($amountOld), Dump($amount), Dump($amountNe +w), print("Went bad here: ($amountOld + $amount = $amountNew)\n\n"); $amount = 0.9; $amountOld = 4253.95; $amountNew = $amountOld + $amount; print("\nNew values\n"), Dump($amountOld), Dump($amount), Dump +($amountNew), print("Correct output: ($amountOld + $amount = $amountN +ew)\n\n"); }

Output:

SV = PVNV(0x2c8ef8c) at 0x23b4958 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,POK,pNOK,pPOK) IV = 4256 NV = 4253.95 PV = 0x2d179a4 "4253.95"\0 CUR = 7 LEN = 35 SV = PVNV(0x2c8ef44) at 0x23b4928 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pIOK,pNOK) IV = 0 NV = 0.9 PV = 0 SV = PVNV(0x2c8efa4) at 0x23b4988 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,POK,pNOK,pPOK) IV = 4309 NV = 4254.84999999999 PV = 0x2d17d9c "4254.84999999999"\0 CUR = 16 LEN = 35 Went bad here: (4253.95 + 0.9 = 4254.84999999999) New values SV = PVNV(0x2c8ef8c) at 0x23b4958 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pNOK) IV = 4256 NV = 4253.95 PV = 0x2d179a4 "4253.95"\0 CUR = 7 LEN = 35 SV = PVNV(0x2c8ef44) at 0x23b4928 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pIOK,pNOK) IV = 0 NV = 0.9 PV = 0x2dfa064 "0.9"\0 CUR = 3 LEN = 35 SV = PVNV(0x2c8efa4) at 0x23b4988 REFCNT = 1 FLAGS = (PADBUSY,PADMY,NOK,pNOK) IV = 4309 NV = 4254.85 PV = 0x2d17d9c "4254.84999999999"\0 CUR = 16 LEN = 35 Correct output: (4253.95 + 0.9 = 4254.85)

As you can see, the flags are different, but I'm not sure what that means. Is it something that is off in the first set?

Is there something else I can look at to understand what's going on here?

OS: w2k

perl -v This is perl, v5.8.3 built for MSWin32-x86-multi-thread (with 8 registered patches, see perl -V for more detail)

Any ideas?

/J


In reply to 4253.95 + 0.9 = 4254.84999999999 (need help to interpret internals) by jplindstrom

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.