'Why are numbers with many significant digits handled differently in C# and JavaScript?

If JavaScript's Number and C#'s double are specified the same (IEEE 754), why are numbers with many significant digits handled differently?

var x = (long)1234123412341234123.0; // 1234123412341234176   - C#
var x =       1234123412341234123.0; // 1234123412341234200   - JavaScript

I am not concerned with the fact that IEEE 754 cannot represent the number 1234123412341234123. I am concerned with the fact that the two implementations do not act the same for numbers that cannot be represented with full precision.

This may be because IEEE 754 is under specified, one or both implementations are faulty or that they implement different variants of IEEE 754.

This problem is not related to problems with floating point output formatting in C#. I'm outputting 64-bit integers. Consider the following:

long x = 1234123412341234123;
Console.WriteLine(x); // Prints 1234123412341234123
double y = 1234123412341234123;
x = Convert.ToInt64(y);
Console.WriteLine(x); // Prints 1234123412341234176

The same variable prints different strings because the values are different.



Solution 1:[1]

There are multiple problems here...

You are using long instead of double. You would need to write:

double x = 1234123412341234123.0;

or

var x = 1234123412341234123.0;

The other problem is that .NET rounds doubles to 15 digits before converting it to string (so before for example printing it with Console.ToString()).

For example:

string str = x.ToString("f"); // 1234123412341230000.00

See for example https://stackoverflow.com/a/1658420/613130

Internally the number is still with 17 digits, only it is shown with 15.

You can see it if you do a:

string str2 = x.ToString("r"); // 1.2341234123412342E+18

Solution 2:[2]

The numbers are not handled differently, they are only displayed differently.

.NET displays the number with 15 significant digits, while JavaScript displays it with 17 significant digits. The representation of a double can hold 15-17 significant digits, depending on what number it contains. .NET only shows the number of digits that the number is guaranteed to always support, while JavaScript shows all digits but the precision limitation may show up.

.NET starts using scientific notation when the exponent is 15, while JavaScript starts using it when the exponent is 21. That means that JavaScript will display numbers with 18 to 20 digits padded with zeroes at the end.

Converting the double to a long in your example will circumvent how .NET displays doubles. The number will be converted without the rounding that hides the precision limitation, so in this case you see the actual value that is in the double. The reason that there isn't just zeroes beyond the 17th digit is that the value is stored in binary form, not decimal form.

Solution 3:[3]

Disclaimer: This is based off the standards wikipedia page

According to the wikipedia page for the standard, it defines that a decimal64 should have 16 digits of precision.

Past that it is the implementors decision as to what should be done with the additional digits.

So you may say that the standard is underspecified but then these numbers aren't designed to fit within the standards specifications anyway. Both languages have ways of handling bigger numbers so these options may be better suited for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2
Solution 3