delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/06/30/08:07:27

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Date: Thu, 30 Jun 2005 14:05:51 +0200 (CEST)
Message-ID: <8864897.1120133151080.JavaMail.adm-moff@moffice2.nsc.no>
From: "Peter J. Acklam" <pjacklam AT online DOT no>
To: <haro AT kgt DOT co DOT jp>
To: <pjacklam AT online DOT no>
Subject: Re: Bug in printf ?
Cc: <cygwin AT cygwin DOT com>
In-Reply-To: <20050630.202554.129758646.haro@kgt.co.jp>
Mime-Version: 1.0
References: <20050630 DOT 194259 DOT 46635312 DOT haro AT kgt DOT co DOT jp> <6632806 DOT 1120129611257 DOT JavaMail DOT adm-moff AT moffice2 DOT nsc DOT no> <20050630 DOT 202057 DOT 83613389 DOT haro AT kgt DOT co DOT jp> <20050630 DOT 202554 DOT 129758646 DOT haro AT kgt DOT co DOT jp>
X-IsSubscribed: yes

------=_Part_8523_1953061.1120133151075
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit

haro AT kgt DOT co DOT jp wrote:

> ::How come "0.125" gets printed as "0.12", and not "1.3"?
>                                                     ^^^ "0.13", off cource ;-)

Dealing with integers illustrates the matter more clearly. When
the decimal value is exactly 0.5, then printf should round to the
nearest *even* integer, as far as I know, so you should get

   0   -> 0
   0.5 -> 0
   1   -> 1
   1.5 -> 2
   2   -> 2
   2.5 -> 2
   3   -> 3
   3.5 -> 4
   4   -> 4
   4.5 -> 4
   5   -> 5

Now I realize that Cygwin's printf doesn't get it right, because

   seq 0 .5 5 | while read x; do printf '%-3s -> %.0f\n' $x $x; done

gives

   0   -> 0
   0.5 -> 1
   1   -> 1
   1.5 -> 1
   2   -> 2
   2.5 -> 2
   3   -> 3
   3.5 -> 3
   4   -> 4
   4.5 -> 4
   5   -> 5

Both Solaris' /bin/printf and Perl's printf() give the right
output.

In your examples there is also an additional issue, which is how
numerical values are represented.  Here is Perl:

   seq .105 .01 .155 | while read x; do printf '%-5s -> %.2f\n' $x $x; done

   0.105 -> 0.10
   0.115 -> 0.12
   0.125 -> 0.12
   0.135 -> 0.14
   0.145 -> 0.14
   0.155 -> 0.15

They all seem correct expect the last one.  The reason why 0.155
becomes 0.15 and not 0.16 is that 0.115 can not be represented
exactly.  It is represented as a number which is slightly
*smaller* than 0.155, so it becomes 0.15 after rounding.

Peter


------=_Part_8523_1953061.1120133151075
Content-Type: text/plain; charset=us-ascii

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/
------=_Part_8523_1953061.1120133151075--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019