[Turn from the original Shell matrix67] Android Calculator low-level error? It's all a binary mess!

Source: Internet
Author: User

Android Calculator has been discovered super big bug! Enter 14.52-14.49 in the Android Calculator program and the calculator says it equals 0.0299999999! In fact, this is already a computer of the same. The computer uses the binary to represent the number, will inevitably produce the error.

I heard about Android super big bug, I immediately tried on my HTC Hero, as everyone said, 14.52-14.49 = 0.0299999999. A little experimentation will reveal similar problems with simpler formulas, such as entering 1.2-1.1 in the Android calculator and the result being equal to 0.0999999999. What is this for?

It's all a binary mess.

Originally, in the computer, the numbers are not stored in decimal, all the numbers are stored in binary way. But a finite decimal in a binary, which is likely to be an infinite decimal in another binary. For example, converting decimal decimal 1.2 to binary decimal will get an infinite loop of decimal 1.001100110011 ... ; converting 1.1 to binary decimals is 1.0001100110011 ... and also an infinite loop decimal. The computer obviously cannot store infinitely many digits, so it has to intercept the finite number of bits approximately. If you keep a 52-digit number, then in the computer's view, 1.2-1.1 is actually the case:

The problem arises: when the calculation results are displayed, the computer needs to convert it back to decimal. But the above results are converted to decimal not exactly 0.1, but a 52-bit decimal 0.09999999999999986677323704 49812151491641998291015625. Since 2-52 is about 10-16, that is, the precision of the 52-bit binary decimal is approximately equal to the 16-bit decimal decimal, so the computer usually retains only 16 digits of the decimal number. As a result, the above decimal is 0.09999999999999987.

It's not just the Android problem.

You will find that such problems are everywhere in the computer. For example, in the address bar of your browser, type

Javascript:alert (1.2-1.1)

You'll see this weird answer: 0.09999999999999987. Even in professional math software, the problem of fractional precision is unavoidable. Enter 1.2-1.1 in Mathematica, and the answer seems to be true:

However, enter inputform[%] to view this 0.1 of the native, you will find that it is not accurate:

In other words, Mathematica finds that the decimal is too close to 0.1, and guesses that the number is really 0.1, and intelligently helps us simplify it. Many calculator software also has the function of the automation simple, but the calculator on the Android does not seem to be good enough, the occasional error is just.

in fact, the problem is not outrageous. Typing 1.2-1.1-0.1 on my Android phone will get a more bizarre result: -1.38777E-16, which is -1.38777x10-16. This is also due to errors in binary representations. However, there seems to be no problem with newer Android systems, and the algorithms for processing decimals in Android are constantly improving.
 

[Turn from shell original matrix67]android Calculator low-level error? It's all a binary mess!

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.