Comparison of Floating Point Numbers

Source: Internet
Author: User

We all know that the comparison of floating-point numbers should be a comparison range. If we think they are equal within this range, why cannot we use "=" to compare them? The following code is provided. You are welcome to discuss it.

#include <stdio.h>int main(){    float f = 1/3.0;    printf("f = %f\n", f);    if (1/3.0 == f)    {        printf("Hello World!\n");    }    return 0;}

During compilation of this Code, warning is given: Warning c4305: 'initializing': truncation from 'const double' to 'float '. that is to say, when initializing a variable, the double type is truncated to the float type, with a loss of precision.

Similarly, the 1/3 statement in the IF statement. 0 is also double type, so the F will be upgraded from float type to double type, here, the problem of different precision will come out, this code will not output "Hello world! \ N ".

Let's look at another piece of code:

#include <stdio.h>int main(){    float f = 1/3.0f;    printf("f = %f\n", f);    if (1/3.0f == f)    {        printf("Hello World!\n");    }    return 0;}

This code is compiled without warning and can output "Hello world! \ N ".

#include <stdio.h>int main(){    double f = 1/3.0;    printf("f = %f\n", f);    if (1/3.0 == f)    {        printf("Hello World!\n");    }    return 0;}

This code is compiled without warning and can also output "Hello world! \ N ".

However, please note that data has not been computed here. The following example

#include <stdio.h>int main(){    float f = 1.111f+2.222f;    printf("f = %f\n", f);    if (3.333f == f)    {        printf("Hello World!\n");    }    return 0;}

AlreadyNot AllowedOutput "Hello world! \ N "now!

Of course, if the compiler optimizes the above addition, it may output "Hello world! \ N ".

Because the data stored in the computer is binary (for specific storage methods, please query other articles of others), the accuracy of the representation is different from that of the decimal representation. Therefore, some data that can be easily expressed in decimal format can be expressed in binary format but cannot be completely expressed. It can only represent an approximate value, just like using a decimal number to represent an irrational number, we also need to take a reasonable approximate value (for example, π we usually use 3.14 ).

When performing operations on data, because some data cannot be expressed in binary format, there is an error. The more data cannot be expressed, the larger the error is, and "=" cannot be used to compare floating point numbers, this error exists.

Finally, compare floating point numbers and use range comparison. The accuracy of the range depends on the actual situation.

(I am not very grateful if you have any incorrect content in this 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.