Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.88k stars 530 forks source link

Perl wrong calculate FLOAT #9709

Closed p5pRT closed 14 years ago

p5pRT commented 15 years ago

Migrated from rt.perl.org#64762 (status was 'rejected')

Searchable as RT64762$

p5pRT commented 15 years ago

From slavon@bigtelecom.ru

Created by slavon@bigtelecom.ru

files to test BUG! Simple run test.pl and test.php and compare return! :)) It's was very hard to find this "magic" numbers :))) Ideas?

test.txt​: 1.9
1.9
1.9
1.9
1.9
1.9
1.9
1.9
1.9
1.9
32.3
1.9
1.9
1.9
1.9
1.9
1.9
1.9
9.5
1.9
13.3
3.8
1.9
77.9
53.2
1.9
1.9
9.5
1.9
11.4
9.5
5.7
3.8
1.9
1.9
13.3
1.9
7.6
1.9
1.9
1.9
3.8
1.9
7.6
3.8
1.9
1.9
1.9
1.9
39.9
49.4
3.8
3.8
1.9
1.9
87.4
3.8
1.9
1.9
3.8
3.8
1.9
3.8
3.8
1.9
3.8
1.9
5.7
1.9
30.4

test.pl​: #!/usr/bin/perl -w

my $x = 0;

open X\,"\<test.txt";

while (\){   $x += $_;   print "$x\n";

} close X;

test.php​: \<?

$x = file_get_contents('test.txt');

$x = explode ("\n"\,$x); $y = 0;
foreach($x as $s){
  $y += $s;
  print "$y\n";
}

?>

anyway compare return of php and perl​:

Inline Patch ```diff diff -u 1.txt 2.txt --- 1.txt 2009-04-16 14:22:49.000000000 +0400 +++ 2.txt 2009-04-16 14:22:59.000000000 +0400 @@ -63,9 +63,8 @@ 537.7 541.5 543.4 -547.2 -549.1 -554.8 -556.7 -587.1 +547.199999999999 +549.099999999999 +554.799999999999 +556.699999999999 +587.099999999999 Best Regals Slavon ```
Perl Info ``` Flags: category=core severity=high Site configuration information for perl v5.8.8: Configured by Gentoo at Thu Apr 16 13:43:00 MSD 2009. Summary of my perl5 (revision 5 version 8 subversion 8) configuration: Platform: osname=linux, osvers=2.6.29-gentoo, archname=i686-linux uname='linux slavon 2.6.29-gentoo #2 smp thu apr 2 12:11:34 msd 2009 i686 intel(r) pentium(r) 4 cpu 3.00ghz genuineintel gnulinux ' config_args='-des -Darchname=i686-linux -Dcccdlflags=-fPIC -Dccdlflags=-rdynamic -Dcc=i686-pc-linux-gnu-gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth= -Doptimize=-O2 -march=pentium4 -pipe -fomit-frame-pointer -Duselargefiles -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dinstallman1dir=/usr/share/man/man1 -Dinstallman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dinc_version_list=5.8.0 5.8.0/i686-linux 5.8.2 5.8.2/i686-linux 5.8.4 5.8.4/i686-linux 5.8.5 5.8.5/i686-linux 5.8.6 5.8.6/i686-linux 5.8.7 5.8.7/i686-linux -Dcf_by=Gentoo -Ud_csh -Dusenm -Di_ndbm -Di_gdbm -Di_db' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='i686-pc-linux-gnu-gcc', ccflags ='-fno-strict-aliasing -pipe -Wdeclaration-after-statement -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm', optimize='-O2 -march=pentium4 -pipe -fomit-frame-pointer', cppflags='-fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/include/gdbm' ccversion='', gccversion='4.3.3', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='i686-pc-linux-gnu-gcc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc libc=/lib/libc-2.9.so, so=so, useshrplib=false, libperl=libperl.a gnulibc_version='2.9' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic' cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib' Locally applied patches: @INC for perl v5.8.8: /etc/perl /usr/lib/perl5/vendor_perl/5.8.8/i686-linux /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/site_perl/5.8.8/i686-linux /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/5.8.8/i686-linux /usr/lib/perl5/5.8.8 /usr/local/lib/site_perl . Environment for perl v5.8.8: HOME=/home/slavon LANG=ru_RU.CP1251 LANGUAGE= LD_LIBRARY_PATH=/usr/lib/xorg LOGDIR (unset) PATH=/usr/local/bin:/usr/bin:/bin:/opt/bin:/usr/i686-pc-linux-gnu/gcc-bin/4.3.3:/opt/sun-jdk-1.4.2.14/bin:/opt/sun-jdk-1.4.2.14/jre/bin:/opt/sun-jdk-1.4.2.14/jre/javaws:/usr/kde/3.5/bin:/usr/qt/3/bin:/usr/games/bin PERL_BADLANG (unset) SHELL=/bin/bash ```
p5pRT commented 15 years ago

