3 ms·
In C# you don't have such a problem and 0.2 + 0.1 is exactly 0.3. Proof: https://dotnetfiddle.net/qTiq6U https://dotnetfiddle.net/qTiq6U
by lkjoi23j3oijf 5y ago
In C# you don't have such a problem and 0.2 + 0.1 is exactly 0.3. Proof:
https://dotnetfiddle.net/qTiq6U https://dotnetfiddle.net/qTiq6U
- viraptor 5y agoIn every language where you can use a decimal the result will match exactly. That's... not what's discussed here and it's not a default in c# either.
- lkjoi23j3oijf 5y agoUnlike other languages, in JS you don't have decimals... So you stuck writing a garbage code by multiplying all floating point numbers by a factor to avoid rounding errors.
- esrauch 5y agoI don't think that's fair: C# has decimals but everyone uses floating point numbers by default so C# developers still need to know that 0.1+0.2 != 0.3
- hajile 5y agoThe last couple generations of POWER chips from IBM have implementations of decimal32, decimal64, and decimal128. To my knowledge, no other big-name, general-purpose CPU has these. To "implement decimal numbers" in .net on x86 hardware simply means writing a custom decimal implementation in software. JS had implicit integers since the beginning. It has had arrays of integers (what's necessary for fast decimal implementations) since BEFORE the release of WebGL a decade ago. It has also added BigInt this year. Just like .net, there's decimal libraries available if you know to use them. https://github.com/MikeMcl/decimal.js/ https://github.com/MikeMcl/decimal.js/ The real takeaway is that modern processors should definitely add hardware support for decimal numbers.