News for hackers. Stuff that matters.
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by Dylan16807 on Saturday July 01, 2017 @07:52AM (#14676693)
> There are no “NaN” (not-a-number) bit representations with posits; instead, the calculation is interrupted, and the interrupt handler can be set to report the error and its cause, or invoke a workaround and continue computing, but posits do not make the logical error of assigning a number to something that is, by definition, not a number. This simplifies the hardware considerably.

What a strange claim. Outputting a NaN is easier than raising an interrupt, or could be done within an interrupt, and detecting a NaN input requires a handful of gates or less.

This is not to say that NaNs are a good or bad method, but they're definitely not expensive to implement.

> posits lack a separate ∞ and −∞ [...] “negative zero” is another defiance of mathematical logic that exists in IEEE floats.

I will note that the IEEE standard almost had a projective infinity mode, and x87 has that mode.

> floats are asymmetric and use those bit patterns for a vast and unused cornucopia of NaN values

If we want to ignore languages like javascript and lua, sure.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by cesarb on Saturday July 01, 2017 @09:03AM (#14676936)
> Outputting a NaN is easier than raising an interrupt

That's probably why the recent RISC-V architecture doesn't even give the option of raising an interrupt on floating point errors ("As allowed by the standard, we do not support traps on floating-point exceptions in the base ISA, [...]"). Going even farther, it also doesn't raise an interrupt on integer divide by zero (https://content.riscv.org/wp-content/uploads/2017/05/riscv-s...). That probably simplifies out-of-order implementations, since the only things that can trap then are the instruction decoder (instruction fetch errors and invalid instructions), the load/store instructions, and special instructions (system calls, breakpoints, and like).

(It also doesn't propagate NaN payloads, always returning the same canonical NaN whenever an operation produces a NaN).

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by RhysU on Saturday July 01, 2017 @08:20AM (#14676789)
> If a programmer finds the need for NaN values, it indicates the program is not yet finished...

These guys have never allreduced a timestep in a simulation where something can go aphysical.

I mean, sure, one can communicate aphysical conditions using out-of-band means. But it is so pretty when it just rides along for free as a NaN result.

Edit: I stand corrected having looked up authors. I still think having float effectively be Maybe[float] is a feature not a bug.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by psykotic on Saturday July 01, 2017 @08:41PM (#14680047)
Quiet NaN is critically different from the Maybe monad's Nothing value. Maybe gives you proper exception semantics even if it's implemented by means of predicated data flow rather than early-out control flow (and in a pure, non-strict language like Haskell there isn't a real distinction between data flow and control flow). Whereas quiet NaNs have a significant issue with non-termination where you have to very carefully express all your loop conditions positively. For example, 'while (x - y > eps)' will terminate if either x, y or eps are NaN, but any code that relies on 'if (positive_condition) break' and similar constructs for termination will loop or recurse forever if the condition operands become NaN.

A closer analogue of NaN might be the x ("unknown") value in the three/four-valued logic of hardware simulation languages like Verilog. But the situation in Verilog, despite its challenges, is so much better since you don't have a hard discontinuity between "numbers" and "logical values". Numerical comparisons between unknowns can produce unknown logical values, and you have equations like true & unknown = unknown, true | unknown = true, ~unknown = unknown, etc, so you don't have the issue with NaNs where as soon as something crosses from the floating-point realm to the integer or logical realm, it has to commit to a definite value, with all the aforementioned issues.

All that said, NaNs were the right design choice given the constraints. But it's far from ideal.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by VikingCoder on Saturday July 01, 2017 @08:24AM (#14676802)
> This is not to say that NaNs are a good or bad method, but they're definitely not expensive to implement.

I saw him explain that in float, there a too many bit representations that amount to "NaN". According to a Stack Overflow I found:

"IEEE 754 standard defines 16,777,214 32-bit floating point values as NaNs, or 0.4% of all possible values."

That's "expensive" in terms of losing bits of expressiveness that could have been used to represent actual numbers.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by louithethrid on Saturday July 01, 2017 @03:50PM (#14678633)
Cost of a NaN Operation on chip- same as every other operation, as NaN just is handled and returned like another floating point value - always resulting in a new NaN value- thus a error invalidates all resulting wrong results.

Cost of a Interrupt: 100 ns to 1 microseconds (Quora) Sorry, that solution is simply not interesting for most implementations where floats are used. There are sensors which in realtime hammer out so many values, that not using NaNs means dropping part of your sensor values.

This is a classic case of re-inventing a optimal wheel (fine for racing) and not really looking at the use-cases (not fine for a lot of normal day to day driving).

Im sure it will bring the groupies on conferences.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by Dylan16807 on Saturday July 01, 2017 @05:20PM (#14679099)
> Cost of a Interrupt: 100 ns to 1 microseconds

That sounds like the time it takes to do a context switch to the OS. A math error interrupt doesn't need to do a context switch. The cost doesn't need to be any higher than a branch misprediction at 10-20 cycles.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by louithethrid on Sunday July 02, 2017 @09:25AM (#14682378)
I stand corrected, sorry, it was late at night and yes of course its a floating point operation that goes sour, which in assembly would be handled and then the result passed to the programm handling. Still expensive when encountered in mass though, on a micro controler.

Thanks for putting it right, before the missinformation could spread.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by geezerjay on Sunday July 02, 2017 @12:16PM (#14683118)
> That's "expensive" in terms of losing bits of expressiveness that could have been used to represent actual numbers.

The double precision floating point format already represents between 15 and 17 significant digits, and those who, for some reason, believe they need more expressiveness can already use the illusive and rarely needed 80-bit extended precision floating point format.

0.4% of anything is nothing.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dbcurtis on Saturday July 01, 2017 @10:31AM (#14677241)
Interrupts on NaNs are extremely expensive to implement on vector and GPU processors. We learned that in the Cray-1 days. Handling the exception in a vector iperation kicks off an ultra-expensive context switch. A NaN does not break the flow.
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dnautics on Saturday July 01, 2017 @10:54AM (#14677362)
It seems like an antipattern. If your execution time is bound by an "ultra expensive context switch" occurring when you hit a Nan, then something is wrong with your algorithm.
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by geezerjay on Sunday July 02, 2017 @12:09PM (#14683088)
> Interrupts on NaNs are extremely expensive to implement on vector and GPU processors.

That's ok, because NaNs are exceptional responses to numerical errors thrown during number crunching, indicating that something failed unexpectedly and the output is crap.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by IshKebab on Saturday July 01, 2017 @08:47AM (#14676880)
Why don't they just change the current +/-inf to NaN, and then have the adjacent values be -inf or +inf.

Or is that exception hard to implement?

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dnautics on Saturday July 01, 2017 @01:19PM (#14677934)
here is the spec we drafted a few months ago on exactly what you suggest.

https://github.com/interplanetary-robot/SigmoidNumbers/blob/...

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by IshKebab on Saturday July 01, 2017 @02:43PM (#14678340)
Doesn't that proposal remove the infinities altogether?
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dnautics on Sunday July 02, 2017 @02:53AM (#14681241)
Yep!
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by IshKebab on Sunday July 02, 2017 @05:16AM (#14681638)
So... not exactly what I suggested then?
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dnautics on Sunday July 02, 2017 @05:58AM (#14681774)
oh whoops, reading comprehension fail! Yeah that is an interesting idea that crossed my mind! I'm a little bit hesitant to do that because 1) infinities are not really that important to have, and 2) if we have them we want them to be exact values in the Valid representation, which any value ending in a binary 1 would not be.
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dnautics on Saturday July 01, 2017 @10:48AM (#14677331)
Great observation! That's a compuational mode we are discussing. It's actually trivial to implement.
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by cesarb on Saturday July 01, 2017 @01:27PM (#14677985)
Another option could be to borrow a trick from floating point, and have a separate status register with bits that represent the kinds of errors encountered. Before a many-step calculation, software could clear these bits, and if any of them is set after the calculation, there was an error somewhere. Of course, since you don't have a NaN, you have to specify the bit pattern which will be returned by each kind of failing operation (like RISC-V does with integer divide by zero).

That doesn't help if the result could be partially valid, but if a "square root of minus one" or similar anywhere in the calculation means the result is invalid (and would be a NaN under floating point), sticky error bits in a status register could be sufficient.

Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dnautics on Saturday July 01, 2017 @01:40PM (#14678036)
question: How do you access this status register using standard C? (because if you can't, basically no one will use it)
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by cesarb on Saturday July 01, 2017 @02:08PM (#14678162)
You use feclearexcept() and fetestexcept() from fenv.h: https://linux.die.net/man/3/fenv
Re:Beating Floating Point at Its Own Game: Posit Arithmetic ... by dnautics on Sunday July 02, 2017 @02:53AM (#14681245)
Awesome, thanks!!!