Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 990
Hans Malherbe Avatar asked May 27 '15 06:05

Hans Malherbe


2 Answers

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
like image 109
xanatos Avatar answered Nov 14 '22 21:11

xanatos


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.

like image 42
Guffa Avatar answered Nov 14 '22 22:11

Guffa