Skip to content
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

sprintf partial at rounding #914

Closed
p5pRT opened this issue Dec 3, 1999 · 2 comments
Closed

sprintf partial at rounding #914

p5pRT opened this issue Dec 3, 1999 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Dec 3, 1999

Migrated from rt.perl.org#1850 (status was 'resolved')

Searchable as RT1850$

@p5pRT
Copy link
Author

p5pRT commented Dec 3, 1999

From aam@astro.caltech.edu

Hi,

I came across the curious fact while using sprintf to round off
numbers that it does not like odd numbers. sprintf rounds
3.5 to 4 as it should (because 3.0 rounds off to 3)
but it also rounds off 4.5 to 4. In fact all semi-integral numbers
are rounded off to even numbers!

Here is a sample program​:

#!/usr/local/bin/perl -w
for($i=-10.5;$i<10.5;++$i){
$rounded=sprintf("%.0f",$i);
print "$rounded ";
}
print "\n";

The program prints​:

-10 -10 -8 -8 -6 -6 -4 -4 -2 -2 -0 0 2 2 4 4 6 6 8 8 10

What sprintf is doing seems very weird and would certainly need
more computation than it should be using.

I do nto think I have any solution but would appreciate one
since I presently have to do more complex things to round off
numbers​:
$i=$i-int($i)<0.5?int($i)​:1+int($i);

Cheers,
ashish mahabal
aam@​astro.caltech.edu

Perl Info


Site configuration information for perl 5.00404:

Configured by cld at Mon Sep  7 15:53:59 PDT 1998.

Summary of my perl5 (5.0 patchlevel 4 subversion 4) configuration:
  Platform:
    osname=solaris, osvers=2.6, archname=sun4-solaris
    uname='sunos phobos 5.6 generic sun4u sparc sunw,ultra-1 '
    hint=recommended, useposix=true, d_sigaction=define
    bincompat3=y useperlio=undef d_sfio=undef
  Compiler:
    cc='gcc', optimize='-O', gccversion=2.8.1
    cppflags='-I/usr/local/include'
    ccflags ='-I/usr/local/include'
    stdchar='unsigned char', d_stdstdio=define, usevfork=false
    voidflags=15, castflags=0, d_casti32=define, d_castneg=define
    intsize=4, alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib /usr/ccs/lib
    libs=-lsocket -lnsl -ldl -lm -lc -lcrypt
    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'

Locally applied patches:
	


@INC for perl 5.00404:
	/usr/local/lib/perl5/sun4-solaris/5.00404
	/usr/local/lib/perl5
	/usr/local/lib/perl5/site_perl/sun4-solaris
	/usr/local/lib/perl5/site_perl
	.


Environment for perl 5.00404:
    HOME=/home/ganesha/aam
    LANG=C
    LD_LIBRARY_PATH=/usr/openwin/lib:/usr/local/lib:/usr/local/opt/SUNWspro/lib:/usr/local/pgplot:~/progs/scheme/scm/scsh-0.5.0/lib:/usr/openwin/lib:/usr/local/lib:/usr/local/opt/SUNWspro/SC4.0/lib:/usr/lib:/usr/local/iraf/iraf/lib:/usr/local/pgplot:/home/minerva/skybase3/lib:/usr/local/lib/netscape/rvplayer5.0:/home/minerva/skybase3/lib
    LOGDIR (unset)
    PATH=/usr/local/frame/bin:/usr/local/skicat/focas/bin:/usr/local/skicat/focas/bin:/home/kalypso/skicat2/bin:/usr/local/skicat/bin:/usr/local/skicat/auxiliary:/usr/openwin/bin:/opt/SUNWspro/bin:/usr/ccs/bin:/usr/sbin:/usr/dt/bin:/usr/openwin/bin:/usr/bin:/bin:/etc:/usr/local/bin:.:/usr/bin:/bin:/usr/ucb:/usr/etc:/usr/local/bin:/home/ganesha/aam/progs/scheme/scm/scsh-0.5.0/bin:/home/ganesha/aam/progs/csh:/home/galatea/sco/bin/morpho_ex:/home/ganesha/aam/dposs_bin:/home/ganesha/aam/bin:/home/ganesha/aam/progs/scheme/scm/scsh-0.5.0:/usr/sbin:/usr/ccs/bin:/opt/SUNWspro/bin:/home/minerva/skybase3/bin:/usr/local/opt/dophot:/usr/local/opt/dophot/extras:/usr/local/opt/dophot/postdaop:/home/minerva/skybase3/bin:/home/galatea/sco/bin:/home/galatea/sco/bin/dposs_ex/ex:/home/galatea/sco/bin/morpho_ex:/scr8/dposs/aam/wcs/wcstools-2.6.9/bin:.
    PERL_BADLANG (unset)
    SHELL=/usr/local/bin/tcsh

@p5pRT
Copy link
Author

p5pRT commented Dec 3, 1999

From @tamias

On Fri, Dec 03, 1999 at 06​:36​:16PM -0800, Ashish Mahabal wrote​:

I came across the curious fact while using sprintf to round off
numbers that it does not like odd numbers. sprintf rounds
3.5 to 4 as it should (because 3.0 rounds off to 3)
but it also rounds off 4.5 to 4. In fact all semi-integral numbers
are rounded off to even numbers!

Have you come across
perldoc -q round
?

Ronald

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

No branches or pull requests

1 participant