Floating point correlation (non-preliminary content)

Source: Internet
Author: User

Floating-point numbers include float double andlong double
Here are some knowledge about floating-point assorted .

Read-In & output
void example(){    float a;    double b;    long double c;    scanf("%f%lf%Lf", &a, &b, &c);    printf("a=%f,b=%lf,c=%Lf\n", a, b, c);}
Comparison

The comparison of floating-point numbers is very special. Two floating-point comparisons, greater than/less than can be used directly a>b a<b , but equal to the need for special attention; In general, we use the following method to compare whether two floating-point numbers are equal:

const double eps = 1e-6; //控制精度误差double a, b;if (fabs(a-b) < eps){    //a = b}else{    //a != b}

This comparison is to avoid floating-point errors , floating-point error generation generally due to two reasons

    1. Because the inside of the computer is stored in binary, the decimal number of a finite bit of decimals is an infinite decimal place inside the computer.
      For example decimal 0.9 although only one decimal place, turn into 2 binary is infinite loop decimal 0.1110011001100110011 ...
    2. Computers save floating-point numbers with limited precision, such as float, which retains a valid decimal number of up to 7 bits (binary 23-bit) and a double that retains a valid number in decimal 15~16 (binary 52-bit). The valid number is ignored.
      For example, the representation of the above 0.9 is limited by precision, and the precision is ignored, so
      Float when it is 0.89999998
      Double when it is 0.90000000000000002
      (This paragraph from Baidu know anonymous netizens answer)
Resources
    1. Floating-point comparison size-CSDN-
      Raspberry Akiko
    2. How the floating-point error is formed-Baidu knows-anonymous Netizen

Floating point correlation (non-preliminary content)

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.