I wrote a perl program that does some calculation on decimal numbers.

My input data looks like this:

............

20120620 093100 100000 84.02 84.09 83.59 83.60

20120621 093100 100000 80.67 80.96 80.35 80.43

20120622 093100 100000 78.71 79.10 78.43 79.09

20120625 093100 100000 79.04 79.09 78.28 78.43

............

I create an array (say @arr) that has each number from a line like above as its elements. Then I define my variables as follows:

$open= $arr[3];$high=$arr[4];$low=$arr[5];$close=$arr[6]; Then I do some calculations using these: The chunk of the code that does the calculation is: if ($close<$open) { $stretch=(($high*100)-($open*100)); $stretcher=(($open*100)-($low*100)); $identifier="Red"; } if ($close>$open) { $stretch=(($open*100)-($low*100)); $stretcher= (($high*100)-($open*100)); $identifier="Green"; } if ($close==$open) { $stretch=(($high*100)-($open*100)); $stretcher=(($open*100)-($low*100)); $identifier="Doji"; } print "$date $time1 $time2 $stretch $stretcher $identifier $open $high + $low $close\n";

My output is like this:

20120620 093100 100000 7 43 Red 84.02 84.09 83.59 83.60

20120621 093100 100000 28.9999999999991 32.0000000000009 Red 80.67 80.96 80.35 80.43

20120622 093100 100000 27.9999999999982 39 Green 78.71 79.10 78.43 79.09

20120625 093100 100000 4.99999999999909 76.0000000000009 Red 79.04 79.09 78.28 78.43

My questions are:

1) The first line of output is how I want each line of my output as. But that isnt so. Although the numbers that I am using for calculation in each of the above lines are almost similar, only the first line is giving me exact output. I want to know why this inconsistency is occurring here and how to rectify the errors.

2) It is unlikely that the error is because of the inherent issues with using decimal numbers in calculations since if that were the case, the first line of output must have been odd too. But that isnt the case. What exactly in perl causes this inconsistency? If it helps, how many ever times I run the program, the first line given here doesnt get odd, but the other lines always remain odd!

3) Of the numbers where output is odd, some have .99999999999 added and others have .00000000001 added. If I can at least make perl to give me numbers that are all appended with one among .999999999 or .000000000001, I will be okay since I can use ceiling or flooring function on the numbers.

Please help!!


In reply to Decimal numbers calculations done inconsistently: Odd behavior! by ferdi

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.