Go to main content
Oracle® Developer Studio 12.6: Numerical Computation Guide

Exit Print View

Updated: July 2017
 
 

Examples

This appendix provides examples of how to accomplish some popular tasks. The examples are written either in Fortran or ANSI C, and many depend on the current versions of libm and libsunmath. These examples were tested with Oracle Developer Studio 12.6 on an Oracle Solaris 10 Update 10 OS or later release. C examples are compiled using the –lsunmath –lm options.

A.1 IEEE Arithmetic

The following examples show one way you can examine the hexadecimal representations of floating-point numbers. Note that you can also use the debuggers to look at the hexadecimal representations of stored data.

The following C program prints a double precision approximation to ?? and single precision infinity:

Example 6  Double Precision Example
#include <math.h>
#include <sunmath.h>
 
int main() {
 union {
   float         flt;
   unsigned     un;
 } r;
 union {
   double      dbl;
   unsigned    un[2];
 } d;
 
 /* double precision */
 d.dbl = M_PI;
 (void) printf("DP Approx pi = %08x %08x = %18.17e \n",
     d.un[0], d.un[1], d.dbl);
 
 /* single precision */
 r.flt = infinityf();
 (void) printf("Single Precision %8.7e : %08x \n", 
     r.flt, r.un);
 
 return 0;
}

On SPARC-based systems, compiled with –lsunmath, the output of the preceding program looks like the following:

DP Approx pi = 400921fb 54442d18 = 3.14159265358979312e+00 
Single Precision Infinity: 7f800000 

The following Fortran program prints the smallest normal numbers in each format:

Example 7  Print Smallest Normal Numbers in Each Format (Continued)
 program print_ieee_values
c
c the purpose of the implicit statements is to ensure
c that the floatingpoint pseudo-intrinsic functions
c are declared with the correct type
c
 implicit real*16 (q)
 implicit double precision (d)
 implicit real (r)
 real*16           z
 double precision  x
 real              r
c
 z = q_min_normal()
 write(*,7) z, z
 7 format('min normal, quad: ',1pe47.37e4,/,' in hex ',z32.32)
c
 x = d_min_normal()
 
 write(*,14) x, x
 14 format('min normal, double: ',1pe23.16,' in hex ',z16.16)
c
 r = r_min_normal()
 write(*,27) r, r
 27 format('min normal, single: ',1pe14.7,' in hex ',z8.8)
c
 end

On SPARC-based systems, the corresponding output reads as follows:

min normal, quad:   3.3621031431120935062626778173217526026E-4932
 in hex 00010000000000000000000000000000
min normal, double:  2.2250738585072014-308 in hex 0010000000000000
min normal, single:  1.1754944E-38 in hex 00800000