3 ms·
The choice of decimal vs float depends on domain. For financial calculations you want to be able to represent amounts exactly. Decimal can represent $1.01 exact
by jacobsenscott 4y ago
The choice of decimal vs float depends on domain. For financial calculations you want to be able to represent amounts exactly. Decimal can represent $1.01 exactly, but float cannot (it picks a value very close to that, and as you do calculations eventually these small inaccuracies amount to real money).
But if you are storing the weight of a product a float might be fine - you don't really care if the system thinks you have 50.000001 pounds of product when you have 50 pounds (because your scale isn't that accurate anyway).
Floats will generally give you better performance than decimal types as well.
- MuffinFlavored 4y agoWould you say it's "odd" that popular languages (C#, Java, JavaScript/node.js/TypeScript, Python) don't have built in decimal libraries?
- MichaelNolan 4y agoJava has BigDecimal. Python has a decimal and a fraction class. I’m assuming C# has something apart of their standard library as well. And JS/TS has third party libraries that can for arbitrary length precision math as well.
- MuffinFlavored 4y agoThank you. I had a feeling I was wrong. So just JavaScript has BigInt/BigDecimal https://stackoverflow.com/questions/16742578/bigdecimal-in-javascript https://stackoverflow.com/questions/16742578/bigdecimal-in-j..., interesting
- int_19h 4y agoC# has it as part of the language itself, with keyword for the type and a special literal syntax for it: decimal n = 123.456m;
- jacobsenscott 4y agoI don't think I would say it is odd because it isn't a type of number that is directly supported by the hardware. That said it is available in the standard library of most popular languages, and probably just a package install away for any other languages. Also, a plain integer is usually sufficient for financial calculations (just count pennies, not dollars) so in some ways I think decimal types are overused when an integer would do just fine.
- int_19h 4y agoThing is, the practical cases where you want to represent amounts exactly all involve decimals, not binary. If you default to decimals, and someone unknowingly uses them in a situation where binary floats would be better, they still get results they expected, just slower. But if you default to floats, and someone unknowingly uses them in a situation where an exact decimal amount is needed, it can actually produce incorrect (in the sense defined by the functional spec) results. Defaults should be safe, and performance optimizations that are more likely to affect correctness should be opt-in.