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,37e5589e32d8f03f X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.megapath.net!news.megapath.net.POSTED!not-for-mail NNTP-Posting-Date: Mon, 05 Dec 2005 17:34:51 -0600 From: "Randy Brukardt" Newsgroups: comp.lang.ada References: <1843142.NoXPLYbHQs@linux1.krischik.com> Subject: Re: Floating-Point Numbers and Internal Representation Date: Mon, 5 Dec 2005 17:38:57 -0600 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4952.2800 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4952.2800 Message-ID: <96qdnRH7F-MGUwneRVn-vw@megapath.net> NNTP-Posting-Host: 64.32.209.38 X-Trace: sv3-imnffjaXesMnzT7JwIaU3mPR9ZpfM88acRvIJge6Mv2PFp/6V04npKuexzFsPy3jLZfwLIpvsgocUPg!4Cf3yIc8AAI5764mLwGDYQUqWZD7pZ1i3W9WpA0nZXwBx3EJCXcMZYjQhffFLbYtdXHGswCHVIvC X-Complaints-To: abuse@megapath.net X-DMCA-Complaints-To: abuse@megapath.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news1.google.com comp.lang.ada:6742 Date: 2005-12-05T17:38:57-06:00 List-Id: > Matthias Kretschmer wrote: > > > Hello, > > > > I had a problem in one of my programs, that was caused by the internal > > representation of floating-point numbers in the fpu. Concrete: > > calculating the value of an optimum for some large number of objects, > > then in a second doing something with all optimal objects. The problem > > was, that when doing the calculation the second time, the compiler left > > the floating-point number in the fpu which had a higher precision than > > the representation I choosed, so comparing for equality returns always > > "False". The problem would be solved by some operation truncating the > > floating-point number to the precision I orginally wanted or used. I > > could of course put all the values in an array or list and then finding > > optimum and optimal objects, but I don't want to go this way. In C iirc > > I could use a volatile variable to ensure the compiler will put the > > value in and read from the variable before comparing, but to achieve > > something similiar (truncating the precision to that of the type used) > > in Ada? If you're only interested in using the memory precision, you can use the Machine attribute, see A.5.3(60-62). http://www.adaic.com/standards/95lrm/html/RM-A-5-3.html But, as others have said, that may not be the best solution, as direct comparison of float values for equality is often dubious. It's also relatively expensive on some machines (such as the Intel Pentium processors), where values in registers are always kept in extended precision; dropping that precision usually requires writing the values to memory and back. So it's best to avoid this attribute in performance critical code portions. Randy.