如果您想要的话,当然可以自己计算这些。维基百科页面上给出了在C语言中计算机器epsilon的几个 例子;类似地,您可以通过除以/乘以二直到下溢或上溢来找到最小值/最大值。(然后应该在最后一个有效值和下一个二进制因数之间搜索,以找到“真正”的最小/最大值,但这为您提供了一个良好的起点)。
但是,如果您拥有计算能力为2.0或更高的设备,则其大部分数学运算都是IEEE 754标准,并带有一些小偏差(例如,不支持所有舍入模式),但这些偏差不足以影响这些基本数值常量;因此,您将获得单精度的标准emach值为5.96e-08,双精度的标准emach值为1.11e-16;FLT_MIN/MAX为1.175494351e-38/3.402823466e+38,DBL_MIN/MAX为2.2250738585072014e-308/1.7976931348623158e+308。
在计算能力为1.3的机器上,单精度不支持非规格化数,因此FLT_MIN会比CPU上的大很多。
在计算能力为2.0的机器上进行快速测试,并进行最小/最大值的简单计算:
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <cuda.h>
#include <sys/time.h>
#include <math.h>
#include <assert.h>
#include <float.h>
#define CHK_CUDA(e) {if (e != cudaSuccess) {fprintf(stderr,"Error: %s\n", cudaGetErrorString(e)); exit(-1);}}
__device__ double machine_eps_dbl() {
typedef union {
long long i64;
double d64;
} dbl_64;
dbl_64 s;
s.d64 = 1.;
s.i64++;
return (s.d64 - 1.);
}
__device__ float machine_eps_flt() {
typedef union {
int i32;
float f32;
} flt_32;
flt_32 s;
s.f32 = 1.;
s.i32++;
return (s.f32 - 1.);
}
#define EPS 0
#define MIN 1
#define MAX 2
__global__ void calc_consts(float *fvals, double *dvals) {
int i = threadIdx.x + blockIdx.x*blockDim.x;
if (i==0) {
fvals[EPS] = machine_eps_flt();
dvals[EPS]= machine_eps_dbl();
float xf, oldxf;
double xd, oldxd;
xf = 2.; oldxf = 1.;
xd = 2.; oldxd = 1.;
while (!isinf(xf)) {
oldxf *= 2.;
xf *= 2.;
}
while (!isinf(xd)) {
oldxd *= 2.;
xd *= 2.;
}
dvals[MAX] = oldxd;
fvals[MAX] = oldxf;
xf = 1.; oldxf = 2.;
xd = 1.; oldxd = 2.;
while (xf != 0.) {
oldxf /= 2.;
xf /= 2.;
}
while (xd != 0.) {
oldxd /= 2.;
xd /= 2.;
}
dvals[MIN] = oldxd;
fvals[MIN] = oldxf;
}
return;
}
int main(int argc, char **argv) {
float fvals[3];
double dvals[3];
float *fvals_d;
double *dvals_d;
CHK_CUDA( cudaMalloc(&fvals_d, 3*sizeof(float)) );
CHK_CUDA( cudaMalloc(&dvals_d, 3*sizeof(double)) );
calc_consts<<<1,32>>>(fvals_d, dvals_d);
CHK_CUDA( cudaMemcpy(fvals, fvals_d, 3*sizeof(float), cudaMemcpyDeviceToHost) );
CHK_CUDA( cudaMemcpy(dvals, dvals_d, 3*sizeof(double), cudaMemcpyDeviceToHost) );
CHK_CUDA( cudaFree(fvals_d) );
CHK_CUDA( cudaFree(dvals_d) );
printf("Single machine epsilon:\n");
printf("CUDA = %g, CPU = %g\n", fvals[EPS], FLT_EPSILON);
printf("Single min value (CUDA - approx):\n");
printf("CUDA = %g, CPU = %g\n", fvals[MIN], FLT_MIN);
printf("Single max value (CUDA - approx):\n");
printf("CUDA = %g, CPU = %g\n", fvals[MAX], FLT_MAX);
printf("\nDouble machine epsilon:\n");
printf("CUDA = %lg, CPU = %lg\n", dvals[EPS], DBL_EPSILON);
printf("Double min value (CUDA - approx):\n");
printf("CUDA = %lg, CPU = %lg\n", dvals[MIN], DBL_MIN);
printf("Double max value (CUDA - approx):\n");
printf("CUDA = %lg, CPU = %lg\n", dvals[MAX], DBL_MAX);
return 0;
}
编译/运行显示答案与CPU版本一致(除了最小值;FLT_MIN在CPU上是否给出了最小的正常值而不是非规范化的值?)
$ nvcc -o foo foo.cu -arch=sm_20
$ ./foo
Single machine epsilon:
CUDA = 1.19209e-07, CPU = 1.19209e-07
Single min value (CUDA - approx):
CUDA = 1.4013e-45, CPU = 1.17549e-38
Single max value (CUDA - approx):
CUDA = 1.70141e+38, CPU = 3.40282e+38
Double machine epsilon:
CUDA = 2.22045e-16, CPU = 2.22045e-16
Double min value (CUDA - approx):
CUDA = 4.94066e-324, CPU = 2.22507e-308
Double max value (CUDA - approx):
CUDA = 8.98847e+307, CPU = 1.79769e+308