5

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?

Community
  • 1
  • 1
Letterman
  • 4,076
  • 5
  • 29
  • 41
  • Bear in mind that **fractions are only imprecise when using fixed-length, floating-point format**. There is nothing stopping you from representing an arbitrarily precise fraction as the true ratio of two numbers, other than the fact that you have to do all math manually. – Adam Robinson Nov 02 '09 at 17:28
  • 1
    what are the type of operations you refer to, that are not available for decimals? Could you please elaborate? – Johannes Rudolph Nov 02 '09 at 17:46

6 Answers6

4

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.

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • Is there a countable infinity? – Adam Robinson Nov 02 '09 at 17:48
  • 2
    Yes, the number of rational numbers (fractions, or technically, those numbers that can be expressed as a ratio of two integers), is countable... The proof is by listing all the integers as the row and column headings in a table, and each cell contains the fraction which is the column value divided by the row value. You "Count" then by diagonal (lower left to upper right direction) starting with upper left cell (1/1), then second diagonal is 1/2, 2/1, then third diagonal is 1/3, 2/2, 3/1, etc.. this procedure effectively "counts" the rationals... – Charles Bretana Nov 02 '09 at 23:17
  • Cantor came up with this (in the 1800s I think) he called this first level of infinity aleph-null or aleph zero. The number of real numbers (rationals and irrationals like sqrt(2), is aleph=One, and is uncountable. He proved that there are an infinite number of levels of infinities, although that is beyond my capacity to explain in a comment! – Charles Bretana Nov 02 '09 at 23:19
  • @CharlesBretana: There's are some interesting countable sets of numbers between the rationals and the irrationals. One of my favorites is the set of all numbers of the form A/sqrt(B), with A being an integer and B being a positive number. If one has a set of points whose coordinates are all of that form, any points constructable from them via compass and straight-edge will have coordinates of that form. – supercat Aug 25 '12 at 20:09
3

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.

H H
  • 263,252
  • 30
  • 330
  • 514
3

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.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
2

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.

4.669201
  • 106
  • 2
1

you could shift the decimal point so that the numbers are whole, then do 64 bit integer arithmetic, then shift it back. Then you would only have to worry about overflow problems.

tbischel
  • 6,337
  • 11
  • 51
  • 73
1

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.

Several of the Math methods do support decimal: Abs, Ceiling, Floor, Max, Min, Round, Sign, and Truncate. What these functions have in common is that they return exact results. This is consistent with the purpose of decimal: To do exact arithmetic with base-10 numbers.

The trig and Exp/Log/Pow functions return approximate answers, so what would be the point of having overloads for an "exact" arithmetic type?

dan04
  • 87,747
  • 23
  • 163
  • 198