Why can't a decimal 0.1 be a good representation of binary?

Source: Internet
Author: User
Tags decimal to binary

Binary notation ....

This is also the basic knowledge:

Don't worry about our 1.1-point explanation. Positive integer to binary, negative integer to binary, decimal to binary

Positive integer to binary: positive integers are converted to binary. Points must be sure to remember Kazakhstan: In addition to the second, and then the reverse order, high 0.

21/2-------------------------------1

10/2-------------------------------0

5/2-------------------------------1

2/2--------------------------------0

---------------------------------more than 1

Remember, to the sort 10101, verify that the next decimal is converted to: 1x2 's 4-square-+1x2 2-+1x2-square-=16+4+1=21. That's right.

The computer is generally 8-bit 16-bit 32-bit 64-bit, so not enough bit high to fill 0. 8-bit notation: 00010101

Negative integer to binary: first convert the corresponding positive integer into binary, reverse the binary, and then add one to the result.

Take-21 demo, see the formula,

The binary representation of 21 is: 10101

Reverse: 01010

Plus one: 01011

Decimal to Binary method: Multiply the number of decimal points by 2, have a result, take the integer portion of the result (not 1 is 0), and then multiply the fractional part by 2, then take the integer portion of the result ... And so on, until the number is divided into 0 or the number of digits is enough, OK.

Demo: 0.125x2=0.25 ......... ........ 0

0.25x2=0.5.............................0

0.5x2=1.0................................1

That is, 0.125 of the binary representation of the decimal part is divided into 0.001

Remember, multiply the decimal number to 0. Sort: Positive order.

Verification: 0.001 0x2 0-+0x2-1-square-+0x2-2-+0x2-3-square =1/8=0.125. Right.

Now to illustrate why the 0.1-turn binary cannot be represented:

0.1x2=0.2 ........ ........ 0

0.2x2=0.4 ........ ......... 0

0.4x2=0.8 ........ ........ 0

0.8x2=1.6.......................1

0.6x2=1.2.......................1

0.2x2=0.4.......................0

.....

is an infinite loop. So.... You know!

for (double i=0;i!=10;i+=0.1)
{
}

There could be a cycle of death. You need to pay attention to the floating point loop.

Why can't a decimal 0.1 be a good representation of binary?

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.