ferdi has asked for the wisdom of the Perl Monks concerning the following question:
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!!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Decimal numbers calculations done inconsistently: Odd behavior!
by toolic (Bishop) on Mar 19, 2015 at 15:28 UTC | |
Re: Decimal numbers calculations done inconsistently: Odd behavior!
by atcroft (Abbot) on Mar 19, 2015 at 15:40 UTC | |
Re: Decimal numbers calculations done inconsistently: Odd behavior!
by LanX (Saint) on Mar 19, 2015 at 15:34 UTC | |
Re: Decimal numbers calculations done inconsistently: Odd behavior!
by Athanasius (Archbishop) on Mar 19, 2015 at 15:49 UTC | |
by BillKSmith (Monsignor) on Mar 19, 2015 at 20:20 UTC | |
by Laurent_R (Canon) on Mar 19, 2015 at 22:14 UTC | |
Re: Decimal numbers calculations done inconsistently: Odd behavior!
by syphilis (Archbishop) on Mar 19, 2015 at 23:50 UTC | |
Re: Decimal numbers calculations done inconsistently: Odd behavior!
by Krambambuli (Curate) on Mar 20, 2015 at 09:35 UTC |