develooper Front page | perl.perl5.porters | Postings from April 2000

[ID 20000426.007] rounding error after division

From:
Daniel Ortmann
Date:
April 26, 2000 20:23
Subject:
[ID 20000426.007] rounding error after division
Message ID:
200004270322.WAA57103@sparc.isl.net

This is a bug report for perl from ortmann@pyrl.eye,
generated with the help of perlbug 1.26 running under perl 5.00557.

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

Unexpected "int"ification caused a program crash at work today.
I submit this for your consideration and possible resolution:

perl -le '
print q[expect to get 707:];
$n = 1272.6 / 1.8;
print $n, q[ <= 1272.6 / 1.8];
print int $n, q[ <= int $n];
print "$n <= inside a string";
$n = "$n";
print $n, q[ <= after $n = "$n" sets a string context];
$n = 1272.6 / 1.8;
$n += 0.0;
print int $n, q[ <= int $n after $n += 0.0]
'

expect to get 707:
707 <= 1272.6 / 1.8
706 <= int $n
707 <= inside a string
707 <= after $n = "$n" sets a string context
706 <= int $n after $n += 0.0

1) I assume this is caused by slight truncation error due to base 2.
2) Perhaps there is IEEE floating point status information which might
   be used to un-do the truncation error?
3) The successful addition of 0.0 seems to support idea #2.
4) It seems strange that "$n" is a better "int" than int($n), although
   it is also slower.

[Please do not change anything below this line]
-----------------------------------------------------------------

---
Site configuration information for perl 5.00557:

Configured by ortmann at Mon May 31 22:49:25 CDT 1999.

Summary of my perl5 (revision 5.0 version 5 subversion 57) configuration:
  Platform:
    osname=freebsd, osvers=4.0-current, archname=i386-freebsd
    uname='freebsd pyrl.eye 4.0-current freebsd 4.0-current #1: sun may 9 23:52:53 cdt 1999 root@pyrl.eye:usrsrcsrcsyscompilepyrl i386 '
    config_args=''
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef useperlio=undef d_sfio=undef
    use64bits=undef usemultiplicity=undef
  Compiler:
    cc='cc', optimize='-O', gccversion=egcs-2.91.66 19990314 (egcs-1.1.2 release)
    cppflags='-I/usr/local/include'
    ccflags ='-I/usr/local/include'
    stdchar='char', d_stdstdio=undef, usevfork=true
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    alignbytes=4, usemymalloc=y, prototype=define
  Linker and Libraries:
    ld='cc', ldflags ='-Wl,-E  -L/usr/local/lib'
    libpth=/usr/lib /usr/local/lib
    libs=-lgdbm -lm -lc -lcrypt
    libc=/usr/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-DPIC -fpic', lddlflags='-shared  -L/usr/local/lib'

Locally applied patches:
    

---
@INC for perl 5.00557:
    /usr/local/lib/perl5/5.00557/i386-freebsd
    /usr/local/lib/perl5/5.00557
    /usr/local/lib/perl5/site_perl/5.00557/i386-freebsd
    /usr/local/lib/perl5/site_perl/5.00557
    .

---
Environment for perl 5.00557:
    HOME=/home/ortmann
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH=/usr/lib:/usr/local/lib/kaffe
    LOGDIR (unset)
    PATH=/home/ortmann/bin:/usr/local/script:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/bin:/usr/bin
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/bash



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About