Sun Java Solaris Communities My SDN Account Join SDN

Article

Fortran 95 SIGN Change

 
By Michael Ingrassia, Sun Studio Solaris Tools Engineering  
The specified behavior of the SIGN(A,B) intrinsic function changed in a subtle way with the adoption of the Fortran 95 standard. Did you notice it? Does it affect your codes?
 
Using Compatible Compilers

Did you notice the change in the specs for SIGN in Fortran 95? Recall that SIGN(A, B) is the Fortran intrinsic function which delivers the magnitude of A with the sign of B. For example, SIGN(-3.0,2.0) has the value 3.0.

The interesting case is when the second argument is zero, because on platforms that support IEEE arithmetic (including Sun SPARC and UltraSPARC platforms), zero is actually a signed number. There is a positive zero and a negative zero. They are hard to tell apart in ordinary Fortran code, but numerical experts can take advantage of the signedness of zero to devise special algorithms. (See the Numerical Computation Guide.) Most programs can safely ignore the distinction.

In Fortran 90, the processor was required always to deliver a positive result for SIGN(A, B) if B was zero.

But in Fortran 95, the processor is allowed to do the correct thing and deliver ABS(A) when B is a positive zero and -ABS(A) when B is a negative zero. This change in the specification becomes apparent only when B is of type real, and is zero, and the processor is capable of distinguishing between positive and negative zero, and B is negative real zero. Then SIGN delivers a negative result where, under Fortran 90 rules,  it used to return a positive result.

The Sun Studio Fortran 95 compiler generally tracks the latest standard. So you won't see the old Fortran 90 behavior with current library releases. If you require the Fortran 90 behavior, the easiest thing to do is to make sure that B cannot ever be negative real zero when you call SIGN. For example, you might try

IF (B .EQ. 0.0) B = +0.0
which may look like it does nothing but actually replaces negative real zero with positive real zero.

For More Information
 
Sun Studio Documentation
 
Fortran 95 Standards Documents
 
 

About the Author

Michael Ingrassia is the Sun Studio Fortran compiler technical lead, and the representative for Sun Microsystems at the ANSI Fortran standards committee.