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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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: g2news2.google.com!postnews.google.com!o14g2000yqe.googlegroups.com!not-for-mail From: Julian Leyh Newsgroups: comp.lang.ada Subject: Re: Cannot summate small float values Date: Mon, 22 Nov 2010 00:35:55 -0800 (PST) Organization: http://groups.google.com Message-ID: <069cc8aa-6527-4710-9258-4b93287565b7@o14g2000yqe.googlegroups.com> References: <8kq1usFojgU1@mid.individual.net> NNTP-Posting-Host: 194.156.172.86 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1290414955 14732 127.0.0.1 (22 Nov 2010 08:35:55 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 22 Nov 2010 08:35:55 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: o14g2000yqe.googlegroups.com; posting-host=194.156.172.86; posting-account=4IMjSwoAAABghF4GBOy5ozdaZM8EkGwR User-Agent: G2/1.0 X-HTTP-Via: 1.0 localhost (squid/3.1.3) X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:16605 Date: 2010-11-22T00:35:55-08:00 List-Id: On 21 Nov., 22:06, tolkamp wrote: > 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 syntacti= c > > 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 ma= y > > 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. This should be taught in basics of computer nummerics or processor architecture. Have a look at IEEE 754 floating point arithmetic.