Write the if statement for bool, Int, float, and comparison between the pointer and zero value.

Source: Internet
Author: User

 

 

In this example, the comparison between float and zero is quite interesting.

Bool:

Bool flag;
If (flag = true)
Return;

 

INT:

Int var;
If (Var = 0)
{
Return;
}

Pointer variable:

Char * P;
If (P = nullptr)
{
Return;
}

Float:

Float cannot be compared directly with 0 due to precision issues, so there is a allowed range. In this range, we think it is equal, generally 10-6 power, small enough

There are several defined Precision values in C/C ++, which are defined in limit. H. flt_epsilon (floating point Precision) and dbl_epsilon (Long Double Precision) can be used directly)

For example, 1.0 + flt_epsilon! = 1.0.

Comparison between float and zero:

Float Var = 0.0;
If (-0.000001 <= var) & (VAR <= 0.000001f ))
{
Printf_s ("% F \ n", VAR); // The output VaR is 0.00000.
}

 

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.