News for hackers. Stuff that matters.
Re:DEC64: Decimal Floating Point by StefanKarpinski on Saturday March 08, 2014 @07:53PM (#7367021)
This is some serious amateur hour. The most glaring problem is this:

> There are 255 possible representations of zero. They are all considered to be equal.

There are also 255 representations of almost all representable numbers. For example, 10 is 1 x 10^1 or 10 x 10^0 – or any one of 253 other representations. Aside from the fact that you're wasting an entire byte of your representation, this means that you can't check for equality by comparing bits. Take a look at the the assembly implementation of equality checking:

https://github.com/douglascrockford/DEC64/blob/master/dec64....

The "fast path" (which is ten instruction) applies only if the two numbers have the same exponent. The slow path calls subtraction and returns true if the result is zero. The implementation of subtraction falls back on yet another function, which jumps around even more:

https://github.com/douglascrockford/DEC64/blob/master/dec64....

For most comparisons (no, comparing numbers with the same exponent is not the norm) it will take around FIFTY INSTRUCTIONS TO CHECK IF TWO NUMBERS ARE EQUAL OR NOT. Many of these instructions are branches – and inherently unpredictable ones at that, which means that pipeline stalls will be normal. All told, I would expect equality comparison to typically take around 100 cycles. It's not even clear to me that this implementation is correct because at the end of the subtraction, it compares the result to the zero word, which is only one of the 255 possible representations of zero. The lack of a single canonical representation of any number is just as bad for other arithmetic operations and comparisons, if not worse.

Crockfords bugaboo with IEEE 754 floating-point is bizarre, verging on pathological. He devoted a section in his book "JavaScript: The Good Parts" to a rather ill-informed rant against it. When I saw him give a talk, I took the opportunity to ask him what he thought would be a good alternative to using IEEE 754. His answer was – I shit you not – "I don't know". Apparently this proposal is the answer. No thanks, I will stick with the amazingly successful, ubiquitous, thoroughly thought out standard, that was spearheaded by William Kahan – one of the greatest numerical analysts of all time. Anyone who doesn't appreciate how good we have it with IEEE 754 should really read "An Interview with the Old Man of Floating-Point" [1], in which Kahan relates just how messed up this stuff was before the IEEE 754 standardization process. It should also be noted that there already is an IEEE standard for decimal floating-point [2], which is not only infinitely better thought out than this drivel, but also is already implemented in hardware on many systems sold by IBM and others, specifically for financial applications.

[1] http://www.cs.berkeley.edu/~wkahan/ieee754status/754story.ht...

[2] http://en.wikipedia.org/wiki/Decimal64_floating-point_format

Re:DEC64: Decimal Floating Point by haberman on Saturday March 08, 2014 @08:36PM (#7367095)
This is some seriously hyperbolic negativity.

> There are also 255 representations of almost all representable numbers. [...] Aside from the fact that you're wasting an entire byte of your representation

How is this different than any other floating-point representation? I'm pretty sure IEEE floating-point has the same redundancy, though numbers are normalized so comparisons are cheaper as you note. But IEEE doubles "waste" even more bits due to the 2^52 representations of NaN.

> For most comparisons [...] it will take around FIFTY INSTRUCTIONS TO CHECK IF TWO NUMBERS ARE EQUAL OR NOT.

Good point, sounds like a notable weakness and barrier to adoption.

> Crockfords bugaboo with IEEE 754 floating-point is bizarre, verging on pathological.

He calls it "the most frequently reported bug in JavaScript." Wouldn't you be interested in improving on the most common cause of user confusion in a technology you care about?

Re:DEC64: Decimal Floating Point by StefanKarpinski on Saturday March 08, 2014 @09:02PM (#7367147)
IEEE 754 doesn't waste any bits – there is only a single representation of each value (except for multiple NaNs). In this proposal, there are 255 representations of most values, which means that it has almost an entire byte of redundancy. The waste is bad, but the lack of a canonical representation of each value is worse.

I personally think that the way to handle floating-point confusion is better user education. However, if you really want a decimal standard, then, as I mentioned above, there already is one that is part of the IEEE 754 standard. Not only do there exist hardware implementations, but there are also high-quality software implementations.

A better approach to making things more intuitive in all bases, not just base 10, is using rational numbers. The natural way is to use reduced paris of integers, but this is unfortunately quite prone to overflow. You can improve that by using reduced ratios of – guess what – floating point numbers.

Re:DEC64: Decimal Floating Point by cornholio on Sunday March 09, 2014 @06:41AM (#7368096)
> There are also 255 representations of almost all representable numbers. For example, 10 is 1 x 10^1 or 10 x 10^0 – or any one of 253 other representations.

You are not correct. The smallest significand possible is 1x10^1, but you can't delve further into positive exponents. Conversely, 56 signed bits allows the largest integer power of 10 as 10 000 000 000 000 000 so the exponent will be -15. So there are exactly 17 representations of 10, and that's the worst it gets. All other numbers except powers of 10 have fewer representations, and most real world data affected by noise has a single representation because they use the full precision of the significand, and you can't shift them to the right or left without overflow or loss of precision.

So the redundancy is much less than you think, one in 10 real values has two representations, one in 100 has three etc. This is common for other decimal formats and not that big of a problem, detecting zero is a simple NOR gate on all significant bits.

The real problem with this format is the very high price in hardware (changing the exponent requires recomputing the significand) and complete unsuitability for any kind of numerical problem or scientific number crunching. Because designing a floating point format takes numerical scientists and hardware designers, not assembly programmers and language designers.

Heck, the only reason he put the exponent in the lower byte and not the upper byte, where it would have ensured a perfect compatibility to most positive integers, is that X64 assembly does not allow direct access to the upper byte.

Re:DEC64: Decimal Floating Point by StefanKarpinski on Sunday March 09, 2014 @11:20AM (#7368639)
You are right. I forgot about the interaction between the exponent and the significand. The lack of a canonical representation is still quite problematic.
Re:DEC64: Decimal Floating Point by JonahBraun on Saturday March 08, 2014 @10:33PM (#7367321)
In one format, IEE754 has 24576 possible representations of zero[1], which fits your definition of "wasted bits". Some of your other criticisms might be valid, but at this point I'd like to see an accurate technical comparison between DEC64 and the decimal formats of IEEE 754.

[1] http://en.wikipedia.org/wiki/Decimal128_floating-point_forma...

Re:DEC64: Decimal Floating Point by StefanKarpinski on Sunday March 09, 2014 @11:04AM (#7368602)
This is why decimal floating-point formats are kind of a disaster in general and are only implemented in relatively rare hardware intended for financial uses. In many of those applications, using a decimal fixed point representations is better – i.e. counting in millionths of pennies (you can still count up to ±9 trillion dollars with 64 bits). But yes, a technical comparison of different decimal formats would definitely be interesting. I suspect that despite the occasional failure of intuitiveness, we're far better off with binary formats and better programmer education.
Re:DEC64: Decimal Floating Point by tel on Saturday March 08, 2014 @08:50PM (#7367120)
I feel like that bug has more to do with Javascript's (and clearly Crockford's) inane desire to pretend it is the case that all numeric types are the same.

Nobody who does anything with numbers believes that! Even if all you can do is count your fingers you believe in the difference between integers and floats. They have different algebraic properties entirely and it takes a whole hell of a lot of work to get from one to the other---there's even a whole class (fractions) in between.

Re:DEC64: Decimal Floating Point by haberman on Saturday March 08, 2014 @09:09PM (#7367160)
I'm not sure what that has to do with it. Even if you are ok with the idea that integers and "decimal numbers" are different, it's still confusing that 0.1 + 0.2 != 0.3.

It's confusing because it is very difficult to look at a decimal number and know whether it can be represented exactly as base-2 floating point. It's especially confusing because you get no feedback about it! Here is a Ruby session:

    irb(main):001:0> 0.1
    => 0.1
    irb(main):002:0> 0.2
    => 0.2
    irb(main):003:0> 0.3
    => 0.3
    irb(main):004:0> 0.4
    => 0.4
    irb(main):005:0> 0.5
    => 0.5
They all look exact, right? Well actually only 0.5 is (IIRC?). All the others are actually approximations internally.
Re:DEC64: Decimal Floating Point by StefanKarpinski on Saturday March 08, 2014 @09:14PM (#7367170)
The problem here is that Ruby is lying to you – if you enter 0.3 as a literal, you will not get that value.
Re:DEC64: Decimal Floating Point by haberman on Saturday March 08, 2014 @09:20PM (#7367181)
I would challenge you to name any software system that reliably shows you the precise value of a floating point number.

I couldn't find any (like not a single one), so I wrote a program myself to do it: http://blog.reverberate.org/2012/11/dumpfp-tool-to-inspect-f...

Re:DEC64: Decimal Floating Point by StefanKarpinski on Saturday March 08, 2014 @10:10PM (#7367281)
Python, JavaScript V8, Julia, Java.
Re:DEC64: Decimal Floating Point by haberman on Sunday March 09, 2014 @12:27AM (#7367571)
The precise value of double(0.1) is 0.1000000000000000055511151231257827021181583404541015625. That is precise, not an approximation.

If you know of a program in any of these languages that will print this value for "0.1" using built-in functionality, please let me know because I would love to know about it.

Likewise the precise value of double(1e50) is 100000000000000007629769841091887003294964970946560. Anything else is an approximation of its true value.

In another message you said that what's really important is that the string representation uniquely identifies the precise value. While that will help you reconstruct the value later, it does not help you understand why 0.1 + 0.2 != 0.3.

Re:DEC64: Decimal Floating Point by masida on Sunday March 09, 2014 @09:22AM (#7368377)
Python does it:

  >>> from decimal import Decimal
  >>> Decimal(0.1)
  Decimal('0.1000000000000000055511151231257827021181583404541015625')
  >>> Decimal(1e50)
  Decimal('100000000000000007629769841091887003294964970946560')
Re:DEC64: Decimal Floating Point by haberman on Sunday March 09, 2014 @11:35AM (#7368677)
Great to know, thanks!
Re:DEC64: Decimal Floating Point by PaulAJ on Sunday March 09, 2014 @06:07AM (#7368043)
Haskell will tell you that

    toRational 0.1 = 3602879701896397 % 36028797018963968
    toRational 1e50 = 100000000000000007629769841091887003294964970946560 % 1
Both of those values are precise, although I admit the first isn't really useful for human beings.
Re:DEC64: Decimal Floating Point by StefanKarpinski on Sunday March 09, 2014 @08:49PM (#7370702)
The easiest way to see this exact value in Julia is to convert the Float64 value 0.1 to BigFloat and then print that:

    julia> big(0.1)
    1.000000000000000055511151231257827021181583404541015625e-01

    julia> big(1e50)
    1.0000000000000000762976984109188700329496497094656e+50
Note that this is exactly why you don't normally want to construct BigFloats this way. Instead you want to do this:

    julia> BigFloat("0.1")
    1.000000000000000000000000000000000000000000000000000000000000000000000000000002e-01

    julia> BigFloat("1e50")
    1e+50
Re:DEC64: Decimal Floating Point by StefanKarpinski on Sunday March 09, 2014 @11:31AM (#7368662)
It helps because 0.1 + 0.2 produces 0.30000000000000004 for 64-bit floats – so at least you can see that this value isn't the same as 0.3. In Ruby you just get two values that print the same yet aren't equal, which is way more confusing. I agree that printing the minimal number of digits required for reconstruction does not help with explaining why 0.1, 0.2 and 0.3 in 64-bit floats aren't the real values 1/10, 2/10 and 3/10.
Re:DEC64: Decimal Floating Point by giovannibajo1 on Saturday March 08, 2014 @10:47PM (#7367347)
Python doesn't:

    rasky at monocle in ~
    ↪ python
    Python 2.7.5 (default, Sep  2 2013, 05:24:04)
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 0.1
    0.1
    >>> ^D
    rasky at monocle in ~
    ↪ python3
    Python 3.3.3 (default, Dec 24 2013, 13:54:32)
    [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 0.1
    0.1

It used to, but it was changed to reduce users' confusion.
Re:DEC64: Decimal Floating Point by StefanKarpinski on Saturday March 08, 2014 @11:29PM (#7367430)
We may be speaking across each other here. Ruby is lying in the sense that there are multiple distinct float values that it will print as 0.3 – in particular, confusion ensues when two values look the same but are unequal. These other languages print each distinct float value differently, using just enough decimal digits to reconstruct the exact binary value. Ruby doesn't give you enough digits to reconstruct the value you have. Nobody actually prints the full correct value because it's fifty digits long and is completely redundant given that you know you're dealing with a 64-bit float.
Re:DEC64: Decimal Floating Point by kzrdude on Sunday March 09, 2014 @02:43PM (#7369401)
You're right.

    Python 3.3.5rc1 (default, Feb 23 2014, 17:44:29)  
    >>> 0.3 == 0.2 + 0.1
    False
    >>> 0.2 + 0.1
    0.30000000000000004
    >>> 0.3
    0.3
Re:DEC64: Decimal Floating Point by tel on Saturday March 08, 2014 @10:46PM (#7367345)
Yeah, that's my point. 0.1 and 0.2 do add exactly to 0.3, but in any finite representation of real numbers you'll get rounding errors on some equations like this. If you use an infinite representation then equality is no longer computable.
Re:DEC64: Decimal Floating Point by curveship on Sunday March 09, 2014 @09:42AM (#7368424)
> There are also 255 representations of almost all representable numbers.

This isn't true. Only zero has 255 representations. All other numbers are limited by the size of the coefficient, 16 digits. So a number with 1 significant digit would have 16 representations, one with two would have 15, etc. Not that I'm defending the format, but if we're critiquing it for being sloppy, we need to be careful about such things.

Re:DEC64: Decimal Floating Point by StefanKarpinski on Sunday March 09, 2014 @10:51AM (#7368570)
Good point, thanks for the correction. There is less waste than I thought, but the non-canonical representation issues are still just as bad.
Re:DEC64: Decimal Floating Point by legulere on Sunday March 09, 2014 @05:45AM (#7368009)
Most of the time you shouldn't compare if two floats are equal anyway.
Re:DEC64: Decimal Floating Point by bluetech on Sunday March 09, 2014 @07:16AM (#7368153)
But the author encourages their use for all types of numbers.
Re:DEC64: Decimal Floating Point by FiloSottile on Saturday March 08, 2014 @08:05PM (#7367045)
Could you elaborate on how these points not apply to the IEEE standard for decimal floating-point? From what I can grasp, also the IEEE standard has multiple representations for the same values.

> For most comparisons (no, comparing numbers with the same exponent is not the norm)

I'd say that exponent 0 is the norm, meaning precise numbers in a really wide range, isn't it?

Re:DEC64: Decimal Floating Point by StefanKarpinski on Saturday March 08, 2014 @08:16PM (#7367065)
That is not the case – there is exactly one representation for every single IEEE 754 value besides NaN – and checking if something is NaN or not is easy (two instructions). Not only that, but negative and positive values are in integer order, meaning that you can compare same-signed values using integer comparisons.

What I meant by exponent zero not being the norm is that most numbers you work with won't have exponent zero. Specifically only one in 2^16 of the possibly representable values and only one in 2^24 of the possible representations. If you're using the "natural" representation, then only the integers -9 through 9 have zero exponent.

Re:DEC64: Decimal Floating Point by JonahBraun on Saturday March 08, 2014 @10:15PM (#7367286)
I'm not an expert but you seem to be wrong on this. The wikipedia article you linked to for the IEEE 754 decimal64 format says:

"Because the significand is not normalized, most values with less than 16 significant digits have multiple possible representations; 1×102=0.1×103=0.01×104, etc. Zero has 768 possible representations (1536 if you include both signed zeros)."

Re:DEC64: Decimal Floating Point by StefanKarpinski on Sunday March 09, 2014 @11:10AM (#7368616)
I meant the IEEE 754 binary32/64 standard that has been implemented in hardware everywhere since the early 1980s and is what we all actually use on a day to day basis – the usage of which is what Crockford thinks is the biggest mistake in JavaScript. I now see that the above post was asking specifically about "IEEE 754 decimal", which I missed. Yes, there are multiple representations of many numbers in that too. This is why non-binary floating-point representations kind of suck.
Re:DEC64: Decimal Floating Point by bsder on Sunday March 09, 2014 @11:08AM (#7368614)
Denorms basically don't occur except in very specific numerical cirumstances, so everybody treats these things as unique.

This actually had a significant impact on the implementation of IEEE754 on the DEC Alpha chips. The DEC Alpha microprocessors used a software trap whenever a denormal was encountered.

This was a fine idea--until you start emulating an x86 and discover that one of the big programs you want to support--AutoCAD--buries things into denormals and your performance is dog-slow.

Re:DEC64: Decimal Floating Point by jamesaguilar on Saturday March 08, 2014 @08:10PM (#7367050)
> There are also 255 representations of almost all representable numbers.

Of course ... That's true of any decimal number representation with an exponent. Why is it a problem?

Re:DEC64: Decimal Floating Point by StefanKarpinski on Saturday March 08, 2014 @08:38PM (#7367098)
That's not true – you could easily pick one of the 255 representations here as the representation and make sure that all operations always produce that one. Then you're still wasting a whole byte of your representation – and one in 255 bit patterns are invalid – but at least you can check for equality using bitwise comparison, and operations don't have to worry about all the possible representation of each value.
Re:DEC64: Decimal Floating Point by StefanKarpinski on Sunday March 09, 2014 @11:49AM (#7368718)
Correction: as was pointed out above, there isn't actually a whole byte of waste in this representation. It varies from 255 representations of zero, 17 of 10^-127, to only one representation of numbers that can't be shifted left or right without overflowing or losing bits.