Skip to content

float calc. strangeness in 5.21.11-ld #14686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
p5pRT opened this issue May 1, 2015 · 6 comments
Closed

float calc. strangeness in 5.21.11-ld #14686

p5pRT opened this issue May 1, 2015 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented May 1, 2015

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

Searchable as RT124433$

@p5pRT
Copy link
Author

p5pRT commented May 1, 2015

From [email protected]

$ cat strange_thing_my.pl
use Devel​::Peek;
use POSIX qw/floor ceil/;
my $sum = 0.002*1000;
my $s2 = int($sum/100);
print "sum is $sum, s2 is $s2\n";
print "floor( sum - (s2 * 100) ) is ";
my $sx = floor($sum - ($s2*100));
print "$sx\n";


$ perlbrew exec --with perl-5.21.11 perl strange_thing_my.pl
perl-5.21.11

sum is 2, s2 is 0
floor( sum - (s2 * 100) ) is 2

$ perlbrew exec --with perl-5.21.11-ld perl strange_thing_my.pl
perl-5.21.11-ld

sum is 2, s2 is 0
floor( sum - (s2 * 100) ) is 1

$ perlbrew exec --with perl-5.21.2-ld perl strange_thing_my.pl
perl-5.21.2-ld

sum is 2, s2 is 0
floor( sum - (s2 * 100) ) is 2


problem that in perl-5.21.11-ld floor( sum - (s2 * 100) ) is 1, not 2.
is this a bug?
found in http​://www.cpantesters.org/cpan/report/1e041e66-ede9-11e4-93de-b9891b12eb46

perl-5.21.11-ld

Summary of my perl5 (revision 5 version 21 subversion 11) configuration​:

  Platform​:
  osname=linux, osvers=3.13.0-49-generic, archname=x86_64-linux-ld
  uname='linux green-u 3.13.0-49-generic #81~precise1-ubuntu smp wed
mar 25 16​:32​:15 utc 2015 x86_64 x86_64 x86_64 gnulinux '
  config_args='-de -Dprefix=/home/perlbrew/perls/perl-5.21.11-ld
-Duselongdouble -Dusedevel
-Aeval​:scriptdir=/home/perlbrew/perls/perl-5.21.11-ld/bin'
  hint=recommended, useposix=true, d_sigaction=define
  useithreads=undef, usemultiplicity=undef
  use64bitint=define, use64bitall=define, uselongdouble=define
  usemymalloc=n, bincompat5005=undef
  Compiler​:
  cc='cc', ccflags ='-fwrapv -fno-strict-aliasing -pipe
-fstack-protector -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64',
  optimize='-O2',
  cppflags='-fwrapv -fno-strict-aliasing -pipe -fstack-protector
-I/usr/local/include'
  ccversion='', gccversion='4.6.3', gccosandvers=''
  intsize=4, longsize=8, ptrsize=8, doublesize=8,
byteorder=12345678, doublekind=3
  d_longlong=define, longlongsize=8, d_longdbl=define,
longdblsize=16, longdblkind=3
  ivtype='long', ivsize=8, nvtype='long double', nvsize=16,
Off_t='off_t', lseeksize=8
  alignbytes=16, prototype=define
  Linker and Libraries​:
  ld='cc', ldflags =' -fstack-protector -L/usr/local/lib'
  libpth=/usr/local/lib
/usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed
/usr/include/x86_64-linux-gnu /usr/lib /lib/x86_64-linux-gnu
/lib/../lib /usr/lib/x86_64-linux-gnu /usr/lib/../lib /lib
  libs=-lpthread -lnsl -lgdbm -ldl -lm -lcrypt -lutil -lc -lgdbm_compat
  perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
  libc=, so=so, useshrplib=false, libperl=libperl.a
  gnulibc_version='2.15'
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
  cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib
-fstack-protector'

Characteristics of this binary (from libperl)​:
  Compile-time options​: HAS_TIMES PERLIO_LAYERS PERL_DONT_CREATE_GVSV
  PERL_HASH_FUNC_ONE_AT_A_TIME_HARD PERL_MALLOC_WRAP
  PERL_NEW_COPY_ON_WRITE PERL_PRESERVE_IVUV
  PERL_USE_DEVEL USE_64_BIT_ALL USE_64_BIT_INT
  USE_LARGE_FILES USE_LOCALE USE_LOCALE_COLLATE
  USE_LOCALE_CTYPE USE_LOCALE_NUMERIC USE_LOCALE_TIME
  USE_LONG_DOUBLE USE_PERLIO USE_PERL_ATOF
  Built under linux
  Compiled at May 1 2015 13​:35​:33
  %ENV​:
  PERL5LIB=""
  PERLBREW_BASHRC_VERSION="0.69"
  PERLBREW_HOME="/home/vse/.perlbrew"
  PERLBREW_MANPATH="/home/perlbrew/perls/perl-5.21.11-ld/man"
  PERLBREW_PATH="/home/perlbrew/bin​:/home/perlbrew/perls/perl-5.21.11-ld/bin"
  PERLBREW_PERL="perl-5.21.11-ld"
  PERLBREW_ROOT="/home/perlbrew"
  PERLBREW_VERSION="0.69"
  @​INC​:
  /home/perlbrew/perls/perl-5.21.11-ld/lib/site_perl/5.21.11/x86_64-linux-ld
  /home/perlbrew/perls/perl-5.21.11-ld/lib/site_perl/5.21.11
  /home/perlbrew/perls/perl-5.21.11-ld/lib/5.21.11/x86_64-linux-ld
  /home/perlbrew/perls/perl-5.21.11-ld/lib/5.21.11
  .

@p5pRT
Copy link
Author

p5pRT commented May 1, 2015

From [email protected]

Victor Efimov wrote​:

problem that in perl-5.21.11-ld floor( sum - (s2 * 100) ) is 1, not 2.
is this a bug?

No.

$ perl-ld -lwe 'printf "%.75f\n", 0.002'
0.001999999999999999999916990771169078566771304394933395087718963623046875000
$ perl-ld -lwe 'printf "%.75f\n", 0.002*1000'
1.999999999999999999891579782751449556599254719913005828857421875000000000000

0.002 does not have a terminating binary representation, so cannot
be represented exactly in floating point. Any computation with it
necessarily suffers rounding error. 0.002*1000 in floating point is
really computing round(round(0.002)*1000)). This produces the above
inexact answer in the x87 80-bit format. As it happens, the same
computation in IEEE double precision produces the mathematically correct
answer, but that's really by accident​:

$ perl-d -lwe 'printf "%.75f\n", 0.002'
0.002000000000000000041633363423443370265886187553405761718750000000000000000
$ perl-d -lwe 'printf "%.75f\n", 0.002*1000'
2.000000000000000000000000000000000000000000000000000000000000000000000000000

-zefram

@p5pRT
Copy link
Author

p5pRT commented May 1, 2015

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented May 1, 2015

From [email protected]

Ok, indeed. Difference between versions confused me. Ticket can be closed.

On Fri May 01 04​:17​:52 2015, zefram@​fysh.org wrote​:

Victor Efimov wrote​:

problem that in perl-5.21.11-ld floor( sum - (s2 * 100) ) is 1, not
2.
is this a bug?

No.

$ perl-ld -lwe 'printf "%.75f\n", 0.002'
0.001999999999999999999916990771169078566771304394933395087718963623046875000
$ perl-ld -lwe 'printf "%.75f\n", 0.002*1000'
1.999999999999999999891579782751449556599254719913005828857421875000000000000

0.002 does not have a terminating binary representation, so cannot
be represented exactly in floating point. Any computation with it
necessarily suffers rounding error. 0.002*1000 in floating point is
really computing round(round(0.002)*1000)). This produces the above
inexact answer in the x87 80-bit format. As it happens, the same
computation in IEEE double precision produces the mathematically
correct
answer, but that's really by accident​:

$ perl-d -lwe 'printf "%.75f\n", 0.002'
0.002000000000000000041633363423443370265886187553405761718750000000000000000
$ perl-d -lwe 'printf "%.75f\n", 0.002*1000'
2.000000000000000000000000000000000000000000000000000000000000000000000000000

-zefram

@p5pRT
Copy link
Author

p5pRT commented May 2, 2015

From @jkeenan

Closing per request of original poster.

--
James E Keenan (jkeenan@​cpan.org)

@p5pRT p5pRT closed this as completed May 2, 2015
@p5pRT
Copy link
Author

p5pRT commented May 2, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant