develooper Front page | perl.perl5.porters | Postings from January 2012

[perl #107702] Re: [OTS#020287925] Rounding error in sprintf.

From:
sdd @ google . com
Date:
January 6, 2012 17:39
Subject:
[perl #107702] Re: [OTS#020287925] Rounding error in sprintf.
Message ID:
rt-3.6.HEAD-14510-1325900368-1815.107702-75-0@perl.org
# New Ticket Created by  sdd@google.com 
# Please include the string:  [perl #107702]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org:443/rt3/Ticket/Display.html?id=107702 >


Sam's answer indicates that this is is a result of an efficient
implementation within Perl, which I would argue, using the principle of
least surprise, is a bug if it gives incorrect answers for math that could
be handled within an int, but perhaps a pragma would be a way to fix that.

I'm not sure Graham's answer covers it -- program below prints "247", while
perl prints "246", so sprintf isn't behaving the same as the underlying C.

-- Sal
smile.


#include <stdio.h>

int main(void)
{
  float a = 2.47;
  char buf[100];
  sprintf(buf, "%d", int(a * 100));
  printf("%s\n", buf);
  return 0;
}


On Fri, Jan 6, 2012 at 5:02 PM, ActiveState Support <support@activestate.com
> wrote:

> sdd@google.com,
>
> Behaviour of sprintf() is dependent on the underlying C libraries. See
> these
> discussions:
>
> http://stackoverflow.com/questions/1651671/why-does-perls-sprintf-not-round-floating-point-numbers-correctly
>
>
> http://perldoc.perl.org/perlfaq4.html#Does-Perl-have-a-round%28%29-function%3f--What-about-ceil%28%29-and-floor%28%29%3f--Trig-functions%3f
>
> Best regards,
>
> Graham Stuart
> Technical Support Engineer
> ActiveState - The Dynamic Language Experts
> http://www.ActiveState.com
> Get insights on Open Source and Dynamic Languages at
> www.activestate.com/blog
>
> 01/06/2012 11:20 - sdd@google.com wrote:
>
> >
> > This is a bug report for perl from sdd@google.com,
> > generated with the help of perlbug 1.39 running under perl 5.14.2.
> >
> >
> > -----------------------------------------------------------------
> > [Please describe your issue here]
> >
> > Using ActivePerl 5.14.2, and at various other versions going as far back
> as the
> 5.8.9 version shipped with Leopard, the following command returns what I
> believe
> to be an incorrect result:
> >
> > bash-3.2$ ./perl -e 'print sprintf("%d\n", "2.47" * 100);'
> > 246
> > bash-3.2$
> >
> >
> >
> > [Please do not change anything below this line]
> > -----------------------------------------------------------------
> > ---
> > Flags:
> >     category=core
> >     severity=medium
> > ---
> > Site configuration information for perl 5.14.2:
> >
> > Configured by ActiveState at Fri Oct  7 15:56:08 PDT 2011.
> >
> > Summary of my perl5 (revision 5 version 14 subversion 2) configuration:
> >
> >   Platform:
> >     osname=darwin, osvers=9.8.0, archname=darwin-thread-multi-2level
> >     uname='darwin kitty 9.8.0 darwin kernel version 9.8.0: wed jul 15
> 16:55:01
> pdt 2009; root:xnu-1228.15.4~1release_i386 i386 '
> >     config_args='-ders -Dcc=gcc -Dusethreads -Duseithreads
> -Uinstallusrbinperl
> -Ulocincpth= -Uloclibpth= -Ud_poll -Ui_poll -Ui_libutil
> -Aldflags=-Wl,-search_paths_first -Alddlflags=-Wl,-search_paths_first
> -Accflags=-arch i386 -arch x86_64 -Aldflags=-arch i386 -arch x86_64
> -Alddlflags=-arch i386 -arch x86_64 -Accflags=-nostdinc
> -B/Developer/SDKs/MacOSX10.5.sdk/usr/include/gcc
> -B/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc
> -isystem/Developer/SDKs/MacOSX10.5.sdk/usr/include
> -F/Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks
> -mmacosx-version-min=10.5
> -Aldflags=-Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk
> -mmacosx-version-min=10.5
> -Alddlflags=-Wl,-syslibroot,/Developer/SDKs/MacOSX10.5.sdk
> -mmacosx-version-min=10.5 -Accflags=-DUSE_SITECUSTOMIZE -Duselargefiles
> -Accflags=-DPERL_RELOCATABLE_INCPUSH -Accflags=-fno-merge-constants
> -Dprefix=/usr/local/ActivePerl-5.14
> -Dprivlib=/usr/local/ActivePerl-5.14/lib
> -Darchlib=/usr/local/ActivePerl-5.14/lib -Dsiteprefix=/usr/local/ActiveP
> >  erl-5.14/site -Dsitelib=/usr/local/ActivePerl-5.14/site/lib
> -Dsitearch=/usr/local/ActivePerl-5.14/site/lib -Dsed=/usr/bin/sed
> -Duseshrplib
> -Dcf_by=ActiveState -Dcf_email=support@ActiveState.com'
> >     hint=recommended, useposix=true, d_sigaction=define
> >     useithreads=define, usemultiplicity=define
> >     useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
> >     use64bitint=define, use64bitall=define, uselongdouble=undef
> >     usemymalloc=n, bincompat5005=undef
> >   Compiler:
> >     cc='gcc', ccflags ='-fno-common -DPERL_DARWIN -arch i386
> -DUSE_SITECUSTOMIZE
> -DPERL_RELOCATABLE_INCPUSH -fno-merge-constants -fno-strict-aliasing -pipe
> -fstack-protector',
> >     optimize='-O3',
> >     cppflags='-no-cpp-precomp -fno-common -DPERL_DARWIN -no-cpp-precomp
> -arch
> i386 -nostdinc -B/Developer/SDKs/MacOSX10.5.sdk/usr/include/gcc
> -B/Developer/SDKs/MacOSX10.5.sdk/usr/lib/gcc
> -isystem/Developer/SDKs/MacOSX10.5.sdk/usr/include
> -F/Developer/SDKs/MacOSX10.5.sdk/System/Library/Frameworks
> -mmacosx-version-min=10.5 -DUSE_SITECUSTOMIZE -DPERL_RELOCATABLE_INCPUSH
> -fno-merge-constants -fno-strict-aliasing -pipe -fstack-protector'
> >     ccversion='', gccversion='i686-apple-darwin10-llvm-gcc-4.2 (GCC)
> 4.2.1
> (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)', gccosandvers=''
> >     intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
> >     d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
> >     ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t',
> lseeksize=8
> >     alignbytes=8, prototype=define
> >   Linker and Libraries:
> >     ld='cc -mmacosx-version-min=10.5', ldflags ='
> -Wl,-search_paths_first -arch
> i386 -fstack-protector'
> >     libpth=/lib /usr/lib /usr/local/lib
> >     libs=-ldbm -ldl -lm -lutil -lc
> >     perllibs=-ldl -lm -lutil -lc
> >     libc=, so=dylib, useshrplib=true, libperl=libperl.dylib
> >     gnulibc_version=''
> >   Dynamic Linking:
> >     dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
> >     cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup
> -Wl,-search_paths_first -arch i386 -fstack-protector'
> >
> > Locally applied patches:
> >     ACTIVEPERL_LOCAL_PATCHES_ENTRY
> >
> > ---
> > @INC for perl 5.14.2:
> >     /Users/sdd/Library/ActivePerl-5.14/lib
> >     /usr/local/ActivePerl-5.14/site/lib
> >     /usr/local/ActivePerl-5.14/lib
> >     .
> >
> > ---
> > Environment for perl 5.14.2:
> >     DYLD_LIBRARY_PATH (unset)
> >     HOME=/Users/sdd
> >     LANG (unset)
> >     LANGUAGE (unset)
> >     LD_LIBRARY_PATH (unset)
> >     LOGDIR (unset)
> >
>
> PATH=/Users/sdd/.config-dir/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/bin/g4bin:/usr/local/sbin:/usr/X11/bin:/opt/X11/bin
> >     PERL_BADLANG (unset)
> >     SHELL=/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