News for hackers. Stuff that matters.
Re:DEC64: Decimal Floating Point by cornholio on Sunday March 04, 2018 @06:10AM (#16514258)
The flaws raised three years ago when it was posted to HN are still relevant:

https://news.ycombinator.com/item?id=7365812

Re:DEC64: Decimal Floating Point by Veedrac on Sunday March 04, 2018 @10:33AM (#16515006)
That thread is rather depressing, being mostly just kneejerk and insults. From what I can tell, the actual criticisms are just:

1. "rounding modes and overflow behavior are not addressed", which is incorrect.

2. "Where's exp(), log(), sin(), cos()?", which you can find in dec64_math.c.

3. "There are also 255 representations of almost all representable numbers", which is incorrect.

4. "it will take around FIFTY INSTRUCTIONS TO CHECK IF TWO NUMBERS ARE EQUAL OR NOT" in the slow case, which is probably true but is unlikely to be common given the design of the type. It can undoubtedly be improved with effort.

5. "The exponent bits should be the higher order bits. Otherwise, this type breaks compatibility with existing comparator circuitry", which seems like an odd comment given the type is not normalized.

6. "With both fields being 2's compliment, you're wasting a bit, just to indicate sign", which AFAICT is just false.

7. "there's no fraction support", which I don't understand.

So the only valid criticism I saw in that whole thread is #4, and even that is only a tenth as valid as its author thought it was.

Re:DEC64: Decimal Floating Point by BlackFingolfin on Sunday March 04, 2018 @03:23PM (#16516477)
Re 1: it is true that there is a single paragraph "discussing" rounding in https://github.com/douglascrockford/DEC64/blob/master/dec64.... -- which is not much. If there is more, I did not find it, and would genuinely appreciate a pointer! In the meantime, though, alternative rounding methods are missing, and those can be quite important for both scientific and business computations. I guess they could be added. But, a lot of things "could be done". Fact is, they were done so far. In so far, DEC64 is mostly a proposal for people to sit down and work out an actual standard. Perhaps this will happen one day, but it seems so far not so many people are convinced it's worth investing efforts into that (I am also not sure if the author is interested in feedback and collaboration? I see no indication for that anywhere)

Re 2: these are at best toy implementations, at worst dangerous (in the sense that they may provide wildly inaccurate results, due to convergence

Re 3: agreed. Though there is still a gargantuan number of values which have 2 or more representations, and that makes all kinds of comparisons more complicated. Dealing with that efficiently in SW is difficult, and more so in HW. It might be worth it if the advantages outweigh it, but at least I personally don't see it.

Re 4: if it can be "undoubtedly improved with effort", why hasn't it been done in several years? Sure, it may be possible, but I will keep my doubts for the time being :-)

I agree with you on 5, 6 and 7.

Re:DEC64: Decimal Floating Point by Veedrac on Sunday March 04, 2018 @09:04PM (#16518167)
> In the meantime, though, alternative rounding methods are missing, and those can be quite important for both scientific and business computations.

My inexpert understanding is that modifying rounding modes is super niche and poorly supported by most things, so this doesn't strike me as much of a problem. A saner replacement to rounding mode flags would just be to offer different operations for those rare cases they are wanted.

> Dealing with that efficiently in SW is difficult, and more so in HW.

Not really; you never really need to normalize values and not doing so makes basically everything other than comparisons cheaper. I don't see how normalizing around every arithmetic operation would make the hardware any simpler.

> if it can be "undoubtedly improved with effort", why hasn't it been done in several years?

Because this is one guy's project and it hasn't seen much (any?) use.

Re:DEC64: Decimal Floating Point by microcolonel on Sunday March 04, 2018 @02:38PM (#16516190)
> So the only valid criticism I saw in that whole thread is #4, and even that is only a tenth as valid as its author thought it was.

Not really here to defend these criticisms, but if #4 is only a tenth as valid as the author thought it was, then does that mean that you know the worst case instruction count to compare two DEC64 numbers is five? Also, we're talking x86 here, how many μops and static instruction bytes are we talking on either side of this? Are all of the branches usually predictable? Can we ever afford to inline something as critical as the comparison operation?

As for the validity of #5, I think this might make more sense in the context of hardware implementations, where a low-order exponent position could increase complexity (requiring adjustments to reuse any of the FPU datapath) AFAIK.

Re:DEC64: Decimal Floating Point by Veedrac on Sunday March 04, 2018 @02:46PM (#16516251)
All I meant is that the author of the comment said it would apply in most cases, where even pessimistically it seems it should apply one time in ten. I don't have detailed performance information.
Re:DEC64: Decimal Floating Point by olliej on Sunday March 04, 2018 @06:57PM (#16517620)
3. Correct there are not 255 possibilities, it’s closer to 50 - I have no paper to work it out on right now. This is because the desire to “not require normalization” is not a good idea.

4. To implement compare quickly you can probably short circuit by first checking that the values are normalized, and normalizing them if not. From a hardware point of view you just added two registers for compare, in addition to the logic for normalizing. For hardware it is probably cheaper to unconditionally normalize.

All math operations will also need to normalize inputs first because otherwise you throw away precision for no reason. For similar reasons all outputs will need to be normalized anyway. Again, given this why would you choose to not just have an implied leading bit?

5. Uh yeah what? If you’re making dedicated hardware you could randomize the bit ordering that’s what making hardware is all about ;)

Re:DEC64: Decimal Floating Point by Veedrac on Sunday March 04, 2018 @08:53PM (#16518122)
> Correct there are not 255 possibilities, it’s closer to 50

1 in 10 values have two representations, 1 in 100 have three, etc., since you can only change the exponent when the (normalized) value is 0 mod 10.

> For hardware it is probably cheaper to unconditionally normalize.

Given normalization is going to be infrequent (most values will either just be integers or have a single representation), and that arithmetic is more common than comparisons, I don't see why this would be. Normalizing a binary value is much easier, so you can't carry floating point intuitions over.

> All math operations will also need to normalize inputs first because otherwise you throw away precision for no reason.

No, you just conform the exponents and handle overflow.

Re:DEC64: Decimal Floating Point by moneytalks on Sunday March 04, 2018 @09:18AM (#16514786)
The desperate clinging to a numeric system oriented to the binary internals of a computer in the human interface to those internals is insane. Choosing base 2 numeric systems as the default number system in any high level language is just shortsighted and stupid.

Maybe DEC64 is flawed, but can we stop pretending that we like binary-based default systems for any reason other than the barrier to entry it creates for newcomers and visceral feeling of communing with computer internals.

Binary has a place -- in low-level languages. Like assembler. There's nothing wrong with any language providing a non-decimal system as an optional number type.

The system we have in many high-level languages is a frankenstein of a binary system that you usually interact with through decimal representation.

The proposal gets at least one thing right: the languages of the future will have a numeric system default that has less insanity. But it will take the passing of a generation or two of developers.

Re:DEC64: Decimal Floating Point by otabdeveloper2 on Sunday March 04, 2018 @09:44AM (#16514846)
There's no such thing as a "binary" or "decimal" number.

In the real world, there are natural numbers, integers, rationals and real numbers.

Computer languages are designed with types that mimic this real-world number stack. Low-level binary implementation details don't leak unless you're overflowing or using bit operations.

What you're really complaining about is the fact that rationals aren't a first-class type in any popular language. With that I agree, it's a shame that corners were cut we three number types instead of four.

Re:DEC64: Decimal Floating Point by tgb on Sunday March 04, 2018 @10:07AM (#16514904)
On the contrary, decimal number types (such as the one in C# that I am most familiar with) are addressing another kind of number that occurs very frequently within "the real world" and is inherently in base ten. Namely, numbers with a fixed number of digits past the decimal point and with specific rounding rules. These are incredibly common in finance.
Re:DEC64: Decimal Floating Point by otabdeveloper2 on Sunday March 04, 2018 @01:40PM (#16515857)
So-called 'decimal' numbers are just rationals with a specific set of denominators.

No sense in special casing the number 10 when you can have proper rational numbers with almost equal effort.

Re:DEC64: Decimal Floating Point by tgb on Sunday March 04, 2018 @02:19PM (#16516068)
The whole point is to support the kind of accounting that happens in real business, which does not do things with rational numbers. One third of your bill does not charge you one third of a penny, these aren't rational numbers but the operations on them involve rounding.
Re:DEC64: Decimal Floating Point by rstuart4133 on Tuesday March 06, 2018 @09:38PM (#16533903)
Dec64 does not reflect what happens in real business any more than IEEE floats does. "There is no reals" applies to currency too, but in a stronger form: in currency there are no fractions either. Translation: in real life, no one can give you a fraction of a cent. So the programmer has to make a decision about what happens to those fractions when you give 1/3 off.

Every newbie programmer tries to avoid thing about this by using IEEE floats. They discover, usually years later after some anal auditor has come down on them like a ton of bricks for the hundredth time because the dropped low order bits from 1/3 of a cent hit that 1 in a billion case and effected a significant digit, and then it finally dawns that 1 in a billion isn't really 1 in a billion because thousands of such calculations get combined into a single profit and loss figure that is out by 2 cents and chasing that 2 cents for 2 weeks really only to discover it was caused by a computer can't compute, really, really pisses off the auditor, that you realise if aren't thinking about those fractions of a cent as hard as a C programmer focuses on malloc(), they will have gone to whoop whoop in half the code you have written. You will have nightmares about divide signs for the rest of your life. Crockford seems to think Dec64 allows the programmer to avoid thinking about the problem. He is just as wrong as every newbie programmer.

There is only one safe format for currency that accurately reflects this reality, that forces you to accept that you must think about those fractions of a cent. It is the humble integer. You put in it the lowest denomination: cents in the case of the USA. And then when you write total = subtotal / 3 * 4, and you test it, your error stands out like dogs balls, and you fix it.

Re:DEC64: Decimal Floating Point by stephencanon on Sunday March 04, 2018 @11:07AM (#16515159)
Tangent: in the real world, there are no real numbers. Whether or not there are arbitrary rational numbers is something of an open question.

"Binary number" as used by grandparent really refers to dyadic rationals (https://en.wikipedia.org/wiki/Dyadic_rational), which are a perfectly well-defined dense subset of the rationals. Similarly, "decimal number" is really "terminating decimal expansion" (or whatever you want to call the decimal analogue of dyadic rational), which is again a well-defined dense subset of the rationals. This is a perfectly valid mathematical distinction; the numbers that people work with day-to-day are much more frequently the latter.

Re:DEC64: Decimal Floating Point by hpcjoe on Sunday March 04, 2018 @03:40PM (#16516580)
Hrmmm. I thought something was fishy with the statement: "Tangent: in the real world, there are no real numbers". The reals are defined as the set of all rational and irrational numbers on the number line. See https://en.wikipedia.org/wiki/Real_number for a reasonably pedagogical discussion.

There are reals, they do exist. In the "real" world (as poorly defined as that is).

The issue is, fundamentally, what programming languages call "real numbers" are not real numbers. They are an approximation to a subset of the reals. This approximation has holes, and the implementations work to some degree to define regions of applicability, and regions of inapplicability. Usually people get hung up or caught in the various traps (inadvertently) added to the specs for "Reals".

Its generally better to say "floating point numbers" than "Reals" in CS, simply because floating point is that subset of the Reals that we are accustomed to using.

I definitely agree with the comment on rationals. I am a fan of Perl6, Julia and other languages ability to use rationals as first class number types.

Sadly, as with other good ideas that require people alter their code/libraries, I fear this will not catch on due to implicit momentum of existing systems.

Re:DEC64: Decimal Floating Point by stephencanon on Sunday March 04, 2018 @03:48PM (#16516627)
In a very reasonable sense, the real numbers do not exist in the real world. Almost all real numbers are non-computable, so under apparently reasonable assumptions about what experiments you can conduct, there is no experiment you can do that will produce a measurement with the value of most real numbers.

From this, it’s fairly non-controvesial to say that only the computable reals exist; these are a tiny (measure-zero) subset of the reals.

If you go further and assume a fundamentally discrete universe (much more controvesial), then all you can really measure are integers.

Re:DEC64: Decimal Floating Point by lmkg on Sunday March 04, 2018 @10:07AM (#16514906)
For Floats in particular, binary implementation details leak all the time due to rounding. A number like 0.0625 can be represented in binary exactly with only 4 bits, but a number like 0.1 can only be represented approximately even when using 64 bits.

This could be solved with a rational-style data type, but I consider the fact that real-style data types don't capture that to also be the implementation leaking.

Re:DEC64: Decimal Floating Point by otabdeveloper2 on Sunday March 04, 2018 @01:38PM (#16515849)
> For Floats in particular, binary implementation details leak all the time due to rounding.

No, they don't. Floats are meant to represent real numbers reasonably accurately.

Real numbers aren't fractions, and the idea of 'rounding' doesn't even apply to reals.

What you want are rational numbers, they solve your use case perfectly.

Floats don't represent rational numbers properly, and they are not meant to.

Re:DEC64: Decimal Floating Point by dnautics on Sunday March 04, 2018 @12:04PM (#16515391)
Rationals are not generally useful for scientific computation because eventually you need to start invoking the relatively costly euclidean algorithm to do basic operations, the compute time is no longer constant. Also lots of operations take square roots anyways, so you wind up in trouble that way.
Re:DEC64: Decimal Floating Point by otabdeveloper2 on Sunday March 04, 2018 @01:42PM (#16515873)
Yeah, you're right, there's a good historic reason for this current mess. Computing originated in scientific computational math problems, so the algorithms and data types are biased for that.

Javascript programmers don't need a number type designed for accurately computing trig and square roots and representing pi, but they got one anyways.

Re:DEC64: Decimal Floating Point by gumby on Sunday March 04, 2018 @11:16AM (#16515199)
Is Common Lisp not a popular language? It has had rational types natively since the 1980s
Re:DEC64: Decimal Floating Point by sedachv on Sunday March 04, 2018 @02:36PM (#16516168)
XLISP (and therefore XLISP-STAT) had rational numbers[1] and came out in 1988, R2RS[2] introduced rational numbers in 1985. The Lisp Machines Lisps had rational numbers. There were a lot of people who had rational numbers in the 1980s and the early 1990s.

[1] http://people.sju.edu/~jhodgson/ppl/lisp/lispman.txt [2] https://dspace.mit.edu/handle/1721.1/5600

Re:DEC64: Decimal Floating Point by poizan42 on Sunday March 04, 2018 @10:03AM (#16514895)
> What you're really complaining about is the fact that rationals aren't a first-class type in any popular language

They are at least in Clojure, which seems quite popular.

Re:DEC64: Decimal Floating Point by hpcjoe on Sunday March 04, 2018 @03:43PM (#16516589)
and in Perl6, and in Julia. Julia is likely more popular than P6 though.
Re:DEC64: Decimal Floating Point by Gibbon1 on Monday March 05, 2018 @02:06AM (#16519305)
A friend sat in the JavaScript standards meeting where they voted not to include rational data types in the language.

He said it's the biggest mistake he's ever seen go down professionally.