3.3 单、双和扩展/四倍精度
大多数数值函数可以使用单精度、双精度和扩展 (x86) 或四倍精度。表 17提供了从不同语言调用各种函数的不同精度版本的示例。
表 17 调用单、双和扩展/四倍精度函数
|
|
|
|
C、C++ |
#include <math.h> float x,y,z; x = sinf(y); x = fmodf(y,z); #include <sunmath.h> float x; x = max_normalf(); x = r_addran_(); |
#include <math.h> double x,y,z; x = sin(y); x = fmod(y,z); #include <sunmath.h> double x; x = max_normal(); x = d_addran_();
|
#include <math.h> long double x,y,z; x = sinl(y); x = fmodl(y,z); #include <sunmath.h> long double x; x = max_normall(); |
Fortran |
REAL x,y,z x = sin(y) x = r_fmod(y,z) x = r_max_normal() x = r_addran() |
REAL*8 x,y,z x = sin(y) x = d_fmod(y,z) x = d_max_normal() x = d_addran() |
REAL*16 x,y,z x = sin(y) x = q_fmod(y,z) x = q_max_normal() |
|
在 C 中,单精度函数的名称是通过将 f 附加到双精度名称的后面形成的;扩展精度或四倍精度函数的名称是通过添加 l 形成的。 因为 Fortran 的调用约定不同,所以 libsunmath 为单、双和四倍精度分别提供 r_...、d_... 和 q_... 函数。可以按所有三种精度的通用名来调用 Fortran 内函数。
并非所有的函数都有 q_... 版本。有关 libm 和 libsunmath 函数的名称和定义,请参见 math.h 和 sunmath.h。
在 Fortran 程序中,切记将 r_... 函数声明为 real,将 d_... 函数声明为双精度,而将 q_... 函数声明为 REAL*16。否则,可能会导致类型不匹配。
注 -
Oracle Developer Studio Fortran 不支持扩展双精度。