From slavon@bigtelecom.ru

It's me and my accaount. Thanks :)

Чтв. Апр. 16 03​:31​:21 2009\, slavon писал​:

This is a bug report for perl from slavon@​bigtelecom.ru\, generated with the help of perlbug 1.35 running under perl v5.8.8.

----------------------------------------------------------------- [Please enter your report here]

files to test BUG! Simple run test.pl and test.php and compare return! :)) It's was very hard to find this "magic" numbers :))) Ideas?

test.txt​: 1.9 1.9 1.9 1.9 1.9 1.9 1.9 1.9 1.9 1.9 32.3 1.9 1.9 1.9 1.9 1.9 1.9 1.9 9.5 1.9 13.3 3.8 1.9 77.9 53.2 1.9 1.9 9.5 1.9 11.4 9.5 5.7 3.8 1.9 1.9 13.3 1.9 7.6 1.9 1.9 1.9 3.8 1.9 7.6 3.8 1.9 1.9 1.9 1.9 39.9 49.4 3.8 3.8 1.9 1.9 87.4 3.8 1.9 1.9 3.8 3.8 1.9 3.8 3.8 1.9 3.8 1.9 5.7 1.9 30.4

test.pl​: #!/usr/bin/perl -w

my $x = 0;

open X\,"\<test.txt";

while (\){ $x += $_; print "$x\n";

} close X;

test.php​: \<?

$x = file_get_contents('test.txt');

$x = explode ("\n"\,$x); $y = 0; foreach($x as $s){ $y += $s; print "$y\n"; }

?>

anyway compare return of php and perl​: diff -u 1.txt 2.txt --- 1.txt 2009-04-16 14​:22​:49.000000000 +0400 +++ 2.txt 2009-04-16 14​:22​:59.000000000 +0400 @​@​ -63\,9 +63\,8 @​@​ 537.7 541.5 543.4 -547.2 -549.1 -554.8 -556.7 -587.1 +547.199999999999 +549.099999999999 +554.799999999999 +556.699999999999 +587.099999999999

Best Regals Slavon

[Please do not change anything below this line] ----------------------------------------------------------------- --- Flags​: category=core severity=high --- Site configuration information for perl v5.8.8​:

Configured by Gentoo at Thu Apr 16 13​:43​:00 MSD 2009.

