(j3.2006) SQRT of square to ABS

Robert Corbett Robert.Corbett
Tue Jan 8 21:53:05 EST 2008


Van Snyder wrote:
> On Tue, 2008-01-08 at 18:02 -0800, Robert Corbett wrote:
> 
>>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.
> 
> 
> Is X complex?

No, it is REAL.

> If so, then sqrt(X**2) isn't abs(X), but rather
> cmplx(abs(real(X)),aimag(X),kind(X))

Not quite.  The standard requires SQRT to return the principal root.
For example, SQRT((-1.0, -1.0)**2) returns (1.0, 1.0).

Bob Corbett



More information about the J3 mailing list