views:

362

answers:

6

So, we know that fractions such as 0.1, cannot be accurately represented in binary base, which cause precise problems (such as mentioned here: http://stackoverflow.com/questions/1421520/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?

+7  A: 

For a comprehensive examination of the challenges involved in performing floating-point calculations, see this article:

What Every Computer Scientist Should Know About Floating-Point Arithmetic http://docs.sun.com/source/806-3568/ncg_goldberg.html

Robert Harvey
Are you sure thats specific enough?
Yuriy Faktorovich
Thank you for the link.
Heath Hunnicutt
+1  A: 

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
+3  A: 

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
Is there a countable infinity?
Adam Robinson
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
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
+3  A: 

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.
+2  A: 

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
A: 

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