6 ms·
Maybe this should be obvious, but I don't have a computer to hand to try and figure it out: In the example for f-string interpolation, why does value have to b
by vosper 10y ago
Maybe this should be obvious, but I don't have a computer to hand to try and figure it out: In the example for f-string interpolation, why does value have to be declared like this:
value = decimal.Decimal("12.34567")
Why wouldn't this work?
value = 12.34567
- orf 10y agoFloats != decimals, and it's good practice to use Decimal I think.
- renzon 10y agoIt's not good practice. You must use Decimal when precision is important. Example: when dealing with currency, so arithmetic operations don't create or miss cents.
- webmaven 10y agoBecause that gives you a Float, not a Decimal.
- gizmo385 10y agoDoesn't seem very Pythonic to me...
- teilo 10y agoEvery type-inference language I am aware of assumes that anything that looks like a float is a float. It would be un-pythonic to assume that everything that looks like a float is a decimal. Imagine having to cast every damn float to a float.
- dragonwriter 10y agoSince when does an exact decimal literal "look like a float" more than it looks like a decimal?
- paulddraper 10y agoTradition. It was easier for computers and the rest is history.
- teilo 10y agoYeah, I was going to say, "Since computers."
- dragonwriter 10y agoSure, lots but not all (Scheme doesn't, for instance) programming languages treat things that look like decimals as floats as a popular performance-over-correctness optimization, but I don't think that makes an exact decimal representation look like anything but a decimal.
- teilo 10y agoThe Python Decimal class was introduced 10 years ago in version 2.4. That's after Python was 12 years old (from v. 1.0). It is a standard library module, not a first level type. This is not the least unusual. Even Swift, as modern as it gets, operates the same way. You are arguing against a convention that is as deep rooted as CPUs capable of floating point calculations. To this day, there ARE no "decimal" CPU operations. There are integer and float operations.
- dragonwriter 10y agoActually, I am arguing against describing exact decimal representation as something that "looks like a float" (particularly a binary float.) I'm not arguing against Python's behavior. I do think that, at a minimum, there are major and very common software domains where the common optimization is more harmful than beneficial, and that it's good that some languages buck the trend. But while I don't think it should be an unquestioned tradition in language design, I don't think it's categorically wrobg, either. > To this day, there ARE no "decimal" CPU operations First, this isn't true, there are CPUs that have decimal (still floating point, so only exact within a subset of their full range) operations. Second, it's irrelevant; many languages have types (including types for common literals) that don't map to a specialized class of operations in the CPU, where the language compiles operations down to aggregates of operations on some lower level type or types.
- kazagistar 10y agoIn Haskell: `2.3 :: Fractional t => t` In other words, it can be inferred to be any type that implements the Fractional typeclass, which includes Decimal types. Not that it is relevant here, because you basically need to have types for something like this to work, if you have proper type inference, then you don't need to assume any type, and can just interpret a literal as whatever is needed.
- vosper 10y agoSure, 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?
- 10y ago
- makecheck 10y agoThe nice thing about Python is that aliasing is pretty easy to shorten tedious statements (while not being magical). If I had to write tons of those in code, I might say: D = decimal.Decimal x = D("1.2345") Also (at least this was once true) by doing so you save Python from a dictionary lookup after the dot "." each time.
- jonatron 10y agoOr from decimal import Decimal as D
- vosper 10y agoYou could, but it's very unobvious. Unless you had it as a clear convention across the code base I'd imagine it would just lead to a lot of confusion.
- JustSomeNobody 10y agoWait. This is extremely common to do in Python. If it really led to lots of confusion, I don't think it'd be that common.
- dolinsky 10y agoWhat's common isn't necessarily a good practice to follow - https://google.github.io/styleguide/pyguide.html#Imports https://google.github.io/styleguide/pyguide.html#Imports By importing a function and assigning a 1 char alias to it, the readability of the code is impacted.
- pfranz 10y agoI'm a bit confused by your comment. From your link: > Use from x import y as z ... if y is an inconveniently long name. "import ... as" is both common and per Google's style guide a good practice to follow in this scenario.
- pizza234 10y agoBecause the latter is a floating point number syntax. There could be an argument for switching to decimals as standard, but even ignoring the practical consequences, I think for historical reasons (I assume this is the standard of programming languages; in Ruby for example is the same) it's not something that will be ever agreed/changed.