Questions about ANSI constant expressions

Norman Diamond diamond at csl.sony.co.jp
Tue Sep 26 22:13:37 AEST 1989


According to the standard, a compiler may optionally fold the
following constants:

[a]    a = 5.0 + 6.0;

[b]    b = 5 + 6;

[c]    c = 5000000000 + 6000000000;  /* five billion + six billion */

If the compiler chooses to fold [a], it is required to provide at least
the same range and precision at compile time as the target machine
will provide at execution time.

If the compiler chooses to fold [b] or [c], the standard does not seem
to place any restriction on compile time accuracy.

Intuitively, [b] might be expected to fold properly, because the host
machine might be required to handle longs of up to around two billion
(there might be a self-compiler on the host machine).  But does the
standard actually require this?

Case [c] becomes interesting if the host machine has 32-bit longs but
the target machine has 64-bit longs.  A sensible implementor might
decline to fold the constant.  I believe the standard permits a lack
of folding, and it permits correct folding.  But does it actually
require one of these?  As far as I can tell, it permits incorrect
folding as well.

The situation becomes more important in the case of a preprocessor
"#if" directive.  Cases [b] and [c] are required to be computed at
compile-time, and accuracy must be guaranteed up to a range of about
two billion.  So case [b] must be folded properly in the case of an
"#if".  What about case [c]?  Is the preprocessor required to simulate
the target machine's range, or is it allowed to produce incorrect
results when they exceed approx. two billion?

(Thanks to a friend, who probably prefers to remain anonymous for
kindly providing an illegal copy of the draft standard.)

--
-- 
Norman Diamond, Sony Corporation (diamond at ws.sony.junet)
  The above opinions are inherited by your machine's init process (pid 1),
  after being disowned and orphaned.  However, if you see this at Waterloo or
  Anterior, then their administrators must have approved of these opinions.



More information about the Comp.std.c mailing list