DEC64: Decimal Floating Point (2020)

(crockford.com)

68 points | by vinhnx 9 days ago ago

31 comments

  • dahart 23 minutes ago ago

    > It can precisely represent decimal fractions with 16 decimal places, which makes it very well suited to all applications that are concerned with money.

    How many decimal places do people use for financial calculations in practice? Google search’s AI answer said 4, 6, or sometimes 8. Is that true for large financial institutions, like banks and hedge funds and governments and bitcoin exchanges?

    I’ve heard lots of people saying floating point isn’t good for financial calculations, and I believe it. Would DEC64 actually be good for money if it has 16 base 10 digits, and if so, why? If you need 8 decimal places, you have 8 digits left, and start losing decimal places at around 100M of whatever currency you’re working with. I’m just guessing that working with large sums is exactly when you actually need more decimal places, no?

  • lifthrasiir 8 hours ago ago

    Previously:

    https://news.ycombinator.com/item?id=7365812 (2014, 187 comments)

    https://news.ycombinator.com/item?id=10243011 (2015, 56 comments)

    https://news.ycombinator.com/item?id=16513717 (2018, 78 comments)

    https://news.ycombinator.com/item?id=20251750 (2019, 37 comments)

    Also my past commentary about DEC64:

    > Most strikingly DEC64 doesn't do normalization, so comparison will be a nightmare (as you have to normalize in order to compare!). He tried to special-case integer-only arguments, which hides the fact that non-integer cases are much, much slower thanks to added branches and complexity. If DEC64 were going to be "the only number type" in future languages, it had to be much better than this.

    • indolering 6 hours ago ago

      Wtf is up with the clown car that is floating point standards?

      • maxlybbert 2 hours ago ago

        Well, for one thing, IEEE-757 was a significant improvement on the vendor-specific ways of handling floating point that it replaced ( https://people.eecs.berkeley.edu/~wkahan/ieee754status/754st... ).

        I wasn't a big fan of floating point until I worked with a former college professor who had taught astrophysics. When possible, he preferred to use respected libraries that would give accurate results fast. But when he had to implement things himself, he didn't always necessarily want the fastest or the most accurate implementation; he'd intentionally make and document the tradeoffs for his implementation. He could analyze an algorithm to estimate the accumulated units in the last place error ( https://en.wikipedia.org/wiki/Unit_in_the_last_place ), but he realized when that wasn't necessary.

      • stephencanon 4 hours ago ago

        IEEE 754 is a floating point standard. It has a few warts that would be nice to fix if we had tabula rasa, but on the whole is one of the most successful standards anywhere. It defines a set of binary and decimal types and operations that make defensible engineering tradeoffs and are used across all sorts of software and hardware with great effect. In the places where better choices might be made knowing what we know today, there are historical reasons why different choices were made in the past.

        DEC64 is just some bullshit one dude made up, and has nothing to do with “floating-point standards.”

      • welferkj 2 hours ago ago

        The set of real numbers is continuous and uncountably infinite. Any attempt to fit it into a discrete finite set necessarily requires severe tradeoffs. Different tradeoffs are desirable for different applications.

        • tialaramex an hour ago ago

          Almost All real numbers are non-Computable, so what we're most commonly reaching for is only the rationals, but the thing is DEC64 can't represent lots of those, it seems like a very niche type rather than, as the author asserts, the only numeric type you need.

      • CoastalCoder 5 hours ago ago

        Can you be more specific?

  • tgv 2 hours ago ago

    What's the point of saying that it is "very well suited to all applications that are concerned with money" and then write 3.6028797018963967E+143, which is obviously missing a few gigamultiplujillion.

    • jcarrano 2 hours ago ago

      No point whatsoever. If you have to deal with money you never use floating point. Either use arbitrary precision, or integers with a sufficiently small base like blockchains do (which can be also though of as fixed point). Also you would never be multiplying two money value (there are no "square dollars").

  • RustyRussell 6 hours ago ago

    A goid friend of mine worked on decimal floating point for IBM Power chips (I think it was Power 7 which had hardware support).

    Anyway, he insisted on calling it just "Decimal Floating". Because there was "no point".

    • sevensor an hour ago ago

      Hilarious and apt.

      Either you want fixed point for your minimum unit of accounting or you want floating point because you’re doing math with big / small numbers and you can tolerate a certain amount of truncation. I have no idea what the application for floating point with a weird base is. Unacceptable for accounting, and physicists are smart enough to work in base 2.

  • mgaunard 8 hours ago ago

    There are a bunch of different encodings for decimal floating-point. I fail to see how this is the standard that all languages are converging to.

    IEEE754 normalizes two encodings, BID and DPD, for decimal32, decimal64 and decimal128 precision presets. This is neither of those.

    Many libraries use an approach with a simple significand + exponent, similar to the article, but the representation is not standardized, some use full integral types for this rather than specific bits (C# uses 96+32, Python uses a tuple of arbitrary integers). It's essentially closer to fixed-point but with a variable exponent.

    The representation from the article is definitely a fairly good compromise though, specifically if you're dealing with mostly-fixed-point data.

    • moron4hire 2 hours ago ago

      > I fail to see how this is the standard that all languages are converging to.

      Yes, you are failing to see it because it's not there.

      Crockford had a hot book amongst junior and amateur JavaScript developers 17 years ago. But he's never really been involved in any language standardization work. Even his self-described "invention" of JSON I wouldn't really call an invention and rather a discovery that one could send JS object literals instead of XML over the wire. This discovery also opened a new class of XSS exploits until browsers implemented JSON.parse.

      So, when he says "DEC64 is intended to be the only number type in the next generation of application programming languages," it's just the same, old bloviation he has always employed in his writing.

      • gwbas1c an hour ago ago

        I do think DEC64 has merit: Most of the time, I'd prefer to be able to reason about how non-integers are handled; and I just can't reason about traditional floating point.

        Yes, Doug does bloviate quite a bit. (And when I asked him if there was a way to make callbacks in Node.js / Javascript simpler, he mocked me. A few years later, "await" was added to the language, which generally cleans up callback chains.)

      • ramses0 an hour ago ago

        He literally says he "discovered" it!?!?! https://nofluffjuststuff.com/blog/douglas_crockford/2008/01/...

        ...primary source: """I do not claim to have invented JSON. I claim only that I discovered it. It existed in nature. I identified it, gave it a name, and showed how it was useful. I never claimed to be the first to have discovered JSON. I made my discovery in the spring of 2001. There were other developers who were using it 2000."""

        I see value in this semi-simplistic representation of DEC64, and to respond to a peer comment, consider "DEC64-norm", where non-normalized representations are "illegal" or must be tainted/tagged/parsed like UTF-8 before processing.

        His other "useful" contribution which I lament not seeing anywhere else is "The Crockford Keyboard" another "near-discovery" of existing useful properties in an endemic standard (the English alphabet): https://www.crockford.com/keyboard.html

        I really wish that the X-Box onscreen keyboard used this layout! You could imagine that if you could "hot-key" back to the left row (vowels), and have "tab-complete" (ie: shift-tab/tab) it could be a pretty comfortable typing experience compared to the existing "let's copy QWERTY(?!)".

        ...I feel him as a kindred spirit of pragmatism and complexity reduction (which necessarily) introduces more complexity since we live in a world where complicated things exist. Compared to IEEE floating point numbers, this DEC64 or "Number()" type seems like a breath of fresh air (apart from normalization, as mentioned!).

  • lambdaone 8 hours ago ago

    If you want the job done properly, this already exists: https://en.wikipedia.org/wiki/Decimal128_floating-point_form...

  • shawn_w 7 hours ago ago

    I was expecting something about floating point formats used by some DEC PDP series computer...

    • PopePompus 4 hours ago ago

      Yes, DEC did use a non-IEEE floating point format at least through the VAX-11 era. I was fooled by the title too.

  • pwdisswordfishy 8 hours ago ago

    This seems optimized for fast integer operations. Except that if I only cared about integers, I'd use an actual integer type.

    • adamnew123456 2 hours ago ago

      > Languages for scientific computing like FORTRAN provided multiple floating point types such as REAL and DOUBLE PRECISION as well as INTEGER, often also in multiple sizes. This was to allow programmers to reduce program size and running time. This convention was adopted by later languages like C and Java. In modern systems, this sort of memory saving is pointless.

      More than that, the idea that anyone would be confused about whether to use integer or floating-point types absolutely baffles me. Is this something anyone routinely has trouble with?

      Ambiguity around type sizes I can understand. Make int expand as needed to contain its value with no truncation, as long as you keep i32 when size and wrapping does matter.

      Ambiguity in precision I can understand. I'm not sure this admits of a clean solution beyond making decimal a built-in type that's as convenient (operator support is a must) and fast as possible.

      But removing the int/float distinction seems crazy. Feel free to argue about the meaning of `[1,2,3][0.5]` in your language spec - defining that and defending the choice is a much bigger drag on everyone than either throwing an exception or disallowing it via the type system.

      • bux93 an hour ago ago

        People routinely have trouble picking ints for monetary amounts, leading to all kinds of lovely rounding errors.

    • sfoley 3 hours ago ago

      Near the end of the article, under Motivation:

      > The BASIC language eliminated much of the complexity of FORTRAN by having a single number type. This simplified the programming model and avoided a class of errors caused by selection of the wrong type. The efficiencies that could have gained from having numerous number types proved to be insignificant.

      DEC64 was specifically designed to be the only number type a language uses (not saying I agree, just explaining the rationale).

    • cozzyd 8 hours ago ago

      Well I suppose it might be preferable if javascript used this type...

      • sjrd 7 hours ago ago

        JavaScript engines do optimize integers. They usually represent integers up to +-2^30 as integers and apply integer operations to them. But of course that's not observable.

        • tgv 2 hours ago ago

          I think it's up to 2^53.

  • spacedcowboy 8 hours ago ago

    Atari 8-bit basic used something pretty similar to this [1], except it did have normalization. It only had 10 BCD digits (5 bytes) and 2 digits (1 byte) for exponent, so more of a DEC48 but still… That was a loooong time ago…

    It was slightly more logical to use BCD on the 6502 because it had a BCD maths mode [2], so primitive machine opcodes (ADC, SBC) could understand BCD and preserve carry, zero etc flags

    [1]: https://www.atarimax.com/freenet/freenet_material/5.8-BitCom...

    [2]: http://www.6502.org/tutorials/decimal_mode.html

  • cozzyd 8 hours ago ago

    The memory savings from 32 bit or even 16 bit floats are definitely not pointless! Not to mention doubling simd throughput. Speaking of which, without simd support this certainly can't be used in a lot of applications. Definitely makes sense for financial calculations though.

  • YesThatTom2 4 hours ago ago

    If they reversed the order of the fields, you could sort them by just pretending they are int64’s.

    • Someone 3 hours ago ago

      No you couldn’t, given “Normalization is not required”, so we have

          10³ × 9 < 10¹ × 100000
  • mildred593 9 days ago ago

    Can't wait to have it in my language!