Summary of my perl5 (revision 5 version 8 subversion 8) configuration​: Platform​: osname=linux\, osvers=2.6.29-gentoo\, archname=i686-linux uname='linux slavon 2.6.29-gentoo #2 smp thu apr 2 12​:11​:34 msd 2009 i686 intel(r) pentium(r) 4 cpu 3.00ghz genuineintel gnulinux ' config_args='-des -Darchname=i686-linux -Dcccdlflags=-fPIC -Dccdlflags=-rdynamic -Dcc=i686-pc-linux-gnu-gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth= -Doptimize=-O2 -march=pentium4 -pipe -fomit-frame-pointer -Duselargefiles -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dinstallman1dir=/usr/share/man/man1 -Dinstallman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dinc_version_list=5.8.0 5.8.0/i686-linux 5.8.2 5.8.2/i686-linux 5.8.4 5.8.4/i686-linux 5.8.5 5.8.5/i686-linux 5.8.6 5.8.6/i686- linux 5.8.7 5.8.7/i686-linux -Dcf_by=Gentoo -Ud_csh -Dusenm -Di_ndbm -Di_gdbm -Di_db' hint=recommended\, useposix=true\, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n\, bincompat5005=undef Compiler​: cc='i686-pc-linux-gnu-gcc'\, ccflags ='-fno-strict-aliasing -pipe -Wdeclaration-after-statement -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm'\, optimize='-O2 -march=pentium4 -pipe -fomit-frame-pointer'\, cppflags='-fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/include/gdbm' ccversion=''\, gccversion='4.3.3'\, gccosandvers='' intsize=4\, longsize=4\, ptrsize=4\, doublesize=8\, byteorder=1234 d_longlong=define\, longlongsize=8\, d_longdbl=define\, longdblsize=12 ivtype='long'\, ivsize=4\, nvtype='double'\, nvsize=8\, Off_t='off_t'\, lseeksize=8 alignbytes=4\, prototype=define Linker and Libraries​: ld='i686-pc-linux-gnu-gcc'\, ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc libc=/lib/libc-2.9.so\, so=so\, useshrplib=false\, libperl=libperl.a gnulibc_version='2.9' Dynamic Linking​: dlsrc=dl_dlopen.xs\, dlext=so\, d_dlsymun=undef\, ccdlflags='- rdynamic' cccdlflags='-fPIC'\, lddlflags='-shared -L/usr/local/lib'

Locally applied patches​:

--- @​INC for perl v5.8.8​: /etc/perl /usr/lib/perl5/vendor_perl/5.8.8/i686-linux /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/site_perl/5.8.8/i686-linux /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/5.8.8/i686-linux /usr/lib/perl5/5.8.8 /usr/local/lib/site_perl .

--- Environment for perl v5.8.8​: HOME=/home/slavon LANG=ru_RU.CP1251 LANGUAGE= LD_LIBRARY_PATH=/usr/lib/xorg LOGDIR (unset) PATH=/usr/local/bin​:/usr/bin​:/bin​:/opt/bin​:/usr/i686-pc-linux- gnu/gcc-bin/4.3.3​:/opt/sun-jdk-1.4.2.14/bin​:/opt/sun-jdk- 1.4.2.14/jre/bin​:/opt/sun-jdk- 1.4.2.14/jre/javaws​:/usr/kde/3.5/bin​:/usr/qt/3/bin​:/usr/games/bin PERL_BADLANG (unset) SHELL=/bin/bash

p5pRT commented 15 years ago

slavon@bigtelecom.ru - Status changed from 'new' to 'open'

p5pRT commented 15 years ago

From @rurban

2009/4/16 slavon@​bigtelecom.ru (via RT) \perlbug\-followup@&#8203;perl\.org​:

# New Ticket Created by  slavon@​bigtelecom.ru # Please include the string​:  [perl #64762] # in the subject line of all future correspondence about this issue. # \<URL​: http​://rt.perl.org/rt3/Ticket/Display.html?id=64762 >

This is a bug report for perl from slavon@​bigtelecom.ru\, generated with the help of perlbug 1.35 running under perl v5.8.8.

Interesting but not our problem. Please submit this php "bug" - rounding problem to php and not to perl. perl does it right.

----------------------------------------------------------------- [Please enter your report here]

files to test BUG! Simple run test.pl and test.php and compare return! :)) It's was very hard to find this "magic" numbers :)))  Ideas?

test.txt​: 1.9 1.9 1.9 1.9 1.9 1.9 1.9 1.9 1.9 1.9 32.3 1.9 1.9 1.9 1.9 1.9 1.9 1.9 9.5 1.9 13.3 3.8 1.9 77.9 53.2 1.9 1.9 9.5 1.9 11.4 9.5 5.7 3.8 1.9 1.9 13.3 1.9 7.6 1.9 1.9 1.9 3.8 1.9 7.6 3.8 1.9 1.9 1.9 1.9 39.9 49.4 3.8 3.8 1.9 1.9 87.4 3.8 1.9 1.9 3.8 3.8 1.9 3.8 3.8 1.9 3.8 1.9 5.7 1.9 30.4

