Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How deal with the fact that most decimal fractions cannot be accurately represented in binary?

So, we know that fractions such as 0.1, cannot be accurately represented in binary base, which cause precise problems (such as mentioned here: Formatting doubles for output in C#).

And we know we have the decimal type for a decimal representation of numbers... but the problem is, a lot of Math methods, do not supporting decimal type, so we have convert them to double, which ruins the number again.

so what should we do?

like image 938
Letterman Avatar asked Nov 02 '09 17:11

Letterman


People also ask

Can all fractions be represented in binary?

Only fractions with a denominator which is a power of two can be finitely represented in a binary form. For example, denominators of 0.1 (1 / 10) and 0.2 (1 / 5) are not powers of two, so these numbers can't be finitely represented in a binary format.

Why can't floating point numbers be always represented exactly in binary?

It's just that there's no hardware support for this, and just most languages and hardware out there implements the IEEE standards for representing an infinite amount of numbers in 32 or 64 bits.

Why can 1/10 be represented exactly in decimal but not in binary?

There are some numbers that are able to be exactly represented in some base, but it takes more than some number of digits/bits to do. In binary, the number 1/10 which is conveniently 0.1 in decimal isn't able to be represented as a number that can be represented in a fixed number of bits in binary.

How does binary handle decimals?

Fractional numbers are treated much the same as in decimal. The basic idea in decimal is that the places after the decimal point have values 1/10, 1/100, 1/1000, etc. In binary, we again use powers of 2 instead, so the places to the right of the binary point have values 1/2, 1/4, 1/8, 1/16, etc.


4 Answers

Oh, what should we do about the fact that most decimal fractions cannot be represented in binary? or for that matter, that binary fractions cannot be represented in Decimal ?

or, even, that an infinity (in fact, a non-countable infinity) of real numbers in all bases cannot be accurately represented in any computerized system??

nothing! To recall an old cliche, You can get close enough for government work... In fact, you can get close enough for any work... There is no limit to the degree of accuracy the computer can generate, it just cannot be infinite, (which is what would be required for a number representation scheme to be able to represent every possible real number)

You see, for every number representation scheme you can design, in any computer, it can only represent a finite number of distinct different real numbers with 100.00 % accuracy. And between each adjacent pair of those numbers (those that can be represented with 100% accuracy), there will always be an infinity of other numbers that it cannot represent with 100% accuracy.

like image 71
Charles Bretana Avatar answered Oct 12 '22 23:10

Charles Bretana


so what should we do?

We just keep on breathing. It really isn't a structural problem. We have a limited precision but usually more than enough. You just have to remember to format/round when presenting the numbers.

The problem in the following snippet is with the WriteLine(), not in the calculation(s):

double x = 6.9 - 10 * 0.69;
Console.WriteLine("x = {0}", x);

If you have a specific problem, th post it. There usually are ways to prevent loss of precision. If you really need >= 30 decimal digits, you need a special library.

like image 33
Henk Holterman Avatar answered Oct 12 '22 23:10

Henk Holterman


Keep in mind that the precision you need, and the rounding rules required, will depend on your problem domain.

If you are writing software to control a nuclear reactor, or to model the first billionth of a second of the universe after the big bang (my friend actually did that), you will need much higher precision than if you are calculating sales tax (something I do for a living).

In the finance world, for example, there will be specific requirements on precision either implicitly or explicitly. Some US taxing jurisdictions specify tax rates to 5 digits after the decimal place. Your rounding scheme needs to allow for that much precision. When much of Western Europe converted to the Euro, there was a very specific approach to rounding that was written into law. During that transition period, it was essential to round exactly as required.

Know the rules of your domain, and test that your rounding scheme satisfies those rules.

like image 31
Eric J. Avatar answered Oct 13 '22 01:10

Eric J.


I think everyone implying: Inverting a sparse matrix? "There's an app for that", etc, etc

Numerical computation is one well-flogged horse. If you have a problem, it was probably put to pasture before 1970 or even much earlier, carried forward library by library or snippet by snippet into the future.

like image 36
4.669201 Avatar answered Oct 13 '22 01:10

4.669201