Zero-Value Comparison

Source: Internet
Author: User

Comparison between float X and zero:

If (x <0.000001 & x>-0.000001)

Note: floating point numbers cannot be directly compared to the size. Because they are not precisely stored, only one precision can be set (0.000001 indicates that the accuracy of the error is allowed ), then, the values within the allowable error are considered to be equal. It is incorrect to use = when comparing the floating point number.

Comparison between int X and zero:

If (X = 0) if (X! = 0)

Comparison between the pointer char * p and zero value:

If (NULL = p) if (NULL! = P)

Note: incorrect syntax

  1. If (p = 0) // It is easy to misunderstand that p is an integer variable.
  2. If (p! = 0)
  3. If (p) // It is easy to misunderstand that p is a bool variable.
  4. If (! P)

Comparison of bool flat and zero value: 

If (flag) if (! Flag)

Note: Based on the Boolean semantics, the zero value is "FALSE" (as FALSE), and any non-zero value is "TRUE" (as TRUE ). There is no uniform standard for the TRUE value. For example, Visual C ++ defines TRUE as 1, while Visual Basic defines TRUE as-1. Therefore, we cannot directly compare a Boolean variable with TRUE, FALSE, or 1 or 0.

 

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.