test.pl​: #!/usr/bin/perl -w

my $x = 0;

open X\,"\<test.txt";

while (\){        $x += $_;        print "$x\n";

} close X;

test.php​: \<?

$x = file_get_contents('test.txt');

$x = explode ("\n"\,$x); $y = 0; foreach($x as $s){        $y += $s;        print "$y\n"; }

?>

anyway compare return of php and perl​: diff -u 1.txt 2.txt --- 1.txt       2009-04-16 14​:22​:49.000000000 +0400 +++ 2.txt       2009-04-16 14​:22​:59.000000000 +0400 @​@​ -63\,9 +63\,8 @​@​  537.7  541.5  543.4 -547.2 -549.1 -554.8 -556.7 -587.1 +547.199999999999 +549.099999999999 +554.799999999999 +556.699999999999 +587.099999999999

Best Regals Slavon

[Please do not change anything below this line] ----------------------------------------------------------------- --- Flags​:    category=core    severity=high --- Site configuration information for perl v5.8.8​:

Configured by Gentoo at Thu Apr 16 13​:43​:00 MSD 2009.

Summary of my perl5 (revision 5 version 8 subversion 8) configuration​:  Platform​:    osname=linux\, osvers=2.6.29-gentoo\, archname=i686-linux    uname='linux slavon 2.6.29-gentoo #2 smp thu apr 2 12​:11​:34 msd 2009 i686 intel(r) pentium(r) 4 cpu 3.00ghz genuineintel gnulinux '    config_args='-des -Darchname=i686-linux -Dcccdlflags=-fPIC -Dccdlflags=-rdynamic -Dcc=i686-pc-linux-gnu-gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth=  -Doptimize=-O2 -march=pentium4 -pipe -fomit-frame-pointer -Duselargefiles -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dinstallman1dir=/usr/share/man/man1 -Dinstallman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dinc_version_list=5.8.0 5.8.0/i686-linux 5.8.2 5.8.2/i686-linux 5.8.4 5.8.4/i686-linux 5.8.5 5.8.5/i686-linux 5.8.6 5.8.6/i686-linux 5.8.7 5.8.7/i686-linux  -Dcf_by=Gentoo -Ud_csh -Dusenm -Di_ndbm -Di_gdbm -Di_db'    hint=recommended\, useposix=true\, d_sigaction=define    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef    use64bitint=undef use64bitall=undef uselongdouble=undef    usemymalloc=n\, bincompat5005=undef  Compiler​:    cc='i686-pc-linux-gnu-gcc'\, ccflags ='-fno-strict-aliasing -pipe -Wdeclaration-after-statement -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm'\,    optimize='-O2 -march=pentium4 -pipe -fomit-frame-pointer'\,    cppflags='-fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/include/gdbm'    ccversion=''\, gccversion='4.3.3'\, gccosandvers=''    intsize=4\, longsize=4\, ptrsize=4\, doublesize=8\, byteorder=1234    d_longlong=define\, longlongsize=8\, d_longdbl=define\, longdblsize=12    ivtype='long'\, ivsize=4\, nvtype='double'\, nvsize=8\, Off_t='off_t'\, lseeksize=8    alignbytes=4\, prototype=define  Linker and Libraries​:    ld='i686-pc-linux-gnu-gcc'\, ldflags =' -L/usr/local/lib'    libpth=/usr/local/lib /lib /usr/lib    libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc    libc=/lib/libc-2.9.so\, so=so\, useshrplib=false\, libperl=libperl.a    gnulibc_version='2.9'  Dynamic Linking​:    dlsrc=dl_dlopen.xs\, dlext=so\, d_dlsymun=undef\, ccdlflags='-rdynamic'    cccdlflags='-fPIC'\, lddlflags='-shared -L/usr/local/lib'

