4 ms·
> 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
by Dylan16807 9y ago
> 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.
- RhysU 9y ago> 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.
- psykotic 9y agoQuiet 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.
- VikingCoder 9y ago> 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.
- louithethrid 9y agoCost 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.
- Dylan16807 9y ago> 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.
- louithethrid 9y agoI 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.
- 9y ago
- IshKebab 9y agoWhy 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?
- dnautics 9y agoGreat observation! That's a compuational mode we are discussing. It's actually trivial to implement.
- cesarb 9y agoAnother 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.
- dnautics 9y agoquestion: How do you access this status register using standard C? (because if you can't, basically no one will use it)
- cesarb 9y ago> 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-spec-v2.2.pdf#section.6.2 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).
- dbcurtis 9y agoInterrupts 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.
- dnautics 9y agoIt 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.
- geezerjay 9y ago> 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.
- deleted 9y ago[deleted]