just simple question about JS/C# floats. I am making multiplayer game and normally have to synchronize stuff between client and server. Now the question, are C# floats and Javascript floats same data type? Like, can I send one to another and it will understand each other. I will send floats with scientific notations because I think this way it will be the shortest and most precise. Unless you guys have any other ideas :)
Thanks in advance.
A C# double
and a JavaScript Number
are the same thing, both are double-precision (64-bit) IEEE-754 binary floating point numbers ("binary64"). (A C# float
is just single-precision [32-bit, "binary32"], so if you want the same thing JavaScript has, use double
, not float
.)
Side note: Although they're the same number type, their respective "to string" operations are slightly different. For instance, given the number 0.87090686143883822
(which is really 0.8709068614388382201241256552748382091522216796875
, the nearest value IEEE-754 binary64 can hold), the "to string" operation from C#, JavaScript, and Java (which also uses binary64 for its double
) are:
0.870906861438838 - C#'s ToString() 0.87090686143883822 - C#'s ToString("R") 0.8709068614388382 - JavaScript's toString() 0.8709068614388382 - Java's String.valueOf(double)
I don't know the rules for C#, but JavaScript and Java both default to including only as many digits as are required to distinguish the number from its nearest representable neighbor. C#'s ToString()
doesn't do that (0.870906861438838
converts to 0.870906861438838
, precisely, losing the remaining 0.0000000000000002201241256552748382091522216796875
). C#'s ToString("R")
includes an unnecessary additional digit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With