Locally applied patches​:

--- @​INC for perl v5.8.8​:    /etc/perl    /usr/lib/perl5/vendor_perl/5.8.8/i686-linux    /usr/lib/perl5/vendor_perl/5.8.8    /usr/lib/perl5/vendor_perl    /usr/lib/perl5/site_perl/5.8.8/i686-linux    /usr/lib/perl5/site_perl/5.8.8    /usr/lib/perl5/site_perl    /usr/lib/perl5/5.8.8/i686-linux    /usr/lib/perl5/5.8.8    /usr/local/lib/site_perl    .

--- Environment for perl v5.8.8​:    HOME=/home/slavon    LANG=ru_RU.CP1251    LANGUAGE=    LD_LIBRARY_PATH=/usr/lib/xorg    LOGDIR (unset)    PATH=/usr/local/bin​:/usr/bin​:/bin​:/opt/bin​:/usr/i686-pc-linux-gnu/gcc-bin/4.3.3​:/opt/sun-jdk-1.4.2.14/bin​:/opt/sun-jdk-1.4.2.14/jre/bin​:/opt/sun-jdk-1.4.2.14/jre/javaws​:/usr/kde/3.5/bin​:/usr/qt/3/bin​:/usr/games/bin    PERL_BADLANG (unset)    SHELL=/bin/bash

-- Reini Urban http​://phpwiki.org/ http​://murbreak.at/

p5pRT commented 15 years ago

From slavon@bigtelecom.ru

Чтв. Апр. 16 04​:33​:58 2009\, rurban писал​:

2009/4/16 slavon@​bigtelecom.ru (via RT) \perlbug\-followup@&#8203;perl\.org​:

# New Ticket Created by  slavon@​bigtelecom.ru # Please include the string​:  [perl #64762] # in the subject line of all future correspondence about this issue. # \<URL​: http​://rt.perl.org/rt3/Ticket/Display.html?id=64762 >

This is a bug report for perl from slavon@​bigtelecom.ru\, generated with the help of perlbug 1.35 running under perl v5.8.8.

Interesting but not our problem. Please submit this php "bug" - rounding problem to php and not to perl. perl does it right.

You not test script? Perl result xxx.x999999 after sum 2 float numbers. Its normal? Php do it right. Its perl and its bug.

p5pRT commented 15 years ago

From slavon@bigtelecom.ru

You not test script? Perl result xxx.x999999 after sum 2 float numbers. Its normal? Php do it right. Its perl and its bug.

Ok. Update script​:

#!/usr/bin/perl -w

my $x = 0; open X\,"\<test.txt";

while (\){   $_ =~ /^([\d\.]+)\D*/;   print "was $x. we add $1. its has ";   $x += $1;   print "$x\n";

}

close X;

Return​:

was 0. we add 1.9. its has 1.9
 
was 1.9. we add 1.9. its has 3.8
 
was 3.8. we add 1.9. its has 5.7
 
was 5.7. we add 1.9. its has 7.6
 
was 7.6. we add 1.9. its has 9.5
 
was 9.5. we add 1.9. its has 11.4
 
was 11.4. we add 1.9. its has 13.3
 
was 13.3. we add 1.9. its has 15.2
 
was 15.2. we add 1.9. its has 17.1
 
was 17.1. we add 1.9. its has 19
 
was 19. we add 32.3. its has 51.3
 
was 51.3. we add 1.9. its has 53.2
 
was 53.2. we add 1.9. its has 55.1
 
was 55.1. we add 1.9. its has 57
 
