無限大小redis
C語言如何表示無限大小,NAN?redis裏面這麼搞的:測試
static double R_Zero, R_PosInf, R_NegInf, R_Nan; spa
/* Double constants initialization */ it
R_Zero = 0.0; io
R_PosInf = 1.0/R_Zero; class
R_NegInf = -1.0/R_Zero; static
R_Nan = R_Zero/R_Zero; 計算機
簡單測試:語言
#include <stdio.h> di
int main()
{
static double R_Zero, R_PosInf, R_NegInf, R_Nan;
/* Double constants initialization */
R_Zero = 0.0;
R_PosInf = 1.0/R_Zero;
R_NegInf = -1.0/R_Zero;
R_Nan = R_Zero/R_Zero;
printf("R_Zero : %lf\n"
"R_PosInf: %lf\n"
"R_NegInf: %lf\n"
"R_Nan : %lf\n",
R_Zero, R_PosInf, R_NegInf, R_Nan);
return 0;
}
結果以下:
[heidong@HEIDONGVM tmp]$ ./z
R_Zero : 0.000000
R_PosInf: inf
R_NegInf: -inf
R_Nan : -nan
果真神奇。
浮動數與0比較
大學時候,老師教導咱們,浮動數不要與0比較,由於浮點數在計算機裏面表示的是近似值。因此若是浮動數要與0作比較就用·< 0.000001·這種齷蹉的方式,明明是0,卻要搞個神馬0.000001出來。如今咱們能夠這麼來稿了:
#include <stdio.h>
int main()
{
static double R_Zero, R_PosInf, R_NegInf, R_Nan;
/* Double constants initialization */
R_Zero = 0.0;
R_PosInf = 1.0/R_Zero;
R_NegInf = -1.0/R_Zero;
R_Nan = R_Zero/R_Zero;
printf("R_Zero : %lf\n"
"R_PosInf: %lf\n"
"R_NegInf: %lf\n"
"R_Nan : %lf\n",
R_Zero, R_PosInf, R_NegInf, R_Nan);
double a = 1.0;
double b = 1.0;
if(a-b == R_NegInf) {
printf("%lf - %lf != 0\n", a, b);
}else{
printf("%lf - %lf == 0\n", a, b);
}
return 0;
}
結果以下:
[heidong@HEIDONGVM tmp]$ ./z
R_Zero : 0.000000
R_PosInf: inf
R_NegInf: -inf
R_Nan : -nan
1.000000 - 1.000000 == 0
之後就能夠用R_NegInf這種方式和0比較了。
POST A:T http://luoguochun.cn/2014/06/28/infinite/