4 ms·
I just wish python would switch float and Decimal. Most of the time float is just an implementation detail when you really want a decimal. I think the literals
by dec0dedab0de 5y ago
I just wish python would switch float and Decimal.
Most of the time float is just an implementation detail when you really want a decimal. I think the literals should be a decimal, and you could explicitly cast float when you actually need to do floating point math, or as an optimization. But I know it's too late for that.
- Spivak 5y agoI mean they switched from strings being bytes of UTF-8 to unicode, never say never for Python 4! It would be a downright Pythonic move.
- tzot 5y agoIn CPython 2, a `str` was bytes period; no encoding was enforced, these bytes were not necessarily valid UTF-8, and a `unicode` had either UTF-16 or UTF-32 in-memory representation (decided at compilation time). In CPython 3, a `bytes` is bytes and a `str` has ASCII/UTF-16/UTF-32 in-memory representation (decided at runtime). So CPython 2 strings were not “bytes of UTF-8”, and that is why strings `'\xfc\xf7\xe9'` worked fine, they even printed fine in the proper environments.
- mistrial9 5y ago> In CPython 2, a `str` was bytes -- like old C language, a str is really bytes, no encoding is enforced.
- amanzi 5y agoThat would suit me too, but not sure of the wider implications.
- creata 5y agoWhy do you say that "most of the time... you really want a decimal"? I can't think of any situation where I'd prefer a decimal.Decimal over an ordinary float, except for the textbook example of dollars and cents. In most situations, I'm pretty sure decimal.Decimals would be unacceptably slower.
- tomrod 5y agoIn data science, this can be an issue.
- samwillis 5y agoEven with currency now it seems more common to just use ints of the currency’s lowest denomination/subunit (cents, pence etc)
- tadfisher 5y agoYou might as well use a Decimal at that point, which usually combines an integer mantissa with some inverse exponent of 10. Otherwise you're applying that exponent everywhere you use the value, whether you display it or use it in a mathematical expression.
- samwillis 5y agoI think the advantage of ints over Decimal is interoperability of systems. Anything with a REST api or JSON doesn’t have native support for Decimals, so to use them you have to represent them in component form as an object or as a string. Ints just make that easer at the slight cost at display time. Stripe is a good example of this. > Otherwise you're applying that exponent everywhere you use the value, whether you display it or use it in a mathematical expression. Slightly disagree, I think it’s only when displaying you have to convert to a “human readable” decimal form. They don’t need converting to Decimal for processing. It’s also worth noting there are actually two none decimal currency’s still in use: “Today, only two countries have non-decimal currencies: Mauritania, where 1 ouguiya = 5 khoums, and Madagascar, where 1 ariary = 5 iraimbilanja.” https://en.m.wikipedia.org/wiki/Non-decimal_currency https://en.m.wikipedia.org/wiki/Non-decimal_currency
- creata 5y ago> Anything with a REST api or JSON doesn’t have native support for Decimals This is a very slight nitpick (that might be wrong) but I think it's perfectly in line with the JSON specification to interpret JSON numbers as decimals.
- munificent 5y agoMy understanding is that even decimals still do rounding, so it doesn't give users accurate arbitrary-precision numbers. A default number format that is much slower than built-in floating point but still subject to unexpected deviation from real arithmetic numbers doesn't seem like a win for users.
- eternityforest 5y agoSo much of computing practice is based on the idea that floats aren't quite real numbers, they're like fuzzy analog voltages almost. Almost nobody thinks about float rounding, they just use something precise enough that it doesn't matter, or use integers. Why break that standard? Everyone expects computer numbers to be just a bit off. If we didn't we would do a lot of stuff differently in incompatible ways. Seems like it would hurt the Python ecosystem if people wrote stuff that assumed floats were perfect decimals, and encouraged code that depends on that. You'd get stuff like serializing to JSON, and reading in some other language with a JSON parser that doesn't know the numbers need to be precise. Plus, it's just faster.