Message from discussion
An strange point
Path: g2news2.google.com!postnews.google.com!j8g2000yql.googlegroups.com!not-for-mail
From: Arjen Markus <arjen.mar...@wldelft.nl>
Newsgroups: comp.lang.fortran
Subject: Re: An strange point
Date: Wed, 1 Apr 2009 07:28:10 -0700 (PDT)
Organization: http://groups.google.com
Lines: 64
Message-ID: <e89b7433-abbb-4cc8-9436-1dfda5972b59@j8g2000yql.googlegroups.com>
References: <bd511e22-1b33-4f3c-b2b6-71c2f42af9af@j39g2000yqn.googlegroups.com>
NNTP-Posting-Host: 145.9.166.23
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1238596117 22358 127.0.0.1 (1 Apr 2009 14:28:37 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 1 Apr 2009 14:28:37 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: j8g2000yql.googlegroups.com; posting-host=145.9.166.23;
posting-account=A91wAAoAAADgBUxBX6QqsrSD26GLhVp8
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR
1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648;
.NET CLR 3.0.4506.2152; .NET CLR 3.5.30729),gzip(gfe),gzip(gfe)
On 1 apr, 16:08, Fatemeh <fateme.mirj...@gmail.com> wrote:
> Dear all ;
>
> I wrote a program which has a bug when the value of u is greater than
> 1.4.
> when I tried to find the bug for values of y greater than 1.4 I saw an
> strange point :
>
> u=3D0.D0
>
> do while (u.lt.20.D0)
> =A0 =A0 .
> =A0 =A0 .
> =A0 =A0 .
> =A0 =A0print*,u, y
> =A0 =A0if (u=3D=3D1.4D0) print*,"ok"
> =A0 =A0.
> =A0 =A0.
> =A0 =A0.
> =A0 u=3Du+0.1D0
> end do
> ------------------------
> the output:
> =A0 =A00.00000000000000 =A0 =A0 =A0-0.900316316157106
> =A0 0.100000000000000 =A0 =A0 =A0-0.896557866215700
> =A0 0.200000000000000 =A0 =A0 =A0-0.892752298842229
> =A0 0.300000000000000 =A0 =A0 =A0-0.889009961947742
> =A0 0.400000000000000 =A0 =A0 =A0-0.885300844120634
> =A0 0.500000000000000 =A0 =A0 =A0-0.881608066131983
> =A0 0.600000000000000 =A0 =A0 =A0-0.877943572939470
> =A0 0.700000000000000 =A0 =A0 =A0-0.874308949879819
> =A0 0.800000000000000 =A0 =A0 =A0-0.870674660655724
> =A0 0.900000000000000 =A0 =A0 =A0-0.867096152952160
> =A0 =A01.00000000000000 =A0 =A0 =A0-0.863546597445969
> =A0 =A01.10000000000000 =A0 =A0 =A0-0.860024078786788
> =A0 =A01.20000000000000 =A0 =A0 =A0-0.856526008002337
> =A0 =A01.30000000000000 =A0 =A0 =A0-0.853103843918795
> =A0 =A01.40000000000000 =A0 =A0 =A0-0.849649287329888
>
> but the program cannot print "ok" while it prints u=3D1.40000000000000 .
> why???
>
> I have got confused. Is there anyone can help me?
>
> Thanks
The classic problem of floating-point data: a finite binary
representation
is not capable of exactly representing 1.4, just a finite decimal
representation
can not represent 1/3 exactly.
You need to define a small margin, like:
if (abs(u-1.4D0) < 2.0*epsilon(u)*1.4d0) print*,"ok"
where epsilon() is a standard function returning the smallest number E
such that 1 and 1+E can be distinguished.
It has everything to do with the finite precision of computers.
Regards,
Arjen