From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,86d4e48d5a9b02a1 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!y23g2000yqd.googlegroups.com!not-for-mail From: tolkamp Newsgroups: comp.lang.ada Subject: Re: Cannot summate small float values Date: Sun, 21 Nov 2010 13:06:47 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <8kq1usFojgU1@mid.individual.net> NNTP-Posting-Host: 77.248.181.38 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1290373608 30772 127.0.0.1 (21 Nov 2010 21:06:48 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 21 Nov 2010 21:06:48 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: y23g2000yqd.googlegroups.com; posting-host=77.248.181.38; posting-account=nZAcngoAAACcfYM9wDw3w9Z1XR3bObfs User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.1; .NET4.0C),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:15631 Date: 2010-11-21T13:06:47-08:00 List-Id: On 20 nov, 14:49, Niklas Holsti wrote: > tolkamp wrote: > > When I summate Float values smaller then 1.0E-6 then the summation is > > not done. > > > Code Example: > > > X, Dx : Float; > > X :=3D 0.0; > > Dx :=3D 1.0E-7; > > lwhile X < =A01.0 loop > > =A0 =A0 X =3D X + Dx; > > =A0 =A0 Float_Io.Put(X, 3,9,0); New_Line; > > end loop; > > Certainly the addition is done. Your program (after some small syntactic > corrections) prints: > > =A0 =A00.000000100 > =A0 =A00.000000200 > =A0 =A00.000000300 > =A0 =A00.000000400 > =A0 =A00.000000500 > =A0 =A00.000000600 > =A0 =A00.000000700 > > and so on. If your program prints out something else, please show the > source code of your whole program, exactly as you compile and run it. > Don't re-type it into your message. > > However, when X approaches 1.0, at some point the addition of 1.0E-7 may > be lost in round-off, since it is close to the precision limit of the > Float type, relative to 1.0. On my system (Debian, Gnat) the X variable > does reach 1.0 and the program stops. > > What are you really trying to do? There are probably safer and more > accurate ways of doing it. > > Here is the program that I used: > > with Ada.Text_IO; > with Ada.Float_Text_IO; > > procedure Sums > is > =A0 =A0 use Ada.Text_IO, Ada.Float_Text_IO; > =A0 =A0 X, Dx : Float; > begin > =A0 =A0 X :=3D 0.0; > =A0 =A0 Dx :=3D 1.0E-7; > =A0 =A0 while X < =A01.0 loop > =A0 =A0 =A0 =A0 X :=3D X + Dx; > =A0 =A0 =A0 =A0 Put(X, 3,9,0); New_Line; > =A0 =A0 end loop; > end Sums; > > -- > Niklas Holsti > Tidorum Ltd > niklas holsti tidorum fi > =A0 =A0 =A0 =A0. =A0 =A0 =A0@ =A0 =A0 =A0 . Thank you your reaction. Using your procedure Sums I found out that when the start value of X < 0.24 the summation works correct with Dx =3D 1.0E-8 When start X > 0.25 the summation remains 0.250000000.