was 57. we add 1.9. its has 58.9 was 58.9. we add 1.9. its has 60.8 was 60.8. we add 1.9. its has 62.7 was 62.7. we add 1.9. its has 64.6 was 64.6. we add 9.5. its has 74.1 was 74.1. we add 1.9. its has 76 was 76. we add 13.3. its has 89.3 was 89.3. we add 3.8. its has 93.1 was 93.1. we add 1.9. its has 95 was 95. we add 77.9. its has 172.9 was 172.9. we add 53.2. its has 226.1 was 226.1. we add 1.9. its has 228 was 228. we add 1.9. its has 229.9 was 229.9. we add 9.5. its has 239.4 was 239.4. we add 1.9. its has 241.3 was 241.3. we add 11.4. its has 252.7 was 252.7. we add 9.5. its has 262.2 was 262.2. we add 5.7. its has 267.9 was 267.9. we add 3.8. its has 271.7 was 271.7. we add 1.9. its has 273.6 was 273.6. we add 1.9. its has 275.5 was 275.5. we add 13.3. its has 288.8 was 288.8. we add 1.9. its has 290.7 was 290.7. we add 7.6. its has 298.3 was 298.3. we add 1.9. its has 300.2 was 300.2. we add 1.9. its has 302.1 was 302.1. we add 1.9. its has 304 was 304. we add 3.8. its has 307.8 was 307.8. we add 1.9. its has 309.7 was 309.7. we add 7.6. its has 317.3 was 317.3. we add 3.8. its has 321.1 was 321.1. we add 1.9. its has 323 was 323. we add 1.9. its has 324.9 was 324.9. we add 1.9. its has 326.8 was 326.8. we add 1.9. its has 328.7 was 328.7. we add 39.9. its has 368.6 was 368.6. we add 49.4. its has 418 was 418. we add 3.8. its has 421.8 was 421.8. we add 3.8. its has 425.6 was 425.6. we add 1.9. its has 427.5 was 427.5. we add 1.9. its has 429.4 was 429.4. we add 87.4. its has 516.8 was 516.8. we add 3.8. its has 520.6 was 520.6. we add 1.9. its has 522.5 was 522.5. we add 1.9. its has 524.4 was 524.4. we add 3.8. its has 528.2 was 528.2. we add 3.8. its has 532 was 532. we add 1.9. its has 533.9 was 533.9. we add 3.8. its has 537.7 was 537.7. we add 3.8. its has 541.5 was 541.5. we add 1.9. its has 543.4 was 543.4. we add 3.8. its has 547.199999999999 was 547.199999999999. we add 1.9. its has 549.099999999999 was 549.099999999999. we add 5.7. its has 554.799999999999 was 554.799999999999. we add 1.9. its has 556.699999999999 was 556.699999999999. we add 30.4. its has 587.099999999999

Its normal?

p5pRT commented 15 years ago

From slavon@bigtelecom.ru

but!

perl -e 'print (543.4 + 3.8); print "\n";' 547.2

and perl -e '$a=543.4; $a+=3.8; print "$a\n";' 547.2

Only with list of "magic numbers".

Overload?

p5pRT commented 15 years ago

From slavon@bigtelecom.ru

New experement! :)))

perl -e '$a=1.1; for $i (1 .. 100) { $a += 1.1; print "$a\n";}'; 2.2
 
3.3
 
4.4
 
5.5
 
6.6
 
7.7
 
8.8
 
9.9
 
11
 
12.1
 
13.2
 
14.3
 
15.4
 
16.5
 
17.6
 
18.7
 
19.8
 
20.9
 
22
 
23.1
 
24.2
 
25.3
 
26.4
 
27.5
 
28.6
 
29.7
 
30.8
 
31.9
 
33
 
34.1
 
35.2
 
36.3
 
37.4
 
38.5
 
39.6
 
40.7
 
41.8
 
42.9
 
44
 
45.1
 
46.2
 
47.3
 
48.4
 
49.5 50.6 51.7 52.8 53.9 55 56.1 57.2 58.3 59.4 60.5 61.6000000000001 62.7000000000001 63.8000000000001 64.9 66 67.1 68.2 69.3 70.4 71.5 72.6 73.7 74.8 75.9 77 78.1 79.2 80.3 81.4 82.5 83.6 84.6999999999999 85.7999999999999 86.8999999999999 87.9999999999999 89.0999999999999 90.1999999999999 91.2999999999999 92.3999999999999 93.4999999999999 94.5999999999999 95.6999999999999 96.7999999999999 97.8999999999999 98.9999999999999 100.1 101.2 102.3 103.4 104.5 105.6 106.7 107.8 108.9 110 111.1

