Examples | 
A | 
 ![]()  | 
This appendix is organized into the following sections:
 
 
 
 IEEE Arithmetic 
This section shows how to examine the stored IEEE hexadecimal representation of floating-point numbers. Examining Stored IEEE hexadecimal Representations 
These examples show a way of examining the hexadecimal representation of floating-point numbers. Note that you can also use the debuggers to look at the hexadecimal representation of stored data, as well as write programs based on other ideas. 
The output of this program looks like (SPARC): 
DP Approx pi = 400921fb 54442d18 = 3.14159265358979312e+00 Single Precision Infinity: 7f800000  | 
	implicit real*16 (q)
	...
	z = q_min_normal()
	write(*,7) z, z
 7	format('min normal, quad: ',1pe47.37e4,/,' in hex ',z32.32)
  | 
The corresponding output looks like (SPARC): 
 
 The Math Libraries
In this section are a few examples that use functions from the math library.  Random Number Generator
The following example calls a random number generator to generate an array of numbers, and uses a timing function to measure the time it takes to compute the EXP of the given numbers: 
Note that compilation must be done using either the -DDP or -DSP flag. The suffix is F instead of f so that the preprocessor is invoked automatically. 
This FORTRAN example uses some functions recommended by the IEEE standard: 
The output from this program looks like: 
 ieee_values
This C program calls several of the ieee_values(3m) functions: 
When linking, you use both -lm and -lsunmath.  
quiet NaN: NaN = 7fffffff ffffffff 
nextafter(max_subnormal,0) = 2.2250738585072004e-308 
                           = 000fffff fffffffe 
single precision min subnormal = 1.40129846e-45 = 00000001 
 | 
(Intel and PowerPC) The output is similar to: 
quiet NaN: NaN = ffffffff 7fffffff 
nextafter(max_subnormal,0) = 2.2250738585072004e-308 
                           = fffffffe 000fffff
single precision min subnormal = 1.40129846e-45 = 00000001 
 | 
	implicit real*16 (q)
    ...
	real*16 z, zero, one
    ...
	zero = 0.0
	one = 1.0
	z = q_nextafter(zero, one)
    ...
	print *, z
 | 
The output is similar to (SPARC):
     6.4751751194380251109244389582276466-4966
  Infinity              
     3.40282E+38 
 | 
 ieee_flags -- Rounding Direction
The following example demonstrates how to set the rounding mode to 
round towards zero:
(SPARC) The output of this short program shows the effects of rounding towards zero: 
(Intel and PowerPC) The output of this short program shows the effects of rounding towards zero: 
Set rounding direction towards zero from a FORTRAN program:  
Rounding direction is: tozero Note: Rounding direction toward zero. See the Numerical Computation Guide, ieee_flags(3M)  | 
 Exceptions and Exception Handling
 ieee_flags -- Accrued Exceptions
Generally, a user program examines or clears the accrued exception bits. Here is a C program that examines the accrued exception flags: 
The output from running this program: 
The same can be done from FORTRAN:
Highest priority exception is underflow 0 0 0 1 1  | 
For a SPARC machine, the exception flags have the following definitions: 
/* *from <sys/ieeefp.h>, the exceptions according to bit number are * fp_inexact = 0, * fp_division = 1, * fp_underflow = 2, * fp_overflow = 3, * fp_invalid = 4 */  | 
(Intel) The output of the Intel C example is:    
out is: division , fp exception code is: 2  | 
 ieee_handler -- Trapping Exceptions
Here is a FORTRAN program that installs a signal handler to locate an exception (for SPARC and PowerPC systems only): 
Note - The examples below only apply to the Solaris operating environment. 
(SPARC) Here is a somewhat more complex C example:   
The output is similar to the following:
 ieee_handler -- Abort on Exceptions
You can use ieee_handler to force a program to abort in case of certain floating-point exceptions:
 
 Miscellaneous
 sigfpe -- Trapping Integer Exceptions
The previous section showed examples of using ieee_handler. In general, when there is a choice between using ieee_handler or sigfpe, the former is recommended.
(SPARC) There are instances, such as trapping integer arithmetic exceptions, when sigfpe is the handler to be used. The following example traps on integer division by zero: 
Note - SIGFPE is available only in the Solaris operating environment.  
 Calling FORTRAN from C
Here is a simple example of a C driver calling FORTRAN subroutines. Refer to the appropriate C and FORTRAN manuals for more information on working with C and FORTRAN. The following is the C driver (save it in a file named driver.c): 
Save the FORTRAN subroutines in a file named drivee.f: 
Then, perform the compilation and linking:
cc -c driver.c f77 -c drivee.f demo_one: demo_two: demo_three: f77 -o driver driver.o drivee.o  | 
 A Few Useful Debugging Commands
Table A-1 shows examples of debugging commands for the SPARC architecture
Table A-2 shows examples of debugging commands for the Intel  architecture. 
Table A-3 shows examples of debugging commands for the PowerPC architecture. 
myfunction:b  | 
myfunction=X 23a8 23a8:b  | 
The main in FORTRAN programs is known as MAIN_ to adb.
To set a breakpoint at a FORTRAN main in adb:  
 
 
 
 
 
 
   MAIN_:b 
When examining the contents of floating-point registers, the hex value shown
by the dbx command x &$f0/X is the base-16
representation, not the number's decimal representation. For SPARC, the
adb commands $x and $X display both the
hexadecimal representation, and the decimal value.  For Intel and PowerPC,
the adb command $x displays only the decimal value.
For SPARC, the double precision values show the decimal value next to the
odd-numbered register. 
(Intel) The corresponding output on Intel  looks like: 
 
Note - (Intel) cw is the control word; sw is the status word.