3 ms·
Sure, I get the difference between float and Decimal, but why can't floats be formatted with precision in the example? Are they using Decimal because it's best
by vosper 10y ago
Sure, I get the difference between float and Decimal, but why can't floats be formatted with precision in the example? Are they using Decimal because it's best practice, even though it adds more code to the string formatting example ?
- webmaven 10y agoFloats can absolutely be formatted, but the result may be unexpected, because it is basically a (hardware based) C binary float: https://en.wikipedia.org/wiki/IEEE_floating_point https://en.wikipedia.org/wiki/IEEE_floating_point So, because users will typically be surprised that 2.2 + 3.1 results in 5.300000000000001, or that round(2.675, 2) gives 2.67 (instead of 2.68), best practice is to use Decimal, which will give users the results they expect: https://docs.python.org/3/library/decimal.html https://docs.python.org/3/library/decimal.html
- vosper 10y agoMakes sense. I guess what I was getting at is that introducing Decimal to the example about f-string interpolation makes the example more complicated than it absolutely needs to be, but I can see why they did it.
- webmaven 10y agoI think some of that extra complexity could be papered over with either making decimal() available like int() and float(), which would eliminate the import, and/or implementing a decimal literal format, such as 0d2.675, analogous to the format of Hexadecimal and Octal literals (0xCC00CC and 0o267, respectively).
- mixmastamyk 10y agoI like that second idea, perhaps a mention on the python-ideas mailing list?
- webmaven 10y agoPerhaps. I need to mull it over a bit before I take that plunge (but thank you for the encouragement!). One possible objection is that while Decimal is in the standard library, it isn't a built-in type, so either Decimals need to be elevated to built-ins, or some magic needs to happen when importing the decimal module to add the literal.
- renzon 10y agoThey used Decimal just to show the nested attribute access
- pfranz 10y agoHere's a practical example that comes to mind. I work in film and we deal with frame rates. Often tools use 24 or 30 frames per second. 30fps is a simplification of 60 frames interlaced, which is technically 29.97fps. The main use-case we have is storing, comparing, and occasionally flipping between fractions and decimals. You would think integers would work. We've tried to update our tools to use 29.97 when it is necessary (although, this spans Python, databases, and third-party applications). The first attempt used floats, but we'd have scenarios where we'd get 24.00001 or errors comparing two values. The second attempt just used strings for frame rate so comparisons and storage work and you have a lookup table when you need to convert. Decimal sounds like a better solution. A similar scenario in film (that hasn't caused as much trouble for me) is aspect ratio, which is the height/width of an image. Fractions and decimals are used interchangeably in conversation.
- webmaven 10y agoBesides using decimals instead of floats for your frame-rate use case (definitely a better fit), using a Python fraction may be a more useful representation for aspect ratios: https://docs.python.org/3/library/fractions.html https://docs.python.org/3/library/fractions.html Also possibly useful in that context (and bringing us back full circle to the OP's subject), Python 3.6 has added the as_integer_ratio() method to Decimal instances: https://docs.python.org/3.6/library/decimal.html#decimal.Decimal.as_integer_ratio https://docs.python.org/3.6/library/decimal.html#decimal.Dec...