comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Floating-Point Numbers and Internal Representation
Date: Mon, 5 Dec 2005 17:38:57 -0600
Date: 2005-12-05T17:38:57-06:00	[thread overview]
Message-ID: <96qdnRH7F-MGUwneRVn-vw@megapath.net> (raw)
In-Reply-To: 1843142.NoXPLYbHQs@linux1.krischik.com

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







  parent reply	other threads:[~2005-12-05 23:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-04 10:33 Floating-Point Numbers and Internal Representation Matthias Kretschmer
2005-12-04 11:12 ` Martin Krischik
2005-12-04 14:11   ` Martin Dowie
2005-12-04 14:50     ` Dmitry A. Kazakov
2005-12-04 15:15       ` Matthias Kretschmer
2005-12-04 18:14         ` Dmitry A. Kazakov
2005-12-04 21:16           ` Matthias Kretschmer
2005-12-06  2:54     ` Steve
2005-12-05 23:38   ` Randy Brukardt [this message]
2005-12-04 11:46 ` Dmitry A. Kazakov
2005-12-04 21:29 ` Gautier Write-only
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox