delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2007/09/27/11:54:57

X-Recipient: archive-cygwin AT delorie DOT com
X-Spam-Check-By: sourceware.org
To: cygwin AT cygwin DOT com
From: Steve Holden <steve AT holdenweb DOT com>
Subject: Re: strange behavior with perl v 5.8.8
Date: Thu, 27 Sep 2007 11:44:43 -0400
Lines: 71
Message-ID: <fdgj5b$83c$1@sea.gmane.org>
References: <5abc24640709270814j5148c43ejbfe46f022fd6ad1b AT mail DOT gmail DOT com>
Mime-Version: 1.0
User-Agent: Thunderbird 2.0.0.6 (Windows/20070728)
In-Reply-To: <5abc24640709270814j5148c43ejbfe46f022fd6ad1b@mail.gmail.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
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

Andrew Louie wrote:
> Hello,
> 
> I've run into a strange adding problem with perl:
> 
> when incrementing by 0.1 i get a strange behavior where at some
> arbitrary  number, it will append 0.9999999999 to the end of the
> number.
> 
> test case:
> ------------------------------------------------------------------------------------
> my $start = 0;
> my $interval = 0.1;
> my $end = 10;
> 
> do{ print "start: $start\n"; $start += $interval;}until($start >= $end);
> ------------------------------------------------------------------------------------
> Results:
> ...
> start: 4.5
> start: 4.6
> start: 4.7
> start: 4.8
> start: 4.9
> start: 5
> start: 5.1
> start: 5.2
> start: 5.3
> start: 5.4
> start: 5.5
> start: 5.6
> start: 5.7
> start: 5.8
> start: 5.9
> start: 5.99999999999999
> start: 6.09999999999999
> start: 6.19999999999999
> start: 6.29999999999999
> start: 6.39999999999999
> start: 6.49999999999999
> start: 6.59999999999999
> start: 6.69999999999999
> start: 6.79999999999999
> start: 6.89999999999999
> ------------------------------------------------------------------------------------
> 
> why all of suddun i get 5.999999999?
> 
Because binary floating-point numbers can't represent decimal values 
like 0.1 exactly.

> Is this a perl problem?
> 
No, it's not a Cygwin problem either. It's a floating-point problem. We 
have to deal with such queries all the time in the Python community:

http://www.python.org/doc/faq/general/#why-am-i-getting-strange-results-with-simple-arithmetic-operations

[...]

The correct way out of this is to use integer variables to control the 
loop and calculate the start value each time around.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline


--
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/

- Raw text -


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