[vox-tech] C and IEEE-754

Micah J. Cowan micah at cowan.name
Wed Jun 7 13:30:50 PDT 2006


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
quite a bit. But the best format for every machine isn't IEEE-754, IBM
and Cray have used their own formats for floating point (though, AIUI,
for the most part they have supported IEEE-754 in addition to their
own).

Note that C doesn't even have particular requirements on the radix used
by the floating point model (i.e., it doesn't have to be 2. Hell, it
could be 10!).

However, you can test for the existence of a macro, __STDC_IEC_559__,
whose existence guarantees conformity to IEC 60559:1989 floating point
(which is the current designation for ANSI/IEEE 754-1989, which was also
designated IEC 559:1989 before it went to 60559. If that macro is
defined, then you are free to assume the usual semantics for floating
point.

C++ uses a completely different approach . You use the numeric_limits<>
type defined in the <limits> header:

  if (std::numeric_limits<double>::is_iec559) {
    ...
  }

AFAICT, the __STDC_IEC_559__ macro is only available in C99; my draft
copy of C90 doesn't mention it (but does refer to IEEE 754 in some
examples).

However, <float.h> provides a number of definitions useful in describing
the floating-point model used, and there are certain guarantees made
about them (such as a double being convertable to 10 decimal digits and
back without loss of information).

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


More information about the vox-tech mailing list