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

[ID 20000917.004] Internal floating-point rounding error?

From:
Philip Andrew
Date:
September 17, 2000 17:55
Subject:
[ID 20000917.004] Internal floating-point rounding error?
Message ID:
200009180054.RAA06040@soichip.inchip.com

This is a bug report for perl from Philip.Andrew@stanfordalumni.org,
generated with the help of perlbug 1.28 running under perl v5.6.0.


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

I get strange results running the following Perl stub:

    printf "%04d\n", $ARGV[0]/$ARGV[1];

Results:

    # Command Run	=>	Output
    # -----------		------
    stub.pl 0.55 0.01   =>      0055
    stub.pl 0.56 0.01   =>      0056
    stub.pl 0.57 0.01   =>      0056	<== HUH?
    stub.pl 0.58 0.01   =>      0057	<== HUH?
    stub.pl 0.59 0.01   =>      0058	<== HUH?
    stub.pl 0.60 0.01   =>      0060
    stub.pl 0.61 0.01   =>      0061
    stub.pl 0.055 0.001 =>      0055
    stub.pl 0.056 0.001 =>      0056
    stub.pl 0.057 0.001 =>      0057
    stub.pl 0.058 0.001 =>      0058
    stub.pl 0.059 0.001 =>      0058	<== HUH?
    stub.pl 0.060 0.001 =>      0060
    stub.pl 0.061 0.001 =>      0061

I can avoid this round-off error by changing the 'printf' to

    printf "%04d\n", ($ARGV[0]/$ARGV[1])+($ARGV[1]/10);

(adding 1/10 of my resolution to the number), but what's the
point -- the round-off is supposed to be correct, isn't it?

I ran into this trying to create an encoding string from a real
number, thereby eliminating the decimal point from the string
(so that I could use the encoding in a file name, and not have an
unwanted period added in the file name).

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

Configured by toolsadm at Thu Aug 17 18:36:22 PDT 2000.

Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration:
  Platform:
    osname=solaris, osvers=2.5.1, archname=sun4-solaris
    uname='sunos chip 5.5.1 generic_103640-08 sun4u sparc sunw,ultra-1 '
    config_args='-de -A cc=gcc'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=define 
    use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
  Compiler:
    cc=' gcc', optimize='-O', gccversion=2.95.2 19991024 (release)
    cppflags='-fno-strict-aliasing -I/usr/local/include -I/opt/gnu/include'
    ccflags ='-fno-strict-aliasing -I/usr/local/include -I/opt/gnu/include '
    stdchar='unsigned char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=4
    alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries:
    ld=' gcc', ldflags =' -L/usr/local/lib -L/opt/gnu/lib '
    libpth=/usr/local/lib /opt/gnu/lib /lib /usr/lib /usr/ccs/lib
    libs=-lsocket -lnsl -ldl -lm -lc -lcrypt -lsec
    libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib -L/opt/gnu/lib'

Locally applied patches:
    

---
@INC for perl v5.6.0:
    /usr/local/lib/perl5/5.6.0/sun4-solaris
    /usr/local/lib/perl5/5.6.0
    /usr/local/lib/perl5/site_perl/5.6.0/sun4-solaris
    /usr/local/lib/perl5/site_perl/5.6.0
    /usr/local/lib/perl5/site_perl
    .

---
Environment for perl v5.6.0:
    HOME=/home/phil
    LANG (unset)
    LANGUAGE (unset)
    LC_COLLATE=en_US.ISO8859-1
    LC_CTYPE=en_US.ISO8859-1
    LC_MESSAGES=C
    LC_MONETARY=en_US.ISO8859-1
    LC_NUMERIC=en_US.ISO8859-1
    LC_TIME=en_US.ISO8859-1
    LD_LIBRARY_PATH=/usr/openwin/lib:/usr/local/lib:/opt/gnu/lib:/usr/lib
    LOGDIR (unset)
    PATH=.:/home/phil/bin:/home/phil/bin/solaris:/usr/local/bin:/opt/gnu/bin:/usr/bin:/usr/sbin:/usr/ccs/bin:/usr/ucb:/etc:/usr/dt/bin:/usr/openwin/bin
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/tcsh




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