3 ms·
Why not just use C# instead of staying in the shitty alternative? You know... https://twitter.com/knocte/status/1207544365607985152 https://twitter.com/knocte/s
by knocte 7y ago
Why not just use C# instead of staying in the shitty alternative? You know... https://twitter.com/knocte/status/1207544365607985152 https://twitter.com/knocte/status/1207544365607985152
- mythz 7y agoBoth these expressions return the same result in both C# and JavaScript: 0.1 + 0.1 == 0.2; //= true 0.1 + 0.2 == 0.3; //= false It's not the language, it's https://floating-point-gui.de https://floating-point-gui.de
- knocte 7y agoI was going to reply that I thought the point of the video/tweet is that JS does choose floating point by default while others don't. But I stand corrected, C# default sucks too: it uses the double type! And here I thought that by using F# I would even be protected from this shit but unfortunately it has the same problem... Performance should be opt-in! (favouring correctness over it) :facepalm: Thanks
- wruza 7y agoFirst you should state the correctness level you need. 0.2? 1/7? sqrt(2)+pi? int n=0, n<inf, sum(1/n^3)?
- sasmithjr 7y agoLook at the decimal type; it's less performant, but you'll avoid floating point issues. https://docs.microsoft.com/en-us/dotnet/api/system.decimal?view=netcore-3.1 https://docs.microsoft.com/en-us/dotnet/api/system.decimal?v...
- ygra 7y agodecimal is still floating-point, just decimal floating-point instead of binary. Things like 1/3 can still not be represented exactly. It's just that for values given in base 10 there's no transformation of the representation and 0.3 remains exactly 0.3.
- knocte 7y agoYes I know the decimal type and it's what I use in all my code, I just thought the default would be something with more precision than double/float even if not decimal.
- gameswithgo 7y agoI'm not sure you appreciate how much performance would be lost...
- golergka 7y agoThis video has nothing to do with Javascript. It's the correct implementation of floating point arithmetic, and would behave the same in any language. If you want 0.1 + 0.2 == 0.3, you should use decimal, not floating point - regardless of the language.