5 ms·
I'm surprised modern languages haven't gone farther with base 10 numbers. C# has decimal (not sure how widely used that is) but what other language has built-in
by jasonthorsness 1y ago
I'm surprised modern languages haven't gone farther with base 10 numbers. C# has decimal (not sure how widely used that is) but what other language has built-in, non-library base 10 numbers?
- viraptor 1y agoPython has decimals for example as do many other languages. C# decimal is used everywhere currency is used (I really hope). But apart from currency... why would you use base 10? We've got native bigints in lots of languages so you don't even have to care how they're represented internally.
- arn3n 1y agoNot all decimal numbers have finite binary representations. 0.3, for example, is 0.010011 with a repeating block of 0011. For some business applications, you know you're being given a base 10 decimal of finite (but possibly very large) length.
- eru 1y agoYou can use an arbitrary length integer to store the number of cents? (Or whatever your smallest unit is.) Many languages also have libraries (or standard libraries) for supporting arbitrary length and precision rational numbers.
- viraptor 1y agoDecimals are always represented in a way that preserves the exact value. That doesn't mean internally it needs to be stored in base10. There's lots of ways to achieve that.
- eru 1y agoI'm not quite sure why anyone would need a 'decimal' data type for currencies? Couldn't you just express everything in eg cents, if you want that? Otherwise, many languages also have libraries for working with exact rational numbers.
- viraptor 1y agoYou're kind of asking why would anyone need decimal data, if they can implement their own decimal data with explicit decimal shifts around the codebase... The answer is - so they don't have to do it.
- koblas 1y agoSome countries don't have cents, some countries have 3 digits of cents. So, yes you can but you also then get thinking you can express everything as ints. But, the moment you need to convert from KWD to VND (84000.00:1) you could be at the edge of the size of a 64 bit int. Note: VND is pretty stable, when currencies start deflating the numbers can get crazy.
- eru 1y agoWell, obviously you put the smallest unit in your integers. Cents was just an example. Old British currency used to be even crazier than your example of a unit divided into 1,000. > But, the moment you need to convert from KWD to VND (84000.00:1) you could be at the edge of the size of a 64 bit int. So? You should use arbitrary length integers for that. Many languages have good support for them. Eg in Python they are the default integer you get.
- Animats 1y agoThe COBOL syntax for that looks like PICTURE IS $-999,999.99 which defines a signed money value represented in decimal. If you print that value, it will be formatted as requested. There are a few other formatting characters. A "CR" at the end will cause negative numbers to be printed with "CR" (Credit) at the end. A "Z" causes lead zero suppression. "*" causes lead zeros to be replaced with asterisks, for check-writing. Numeric display formatting is an attribute of the data. That concept, and built-in support of money-like values, has been lost in later languages. In Rust you can have implicit Display or Debug functions for types, although it's mostly used for debugging.
- rbanffy 1y ago> Numeric display formatting is an attribute of the data Mixing value and presentation wasn’t the brightest idea. In their defence, everything was new at the time, and everywhere they were breaking new ground.