I have been banging my head against the wall and thought i should find some help. This is some really basic addition and subtraction that should equal 0.

#!/usr/bin/perl use strict; my $first_num = '-3500.78'; my $second_num = '-1057.12'; my $third_num = '5100'; my $fourth_num = '-681'; my $fifth_num = '46.3'; my $sixth_num = '46.3'; my $seventh_num = '46.3'; print "FIRST NUMBER = $first_num\n"; print "SECOND NUMBER = $second_num\n"; print "THIRD NUMBER = $third_num\n"; print "FOURTH NUMBER = $fourth_num\n"; print "FIFTH NUMBER = $fifth_num\n"; print "SIXTH NUMBER = $sixth_num\n"; print "SEVENTH NUMBER = $seventh_num\n"; my $total = $first_num + $second_num + $third_num + $fourth_num + $fif +th_num + $sixth_num + $seventh_num; print "TOTAL = $total\n\n"; #BREAK IT DOWN my $first_total = $first_num + $second_num; print "#1: $first_num + $second_num = $first_total\n"; my $second_total = $first_total + $third_num; print "#2: $first_total + $third_num = $second_total\n"; my $third_total = $second_total + $fourth_num; print "#3: $second_total + $fourth_num = $third_total\n"; my $fourth_total = $third_total + $fifth_num; print "#4: $third_total + $fifth_num = $fourth_total\n"; my $fifth_total = $fourth_total + $sixth_num; print "#5: $fourth_total - $sixth_num = $fifth_total\n"; my $sixth_total = $fifth_total + $seventh_num; print "#6: $fifth_total + $seventh_num = $sixth_total\n";

Here are the results of the program:

FIRST NUMBER = -3500.78
SECOND NUMBER = -1057.12
THIRD NUMBER = 5100
FOURTH NUMBER = -681
FIFTH NUMBER = 46.3
SIXTH NUMBER = 46.3
SEVENTH NUMBER = 46.3
TOTAL = 3.5527136788005e-13

#1: -3500.78 + -1057.12 = -4557.9 (correct)
#2: -4557.9 + 5100 = 542.1 (correct)
#3: 542.1 + -681 = -138.9 (correct)
#4: -138.9 + 46.3 = -92.5999999999996 (WHAT?)
#5: -92.5999999999996 - 46.3 = -46.2999999999996 (WHAT?)
#6: -46.2999999999996 + 46.3 = 3.5527136788005e-13 (WHAT?)

I have a program that goes out and adds a budget up and kept encountering this problem. I decided to simplify it and break it down to try to find where the issue is. Been a long day and think my brains are just leaking out of my head. Thanks in advance, -vptimmy

In reply to Simple addition/subtraction not working by vptimmy

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.