Comparison between floating number and 0

Source: Internet
Author: User

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.