Fixing double arithmetic errors
Updated:

Seems like C# has its own format specifier "R" Round trip


  The round-trip ("R") format specifier guarantees that a numeric value that is converted to a string will be parsed back into the same numeric value. This format is supported only for the Single, Double, and BigInteger types. 


public static double RoundTrip(double d)
{
    return Double.Parse(d.ToString("R"));
}


Original:

The way to represent floating point numbers exactly on a stream in readable format is to print out there representation in hex format (rather than decimal):

Not sure how it is done in C# or Java but I bet if you dig the exact same method is used down deep in these languages.

In C++ and C the methods are the same.
The C++ version. 

double x = 5.6789;

std::cout &lt;&lt; x &lt;&lt; "\n";   // Human readable dot format.
                          // Has loss of precision as number may not 
                          // be exactly representable in decimal.


// This the equivalent of using the "%a" string formatter.
// fprintf(stdout, "%a\n", x);
//
std::cout.flags(std::ios_base::fixed | std::ios_base::scientific);
std::cout &lt;&lt; x &lt;&lt; "\n";   // Hex Format human readable (well nearly human readable)
                          // Prints each part of the float as a hex value
                          // That can be represented exactly.
                          // on a stream of char


Updated after reading: Printing double without losing precision

// In C++11 there is an additional manipulator
// that achieves the same affect.
std::cout &lt;&lt; std::hexfloat &lt;&lt; x &lt;&lt; "\n";


The results:

5.6789                    // Easy to read (not exact)

0x1.6b7318fc50481p+2      // Difficult to read (but exact).

0x1.6b7318fc50481p+2      // Difficult to read (but exact).