By “full precision” I mean that there won’t be a case where the right result differs from the one I’ve calculated with my C# program.
I’ve checked this by running the same tests under a C# and C++ (without using cout.precision()). I’m sure the 2 variants of my program under C# and C++ act similarly on the same tests.
Sample code on C#.
double a;
//...code...
Console.WriteLine(a.ToString(CultureInfo.InvariantCulture));
The same but on C++.
double a;
//...code...
cout << a << endl;
Actually my C++ program without cout.precision() failed and C# with nothing but Console.WriteLine() passed.
I know that this happens because my C++ program truncates the number somehow (maybe magically) and precision is lost. But does passing all the tests mean that C# always prints a doulbe variable with full precision?
If you format your double with the “R” format specifier, you get a “round-trip” representation. See http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#RFormatString for details. Usage:
The default format specifier, if none is specified, is “G” (General), which “converts a number to the most compact of either fixed-point or scientific notation, depending on the type of the number and whether a precision specifier is present.” See http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString for details. If no precision specificer is supplied,
doublegets 15 digits of precision, so using “G” offers the possibility of a loss of precision.