24.05.2014 Views

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

XL Fortran Enterprise Edition for AIX : User's Guide - IBM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

How <strong>XL</strong> <strong>Fortran</strong> Rounds Floating-Point Calculations<br />

Understanding rounding operations in <strong>XL</strong> <strong>Fortran</strong> can help you get predictable,<br />

consistent results. It can also help you make in<strong>for</strong>med decisions when you have to<br />

make tradeoffs between speed and accuracy.<br />

In general, floating-point results from <strong>XL</strong> <strong>Fortran</strong> programs are more accurate than<br />

those from other implementations because of MAF operations and the higher<br />

precision used <strong>for</strong> intermediate results. If identical results are more important to<br />

you than the extra precision and per<strong>for</strong>mance of the <strong>XL</strong> <strong>Fortran</strong> defaults, read<br />

“Duplicating the Floating-Point Results of Other Systems” on page 295.<br />

Selecting the Rounding Mode<br />

To change the rounding mode in a program, you can call the fpsets and fpgets<br />

routines, which use an array of logicals named fpstat, defined in the include files<br />

/usr/include/fpdt.h and /usr/include/fpdc.h. The fpstat array elements correspond<br />

to the bits in the floating-point status and control register.<br />

For floating-point rounding control, the array elements fpstat(fprn1) and<br />

fpstat(fprn2) are set as specified in the following table:<br />

Table 18. Rounding-Mode Bits to Use with fpsets and fpgets<br />

fpstat(fprn1) fpstat(fprn2) Rounding Mode Enabled<br />

.true. .true. Round towards -infinity.<br />

.true. .false. Round towards +infinity.<br />

.false. .true. Round towards zero.<br />

.false. .false. Round to nearest.<br />

For example:<br />

program fptest<br />

include ’fpdc.h’<br />

call fpgets( fpstat ) ! Get current register values.<br />

if ( (fpstat(fprn1) .eqv. .false.) .and. +<br />

(fpstat(fprn2) .eqv. .false.)) then<br />

print *, ’Be<strong>for</strong>e test: Rounding mode is towards nearest’<br />

print *, ’ 2.0 / 3.0 = ’, 2.0 / 3.0<br />

print *, ’ -2.0 / 3.0 = ’, -2.0 / 3.0<br />

end if<br />

call fpgets( fpstat ) ! Get current register values.<br />

fpstat(fprn1) = .TRUE. ! These 2 lines mean round towards<br />

fpstat(fprn2) = .FALSE. ! +INFINITY.<br />

call fpsets( fpstat )<br />

r = 2.0 / 3.0<br />

print *, ’Round towards +INFINITY: 2.0 / 3.0= ’, r<br />

call fpgets( fpstat ) ! Get current register values.<br />

fpstat(fprn1) = .TRUE. ! These 2 lines mean round towards<br />

fpstat(fprn2) = .TRUE. ! -INFINITY.<br />

call fpsets( fpstat )<br />

r = -2.0 / 3.0<br />

print *, ’Round towards -INFINITY: -2.0 / 3.0= ’, r<br />

end<br />

! This block data program unit initializes the fpstat array, and so on.<br />

292 <strong>XL</strong> <strong>Fortran</strong> <strong>Enterprise</strong> <strong>Edition</strong> <strong>for</strong> <strong>AIX</strong> : User’s <strong>Guide</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!