Prev: Accessor Functions (getter) for C String (Character Array) Members
Next: Why is the return type of count_if() "signed" rather than "unsigned"?
From: Andrew on 28 Jun 2010 06:01 On 27 June, 14:55, Martin Vejn�r <ava...(a)ratatanek.cz> wrote: > > See my sample program in this thread that uses the value > > -937566.2364699869. When GCC takes that string, converts it to a > > double, then converts the double back to a string, it gives > > -937566.2364699868. Adding an extra digit of precision gives > > -937566.23646998685. IFAICS this means it is doing the rounding > > incorrectly. > > No, as mentioned before, the nearest double is -937566.2364699868 485... > Rounding to 16 digits correctly returns -937566.2364699868. Rounding to > 17 yields (also correctly) -937566.23646998685. > -- > Martin Yes, you're right of course. I must have been having a brainstorm. I got a bit distracted by the fact that -937566.2364699869 is the original input string. Since the program is supposed to reproduce the original input string I got carried away with -937566.2364699869 being the right answer. I think this goes to show that the program has to store the value as a string to preserve accuracy. Doubtless there will be other values where the VS rounding does not reproduce the original string, especially since the VS rounding appears to be incorrect. It is only reproducing the desired string by a fluke given the imprecision involved. Regards, Andrew Marlow -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Zeljko Vrba on 29 Jun 2010 05:31 On 2010-06-28, SG <s.gesemann(a)gmail.com> wrote: > > In more mathematical terms, a conversion from string to double where > the string contains 16 significant decimal digits CAN NOT be an > injective mapping. You just witnessed a proof for this. > A bit of nitpicking: While true, this statement could be easily misunderstood to say something else, i.e., a question arises: "what about 10 significant decimal digits"? I would rather say that *no* conversion from a decimal string to a finite-precision, base-2 floating point number can be injective, *regardless* of the precision of the original decimal string (decimal 0.1 is the most obvious example). -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: A. McKenney on 29 Jun 2010 16:57 On Jun 29, 4:31 pm, Zeljko Vrba <mordor.nos...(a)fly.srk.fer.hr> wrote: > On 2010-06-28, SG <s.gesem...(a)gmail.com> wrote: > > > In more mathematical terms, a conversion from string to double where > > the string contains 16 significant decimal digits CAN NOT be an > > injective mapping. You just witnessed a proof for this. > > A bit of nitpicking: .... > I would rather say that *no* conversion from a decimal string to a > finite-precision, base-2 floating point number can be injective, *regardless* > of the precision of the original decimal string (decimal 0.1 is the most > obvious example). Time to pick the nit-picker's nits: I'm going to assume that your point is that there is no IEEE [base-2] floating-point number that is exactly equal to 0.1 But that has nothing to do with "injective". A map/function f is injective if a != b implies f(a) != f(b). That's the definition. If we're restricting ourselves to decimal strings with a maximum of, say, 6 significant digits, then convertion from this set of decimal representations to IEEE double-precision is injective. If we consider decimal strings with 17 significant digits, it isn't. The fact that f(a) != a doesn't make it not injective. And this definition of "injective" is what is relevant to this thread. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: SG on 29 Jun 2010 17:08
On 29 Jun., 22:31, Zeljko Vrba wrote: > On 2010-06-28, SG wrote: > > > In more mathematical terms, a conversion from string to double where > > the string contains 16 significant decimal digits CAN NOT be an > > injective mapping. > > A bit of nitpicking: > > While true, this statement could be easily misunderstood to say something else, > i.e., a question arises: "what about 10 significant decimal digits"? > > I would rather say that *no* conversion from a decimal string to a > finite-precision, base-2 floating point number can be injective, *regardless* > of the precision of the original decimal string I don't really know what it is you are referring to. Maybe you are referring to the fact that *different* strings ("0.1", "0.10") refer to the same number. So yes, if we're talking about "string values" of decimal encodings with at most 10 decimal digits, there is no such injective mapping from string values to double values. But there is an injective mapping from the *values* you can represent with those strings (of at most 10 decimal digits) to an IEEE-754 64-bit floating point value (ignoring exponent under/overflow). But maybe you confuse the meaning of "injective" with something else. > (decimal 0.1 is the most obvious example). No, actually, it's not obvious how this is supposed to be a counter example. Cheers! SG -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |