Thank you for your explanation both of you.

> alester@flr4[~/play]$ perl sprintf.pl
> 056 56.9999999999999928946
>
> ...
>
> In general, the chances that you've found a bug in the language or
> implementation that's been around as long and is as widely used as Perl
> 5.6 is preeeeeeetty small.

I know that !
        
        That's why I didn't understand this behaviour first. Perl has been around      
 
        since 13 years and during past years I've already found a few 
        workarounds about bad calculations due to roundings.

        I imagine that such a discussion occured in 80's.

        But this one is different, behaviour is not equally distributed amongst
        input values. I'm very puzzling with that.


I know that roundings are often tricky from language to language (Javascript 
has not the same rules for example and a small solid state calculator has 
different ones also).

But you must know that it's very difficult to write a portable code from 
langage to langage when rules are different.


And in Perl I found these rules not very consistent.


Can you tell me what you are thinking of that ?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


perl -e '$a=0.79; printf sprintf("%03d %30.29f", $a*100, $a*100);'
079 79.00000000000000000000000000000

perl -e '$a=0.69; printf sprintf("%03d %30.29f", $a*100, $a*100);'
069 69.00000000000000000000000000000

perl -e '$a=0.49; printf sprintf("%03d %30.29f", $a*100, $a*100);'
049 49.00000000000000000000000000000

perl -e '$a=0.55; printf sprintf("%03d %30.29f", $a*100, $a*100);'
055 55.00000000000000710542735760100

perl -e '$a=0.59; printf sprintf("%03d %30.29f", $a*100, $a*100);'
059 59.00000000000000000000000000000

perl -e '$a=0.56; printf sprintf("%03d %30.29f", $a*100, $a*100);'
056 56.00000000000000710542735760100

perl -e '$a=0.61; printf sprintf("%03d %30.29f", $a*100, $a*100);'
061 61.00000000000000000000000000000

perl -e '$a=0.65; printf sprintf("%03d %30.29f", $a*100, $a*100);'
065 65.00000000000000000000000000000

perl -e '$a=0.66; printf sprintf("%03d %30.29f", $a*100, $a*100);'
066 66.00000000000000000000000000000

perl -e '$a=0.68; printf sprintf("%03d %30.29f", $a*100, $a*100);'
068 68.00000000000000000000000000000

perl -e '$a=0.28; printf sprintf("%03d %30.29f", $a*100, $a*100);'
028 28.00000000000000355271367880050

perl -e '$a=0.27; printf sprintf("%03d %30.29f", $a*100, $a*100);'
027 27.00000000000000000000000000000

perl -e '$a=0.26; printf sprintf("%03d %30.29f", $a*100, $a*100);'
026 26.00000000000000000000000000000

perl -e '$a=0.24; printf sprintf("%03d %30.29f", $a*100, $a*100);'
024 24.00000000000000000000000000000

perl -e '$a=0.47; printf sprintf("%03d %30.29f", $a*100, $a*100);'
047 47.00000000000000000000000000000

perl -e '$a=0.33; printf sprintf("%03d %30.29f", $a*100, $a*100);'
033 33.00000000000000000000000000000

perl -e '$a=0.17; printf sprintf("%03d %30.29f", $a*100, $a*100);'
017 17.00000000000000000000000000000

And if you try a loop, it works too, since, for perl, 0.01+0.01 > 0.02
and so on.


But if I take my calculator :
0.57*100 = 57 exactly !

Example :
perl -e 'for ($a=0.01;$a<1;$a+=0.01) { print  sprintf("%20.19f %03d 
%20.19f\n", $a, $a*100, $a*100);} '
0.0100000000000000002 001 1.0000000000000000000
0.0200000000000000004 002 2.0000000000000000000
0.0299999999999999989 003 3.0000000000000000000
0.0400000000000000008 004 4.0000000000000000000
0.0500000000000000028 005 5.0000000000000000000
0.0600000000000000047 006 6.0000000000000008882
0.0700000000000000067 007 7.0000000000000008882
0.0800000000000000017 008 8.0000000000000000000
0.0899999999999999967 009 9.0000000000000000000
0.0999999999999999917 010 10.0000000000000000000
0.1099999999999999867 010 10.9999999999999982236
0.1199999999999999817 011 11.9999999999999982236
0.1299999999999999767 012 12.9999999999999982236
0.1399999999999999856 013 13.9999999999999982236
0.1499999999999999944 015 15.0000000000000000000
0.1600000000000000033 016 16.0000000000000000000
0.1700000000000000122 017 17.0000000000000000000
0.1800000000000000211 018 18.0000000000000035527
0.1900000000000000300 019 19.0000000000000035527
0.2000000000000000389 020 20.0000000000000035527
0.2100000000000000477 021 21.0000000000000035527
0.2200000000000000566 022 22.0000000000000071054
0.2300000000000000655 023 23.0000000000000071054
0.2400000000000000744 024 24.0000000000000071054
0.2500000000000000555 025 25.0000000000000071054
0.2600000000000000644 026 26.0000000000000071054
0.2700000000000000733 027 27.0000000000000071054
0.2800000000000000822 028 28.0000000000000071054
0.2900000000000000910 029 29.0000000000000106581
0.3000000000000000999 030 30.0000000000000106581
0.3100000000000001088 031 31.0000000000000106581
0.3200000000000001177 032 32.0000000000000142109
0.3300000000000001266 033 33.0000000000000142109
0.3400000000000001354 034 34.0000000000000142109
0.3500000000000001443 035 35.0000000000000142109
0.3600000000000001532 036 36.0000000000000142109
0.3700000000000001621 037 37.0000000000000142109
0.3800000000000001710 038 38.0000000000000142109
0.3900000000000001799 039 39.0000000000000213163
0.4000000000000001887 040 40.0000000000000213163
0.4100000000000001976 041 41.0000000000000213163
0.4200000000000002065 042 42.0000000000000213163
0.4300000000000002154 043 43.0000000000000213163
0.4400000000000002243 044 44.0000000000000213163
0.4500000000000002331 045 45.0000000000000213163
0.4600000000000002420 046 46.0000000000000213163
0.4700000000000002509 047 47.0000000000000284217
0.4800000000000002598 048 48.0000000000000284217
0.4900000000000002687 049 49.0000000000000284217
0.5000000000000002220 050 50.0000000000000213163
0.5100000000000002309 051 51.0000000000000213163
0.5200000000000002398 052 52.0000000000000213163
0.5300000000000002487 053 53.0000000000000284217
0.5400000000000002576 054 54.0000000000000284217
0.5500000000000002665 055 55.0000000000000284217
0.5600000000000002753 056 56.0000000000000284217
0.5700000000000002842 057 57.0000000000000284217
0.5800000000000002931 058 58.0000000000000284217
0.5900000000000003020 059 59.0000000000000284217
0.6000000000000003109 060 60.0000000000000284217
0.6100000000000003197 061 61.0000000000000284217
0.6200000000000003286 062 62.0000000000000355271
0.6300000000000003375 063 63.0000000000000355271
0.6400000000000003464 064 64.0000000000000284217
0.6500000000000003553 065 65.0000000000000284217
0.6600000000000003642 066 66.0000000000000426326
0.6700000000000003730 067 67.0000000000000426326
0.6800000000000003819 068 68.0000000000000426326
0.6900000000000003908 069 69.0000000000000426326
0.7000000000000003997 070 70.0000000000000426326
0.7100000000000004086 071 71.0000000000000426326
0.7200000000000004174 072 72.0000000000000426326
0.7300000000000004263 073 73.0000000000000426326
0.7400000000000004352 074 74.0000000000000426326
0.7500000000000004441 075 75.0000000000000426326
0.7600000000000004530 076 76.0000000000000426326
0.7700000000000004619 077 77.0000000000000426326
0.7800000000000004707 078 78.0000000000000426326
0.7900000000000004796 079 79.0000000000000426326
0.8000000000000004885 080 80.0000000000000426326
0.8100000000000004974 081 81.0000000000000568434
0.8200000000000005063 082 82.0000000000000568434
0.8300000000000005151 083 83.0000000000000568434
0.8400000000000005240 084 84.0000000000000568434
0.8500000000000005329 085 85.0000000000000568434
0.8600000000000005418 086 86.0000000000000568434
0.8700000000000005507 087 87.0000000000000568434
0.8800000000000005596 088 88.0000000000000568434
0.8900000000000005684 089 89.0000000000000568434
0.9000000000000005773 090 90.0000000000000568434
0.9100000000000005862 091 91.0000000000000568434
0.9200000000000005951 092 92.0000000000000568434
0.9300000000000006040 093 93.0000000000000568434
0.9400000000000006128 094 94.0000000000000568434
0.9500000000000006217 095 95.0000000000000568434
0.9600000000000006306 096 96.0000000000000568434
0.9700000000000006395 097 97.0000000000000568434
0.9800000000000006484 098 98.0000000000000710543
0.9900000000000006573 099 99.0000000000000710543

db

Reply via email to