(j3.2006) SQRT of square to ABS

Robert Corbett Robert.Corbett
Tue Jan 8 21:02:21 EST 2008

Malcolm Cohen wrote:

> On Tue, 08 Jan 2008 14:34:55 +0900, Robert Corbett <Robert.Corbett at sun.com> wrote:
>>If the modules IEEE_EXCEPTIONS and IEEE_FEATURES are used in a
>>program unit, may a standard-conforming implementation transform
>>the expression SQRT(X**2) into ABS(X)?
> Probably.  **2 is not an IEEE operation, and neither is ABS(X).
> OTOH, it probably ought not to transform SQRT(X*X) into ABS(X).
>>The first expression might signal
> Never mind signalling, if X*X overflows it returns a different result,
> viz +INF.  You only need to use the relevant constants out of IEEE_FEATURES
> to get that guaranteed.  Maybe.
> (Maybe not on x87, but that's a horse of a different colour;
> 754 isn't a sharp enough instrument for that analysis.)
> Didn't the user write SQRT(X**2) instead of ABS(X) deliberately because he
> wanted to get the infinity/overflow stuff?

No.  The question was motivated by a user request that SQRT(X**2) be
optimized to ABS(X).  Sun's compiler does not do that optimization, but
apparently some other compilers do.  The question about compatibility
with the IEEE modules originated with me, not the user.  I suspect the
user has no idea that the IEEE modules exist.

I do not know why the user wrote SQRT(X**2) instead of ABS(X), but I
doubt it was because he wanted to get overflow, underflow and imprecise
exceptions.  I conjecture that the user who asked for the optimization
is not the programmer who wrote the original code.

> If he also wrote
> it is more than reasonable to think that he wanted what he asked for!
> We shouldn't make him have to assign all intermediate results to VOLATILE
> variables just to get the IEEE semantics he already asked for...

Bob Corbett

More information about the J3 mailing list