C - Summation with Double - Precision?

In the world of Double Precision IEEE 754 binary floating-point format (the ones used on Intel and other processors) 0.1 + 0.2 == 0.30000000000000004 :-) And 0.30000000000000004! = 0.3 (and note that in the marvelous world of double s, 0.1, 0.2 and 0.3 don't exist as "exact" quantities. There are some double numbers that are very near them, but if you printed them with full precision, they wouldn't be 0.1, 0.2 and 0.3) To laugh a little, try this: pages.cs.wisc.edu/~rkennedy/exact-float Insert a decimal number and look at the second and third row, it shows how the number is really represented in memory.It's for Delphi, but Double and Single are the same for Delphi and for probably all the C compilers for Intel processors (they are called double and float in C) And if you want to try for yourself, look at this http://ideone.com/WEL7h include int main() { double d1 = (0.1 + 0.2); double d2 = 0.3; printf("%.20e\n%.20e", d1, d2); return 0; } output: 3.00000000000000044409e-01 2.99999999999999988898e-01 (be aware that the output is compiler dependant.

Depending on the options 0.1 + 0.2 could be compiled and rounded to 0.3).

In the world of Double Precision IEEE 754 binary floating-point format (the ones used on Intel and other processors) 0.1 + 0.2 == 0.30000000000000004 :-) And 0.30000000000000004! = 0.3 (and note that in the marvelous world of doubles, 0.1, 0.2 and 0.3 don't exist as "exact" quantities. There are some double numbers that are very near them, but if you printed them with full precision, they wouldn't be 0.1, 0.2 and 0.3) To laugh a little, try this: pages.cs.wisc.edu/~rkennedy/exact-float Insert a decimal number and look at the second and third row, it shows how the number is really represented in memory.It's for Delphi, but Double and Single are the same for Delphi and for probably all the C compilers for Intel processors (they are called double and float in C) And if you want to try for yourself, look at this http://ideone.com/WEL7h #include int main() { double d1 = (0.1 + 0.2); double d2 = 0.3; printf("%.20e\n%.20e", d1, d2); return 0; } output: 3.00000000000000044409e-01 2.99999999999999988898e-01 (be aware that the output is compiler dependant.

Depending on the options, 0.1 + 0.2 could be compiled and rounded to 0.3).

Unlike integer values floating point values are not stored exactly the way you assign values to them. Lets consider the following code: int I = 1; // this is and always will be 1 float j = 0.03 // this gets stored at least on my machine as something like 0.029999999 Why is this so? Well how many floating point number exist in the interval between 0.1 and 0.2?

An infinite number! So there are values which will get stored as you intended but a hell of a lot of values which will be stored with a small error. This is the reason why comparing floating point values for equality is not a good idea.

Try something like this instead: float a = 0.3f; float be = 0.301f; float threshold = 1e-6; if( abs(a-b).

An infinite number! " Entirely irrelevant. There are infinitely many natural numbers, yet the representation of any natural number doesn't take infinite memory.

We do not want to represent "all" real numbers. – curiousguy Oct 13 at 23:56 While it is true that each natural number can be represented with a finite amount of memory, it is also true that there for a given fixed amount of memory exists a natural number whose representation does not fit in said memory. Your point about not wanting to represent all real numbers is precisely the point: We cannot represent every real number, and thus every scheme will have problems of the type the original question asks about, namely there are real numbers that cannot be represented in a given scheme.

– gspr Oct 14 at 10:57.

There are infinitely many real numbers between any two distinct real numbers. If we were to be able to represent every one of those, we would need infinite memory. Since we only have finite memory, floating point numbers need to be stored with only finite precision.

Up to that finite precision, it might be not be true that 0.1 + 0.2.

But his program would work as expected if only double had de decimal representation, just as we usually write numbers on paper. "Infinite" (or unlimited) memory is not needed: we do not need infinite paper space to write a decimal number. You can program a data-type with decimal representation if you want.

You will have to use named functions like dec_add, dec_mult... in C, in C++ you could use operator overloading to keep the usual +, -, *... infix syntax. – curiousguy Oct 13 at 13:40 1 s particular example would work, but decimal representation would suffer for other numbers. How would you store 1/3 exactly with finitely many terms in decimal representation?

You would indeed need to store an infinite amount of data. – gspr Oct 13 at 13:49 Again, you do not need unlimited paper space to write "1/3". If you need to store 1/3 exactly, again, do it just as you would do on paper: store "1/3", not "0.333333333....".

It depends on what you need to represent exactly: describe your needs, then choose (or implement) the appropriate data-type. – curiousguy Oct 13 at 14:43 By storing "1 over 3", you're not storing the decimal representation of 1/3. You are, in fact, storing a fractional representation.

I agree that if you only need very limited types of real numbers, you can get away with some special scheme, but in general you would need unlimited memory. To summarize with examples: IEEE 754 can't store 0.1 exactly. Decimal can't store 1/3 exactly.

Ratios of integers can't store pi exactly. A ratio scheme can store any rational number exactly as long as the integers describing the ratios aren't limited. But they are limited by memory.

There's no free lunch! – gspr Oct 13 at 22:01 "By storing "1 over 3", you're not storing the decimal representation of 1/3. " Exactly."I agree that if you only need very limited types of real numbers, you can get away with some special scheme, but in general you would need unlimited memory.

" No, you don't. "Ratios of integers can't store pi exactly." If you need to "store pi exactly", you can store it as "pi" (or as something less ad hoc).

Again, it all depends on what you need to do. "There's no free lunch!" True: with every scheme designed to "represent exactly" more real numbers, there is additional code complexity.

– curiousguy Oct 13 at 23:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions