The decimals around us
Decimals numbers are a data type that aims to exactly represent decimal numbers. Some programmers may not know, or fully realize, that, in most programming languages, the numbers that you enter look like decimal numbers but internally are represented as binary—that is, base-2—floating-point numbers. Things that are totally simple for us, such as 0.1, simply cannot be represented exactly in binary. The decimal data type—whatever its stripe or flavor—aims to remedy this by giving us a way of representing and working with decimal numbers, not binary approximations thereof. (Wikipedia has more.)
To help with my work on adding decimals to JavaScript, I've gone through a list of popular programming languages, taken from the 2022 StackOverflow developer survey. What follows is a brief summary of where these languages stand regarding decimals. The intention is to keep things simple. The purpose is:
- If a language does have decimals, say so;
- If a language does not have decimals, but at least one third-party library exists, mention it and link to it. If a discussion is underway to add decimals to the language, link to that discussion.
There is no intention to filter out a language in particular; I'm just working with a slice of languages found in the StackOverflow list linked to earlier. If a language does not have decimals, there may well be multiple third-party decimal libraries. I'm not aware of all libraries, so if I have linked to a "minor" library and neglect to link to a more high-profile one, please let me know. More importantly, if I have misrepresented the basic fact of whether decimals exist at all in a language, send mail.
C
C does not have decimals. But they're working on it! The C23 standard (as in, 2023) proposes to add new fixed bit-width data types (32, 64, and 128) for these numbers.
C#
C# does have decimals via the built-in decimal type.
[...article continues with more programming languages...]