Hi monks,
i know perl is not aim at doing calculation at first, but here is a simple thing i coded, and the result is quite strange, could someone explain me why , and if there is a solution to avoid this, it would be welcomed !
I just add recursively 0.1 to a number, and sometime i got more than 1 decimal !
here is the code, following by the output :
#!/usr/bin/perl -w
use strict;
use warnings;
my $bou = 20;
my $beu = 0.1;
my $nb=$bou/$beu;
my(@bu);
$bu[0]=0;
for (my $i=1;$i<=2*$nb;$i++){
$bu[$i]=$bu[$i-1]+$beu;
print "$i\t$bu[$i]\n";
}
and it gives :
1 0.1
2 0.2
3 0.3
4 0.4
5 0.5
6 0.6
7 0.7
8 0.8
9 0.9
10 1
11 1.1
...
55 5.5
56 5.6
57 5.7
58 5.8
59 5.9
60 5.99999999999999
61 6.09999999999999
62 6.19999999999999
63 6.29999999999999
64 6.39999999999999
65 6.49999999999999
66 6.59999999999999
67 6.69999999999999
68 6.79999999999999
69 6.89999999999999
70 6.99999999999999
71 7.09999999999999
72 7.19999999999999
73 7.29999999999999
...
98 9.79999999999998
99 9.89999999999998
100 9.99999999999998
101 10.1
102 10.2
103 10.3
104 10.4
105 10.5
...
223 22.3
224 22.4
225 22.5
226 22.6000000000001
227 22.7000000000001
228 22.8000000000001
229 22.9000000000001
230 23.0000000000001
231 23.1000000000001
232 23.2000000000001
233 23.3000000000001
...
396 39.6000000000003
397 39.7000000000003
398 39.8000000000003
399 39.9000000000003
400 40.0000000000003
Thanks,
Marcel
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.