4 ms·
"You can build arbitrary-precision rationals" is a far cry from saying, "this is a language designed for precision arithmetic." You can technically build arbitr
by giaour 3y ago
"You can build arbitrary-precision rationals" is a far cry from saying, "this is a language designed for precision arithmetic." You can technically build arbitrary-precision rationals without BigInts using arrays, but it's not exactly ergonomic.
Even if we stipulate that users will be cognizant of floating point limitations and convert everything to integers, JS still requires all literals to be suffixed with n to avoid silently losing precision when integral values exceed 2*52.
- WorldMaker 3y agoMany languages that are considered "precision" languages have some form of suffix to denote large/long/arbitrary-sized literals. (C# there's a big useful difference if a literal ends with `m` or `l`.) Most languages you have to be aware of your data types and use the appropriate data types. No one is saying that "JS is a language designed for precision arithmetic", everyone is saying "JS is a language with the tools today to do precision arithmetic (whether you like it or not)".
- giaour 3y ago> Many languages that are considered "precision" languages have some form of suffix to denote large/long/arbitrary-sized literals. (C# there's a big useful difference if a literal ends with `m` or `l`.) The failure mode is significantly different when comparing JS to other mainstream languages. Most languages have a "checked" mode (either opt-in or mandatory) so that expressions like `4611686018427387904 + 4611686018427387904` can raise an error, whereas JS will just give you the wrong answer. Python is the most ergonomic for beginners or non-developers because the runtime will convert an integral type to an arbitrary precision integer on overflow. Yes, there are alternative primitives that can be used, but you need to know they exist. We're talking about a tool that is explicitly designed for people who are not professional programmers, and JS's behavior here is surprising unless you understand the underlying data model. I would personally much rather give beginners a tool incorporating as few footguns as possible. > JS is a language with the tools today to do precision arithmetic (whether you like it or not) Arbitrary precision arithmetic has been possible in JS since the language was invented, but it has always been a pain. The bignum NPM package[0] predates the BigInt numeric primitive by 9 years. The addition of `BigInt` to ES2020 was important for performance (since implementing an arithmetic primitive in JS makes calculations dog slow), but it didn't add any fundamental affordances to the language. [0]: https://github.com/justmoon/node-bignum https://github.com/justmoon/node-bignum