p5pRT commented 15 years ago

From domo@computer.org

On 2009–04–16\, at 13​:33\, Reini Urban wrote​:

Please submit this php "bug" - rounding problem to php and not to
perl. perl does it right.

Agreed. From the OP's example\, PHP's default rounding seems over- aggressive​: it's masking machine behaviour which is liable to bite
you​: there's some (probably very long) sequence of single-place
decimal numbers that will ultimately make PHP give a result that is
off by 0.1.

See also the FAQ\, part 4\, "Why am I getting long decimals (eg\,
19.9499999999999) instead of the numbers I should be getting (eg\,
19.95)?" (from the command line\, use perldoc -q decimal) and also \<http​://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

.

If you can't live with floating point's inherent approximations\, and
don't need blinding speed\, look at the bignum\, Math​::BigFloat or even
the Math​::BigRat packages. For example\, putting

use bignum;

as the second line in test.pl will probably solve the "problem"; -- Dominic Dunlop

p5pRT commented 15 years ago

From @schwern

Reini Urban wrote​:

Interesting but not our problem. Please submit this php "bug" - rounding problem to php and not to perl. perl does it right.

I know we get 2398297 of these sort of reports a month\, and it gets tiresome\, but please keep in mind that the reporter is usually encountering this for the first time. They don't know anything about how floating point numbers work\, why should they? It violates everything they were taught in math class. It's TOTALLY INSANE unless you understand the intimate details of how computers work. Even then it's still pretty crazy that we let that known broken detail leak out in a high language like Perl. How would you like it if your supermarket clerk said you owed $29.999999999999999999999 and looked at you like you had nine heads when you objected? "That's the way supermarkets do their accounting\, OF COURSE." That PHP decided to round it off when floats go mad is entirely in keeping with their use-case.

Anyhow\, just try to be understanding when we get this sort of bug report. Thank the reporter for the report. Point to the perlfaq entry. Encourage them to keep reporting things that seem broken\, because the last thing you want is newbs deciding what seems right and what seems wrong and not telling us when things are broken because they're afraid of being told that its not a bug OF COURSE.

-- Defender of Lexical Encapsulation

p5pRT commented 15 years ago

From @nwc10

On Sat\, Apr 18\, 2009 at 11​:46​:47AM -0700\, Michael G Schwern wrote​:

I know we get 2398297 of these sort of reports a month\, and it gets tiresome\, but please keep in mind that the reporter is usually encountering this for the first time. They don't know anything about how floating point numbers work\, why should they? It violates everything they were taught in math class. It's TOTALLY INSANE unless you understand the intimate details of how computers work. Even then it's still pretty crazy that we let that known broken detail leak out in a high language like Perl. How would you like it if your supermarket clerk said you owed $29.999999999999999999999 and looked at you like you had nine heads when you objected? "That's the way supermarkets do their accounting\, OF COURSE."

A good point\, well made. On the subject of supermarkets and floating point\, you might be amused by

http​://www.flickr.com/photos/blech/2434402191/in/photostream/

Nicholas Clark

p5pRT commented 15 years ago

From sergstesh@yahoo.com

I'll add a comment in both English and Russian - it appears the OP's native language is Russian.

In English​: one shouldn't even start using floating point numbers untill he/she takes a basic course on computer arithmetics. A good starting point is this​:

http​://en.wikipedia.org/wiki/Floating_point .

One should also remember that periodic fractions in one radix are not necessarily periodic in another.

In Russian​: не стОит пытаться писать программы\, использующие числа с плавающей точкой\, до изучения базовых понятий компьютерной арифметики. Хорошо начать с этого​:

http​://en.wikipedia.org/wiki/Floating_point .

Следует помнить\, что периодические дроби в одной системе счисления не обязательно являются таковыми в другой.

p5pRT commented 14 years ago

@iabyn - Status changed from 'open' to 'rejected'