C language type conversion those things

Source: Internet
Author: User

One: Cause

(1) Recently contacted type conversion, such as signed number to unsigned number----int converted to unsigned int type, MO to take for granted conversion;

(2) to make up for your first learning computer composition principle, the original code, anti-code, the complement is more easily confused concept;

(3) Double type of how to determine whether a parameter is zero, direct = = 0.0, it seems to be able to----indeed sometimes, but sometimes not, which is why?

(4) Knowledge Supplement: in int signed number: 0x80000000 (except the sign bit is 1, all the rest is 0)----the smallest number ( -2147483648 i.e. -2^32);

0x80000001----Times Minimum number ( -2147483647 i.e. -2^32-1);

0xFFFFFFFE ( -2); 0xFFFFFFFF (together with the sign bit is all 1)-----The largest negative integer (-1);

0x7ffffff (except that the sign bit is 0, all the rest is 1)----The maximum number (2147483647 is 2^32-1)

(5) The question is : How much is 0x80000000-1? (Maximum number of 0x7ffffff); 0x7ffffff + 1 How much is it? (Minimum number of 0x80000000 );

How is the 0x80000000-1 calculated? (0x80000000 + 0xFFFFFFFF, first to change the subtraction to addition, that is, positive 1 change to negative 1, overflow bit out )

Two: See the truth

(1) Code

  int min_int = 0x80000000;      int int_fu1 = 0xffffffff;      int max_int = 0X7FF fffff;      cout << "min_int=" << min_int << "\tmin_int-1=" << min_int-1 <<       "\ t (unsigned) Min_int =" << (unsigned int) min_int << endl;      cout <& Lt  "Int_fu1=" << int_fu1 << "\tint_fu1-1=" << int_fu1-1 <<      "\ t (unsigned) INT_FU1 = "<< (unsigned int) int_fu1 << endl;      cout <<" 0x7fffffff= "<< 0x7FFFFFFF &L t;< "\t0x7fffffff-1=" << 0x7FFFFFFF +1 << endl;      Double Test_zero = 1.333333;  &NB Sp    cout << (test_zero-1.3333334) << endl;      if (0 = = (test_zero-1.3333334))          cout << "(test_zero-1.3333334) =" << (test_zero-1.3333334) <<        "\t0 = = (Test_zero-1.3333334) "<< endl;      Else if ((test_zero-1.3333334) >-1e-6 && (test_ zero-1.3333334) < 1e-6)         cout << "(test_zero-1.3333334) =" << (test_zero-1.33333 <<        "\ t (test_zero-1.3333334) >-1e-6 && (test_zero-1.3333334) < 1e-6" & lt;< Endl;

(2) Result diagram


(3) Analysis of results

floating-point numbers (double) cannot be used = = and! = because floating-point numbers are stored in memory by precision (with the maximum precision bit number, you can also set the precision bit number), so you see the 3.12 may have been 3.123456 to take the decimal point after 2 bit accuracy, so when with Another 3.12 comparisons may occur in a number of cases, if the other 3.12 is actually 3.121111 take the decimal point after the 2 bit precision, then two number you look equal, actually is unequal.
So you do it. When comparing floating-point numbers, they are generally subtracted and then compared to a precision. But sometimes, comparisons can really pass, because that's a fluke.

You can see the minimum and maximum numbers followed by a step away.

The knowledge supplement of the inverse code complement of the original code

(4) Related learning C + + floating pointer and field pointer two level pointer I'm not a qualified programmer once again feel his ignorance

C + + deep-copy and shallow copy interview frequently asked about the distribution of C. Variables in memory (VC6.0)

C language type conversion those things

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.