Unlimited size
How does the C language express infinite size, NAN? In redis:
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;
Simple Test of zookeeper:
# 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 );
Return 0;
}
The following figure shows the results of explain sequence:
[Heidong @ HEIDONGVM tmp] $./z
R_Zero: 0.000000
R_PosInf: inf
R_NegInf:-inf
R_Nan:-nan
It's amazing.
Comparison between floating number and 0
When I was in college, the teacher taught us that the floating number should not be compared with 0, because the floating point number represents an approximate value in the computer. Therefore, if the floating number is to be compared with 0, the method of using the variable number <0.000001 · is obviously 0, but a magic horse is required to come out. Now we can write this article:
# 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;
}
The following figure shows the results of explain sequence:
[Heidong @ HEIDONGVM tmp] $./z
R_Zero: 0.000000
R_PosInf: inf
R_NegInf:-inf
R_Nan:-nan
1.000000-1.000000 = 0
Then you can use R_NegInf to compare it with 0.
Post a: T http://luoguochun.cn/2014/06/28/infinite/