[vox-tech] C and IEEE-754

Micah J. Cowan micah at cowan.name
Wed Jun 7 14:43:45 PDT 2006


On Wed, Jun 07, 2006 at 02:10:56PM -0700, Richard Harke wrote:
> On Wed June 7 2006 13:30, Micah J. Cowan wrote:
> > On Wed, Jun 07, 2006 at 02:48:36PM -0500, Ken Bloom wrote:
> > > On Wed, Jun 07, 2006 at 03:41:02PM -0400, Peter Jay Salzman wrote:
> > > > I started to read:
> > > >
> > > >    http://www.cs.princeton.edu/introcs/91float/
> > > >
> > > > and came across an interesting comment:
> > > >
> > > >    "Java uses a subset of the IEEE 754 binary floating point standard
> > > > to represent floating point numbers and define the results of
> > > > arithmetic operations. Most machines conform to this standard, although
> > > > some languages (C, C++) do not guarantee that this is the case."
> > > >
> > > > It's a poorly written paragraph, but seems to say that C and C++ don't
> > > > guarantee adherence to the IEEE 754 standard.  If this really is the
> > > > case, why don't they?
> > >
> > > I suppose if your hardware supports something else instead of
> > > IEEE-754, then a conforming C/C++ implementation can use the hardware,
> > > rather than having to emulate IEEE-754.
> >
> > In fact, IIRC, C predates IEEE('s very existance as an orginazation) by
> IEEE is actually quite an old organization around 100 yrs, I believe.
> IEEE has sections for power generation and transmission, and many others,
> not just computers. 

Doh! Of course, you are right. I was guessing on that. I meant to look
it up before saying such a thing, but I forgot.

However, I /believe/ I'm right when I say that it at least predates IEEE
754... which AFAICT comes from ~1987? Of course, I have no idea in what
forms it existed before it become an international standard. Certainly,
it predates C's first forray into standardization.

<snip>

> For a long time, there has been no way in C to control floating pt modes
> but I think recently some effort has been made. Rounding modes in
> particular can be quite important in some computations.
> I think C mostly goes with what is expedient with the hardware in use.
> On x86, the hardware float registers are actually 82 bits. This can be
> accessed directly with some compilers as long double But, OTOH, if you
> specify double it will be expaned to long double when loaded into
> a register. Now if it stays in the register through a series of operations,
> the result can subtly different than if it was rounded to double after
> every intermediate operation.

Yeah. Which was frustrating to implementors, because in order to be 100%
conforming, they _had_ to do the stupid truncations every single
operation. Which is why GCC added -funsafe-math-operations, I think.

So C99 (1) allowed these optimizations to take place by default, and (2)
defined a standard #pragma directive to control it (FP_CONTRACT).

Of course, this is all AIUI: I haven't nearly the intimate knowledge of
C and floating-point that many implementors and various folks at
comp.lang.c have; so better to check with them for details.

-- 
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/


More information about the vox-tech mailing list