この付録では、一般的なタスクを行う方法の例を示します。例は Fortran または ANSI C で記述されており、その多くは現在のバージョンの libm および libsunmath に依存しています。これらの例は、Oracle Solaris 10 Update 10 OS 以降のリリースの Oracle Developer Studio 12.5 でテストされています。C の例は、–lsunmath –lm オプションを使用してコンパイルされています。
次の例は、浮動小数点数の 16 進表現を検査できる 1 つの方法を示しています。格納されているデータの 16 進表現を参照するには、デバッガを使用することもできます。
次の C プログラムは、倍精度の近似値を ?? と単精度の無限大に出力しています。
使用例 6 倍精度の例#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;
}
–lsunmath を指定してコンパイルされた SPARC ベースのシステムでは、前述の プログラム の出力は次のようになります。
DP Approx pi = 400921fb 54442d18 = 3.14159265358979312e+00 Single Precision Infinity: 7f800000
次の Fortran プログラムは、最小の正規数をそれぞれの形式で出力します。
使用例 7 各形式での最小の正規数の出力 (続き)
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
SPARC ベースのシステムでは、対応する 出力は次のようになります。
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