Earlier quoted context omitted.
Would you prefer NaN be another type, like Maybe? And have to all math in a monad or with chronically repeated case analysis?It's necessary complexity, whichever way you do it.
Oh, I don't really have a good solution in mind--it's just annoying. I guess one option would be to just declare that all NaNs are equal--I'm pretty sure that's how bottoms work in Haskell, and it seems that NaN is essentially a floating-point version of bottom.
NaNs Just Don't Get No Respect
21–30 of 36 posts
Re: NaNs Just Don't Get No Respect
#22The things this author likes about NaN are also properties of NULL in many environments (that NULL cannot be compared to NULL, that operating on NULL returns NULL, etc.); so while you might not see many languages default initializing things to NaN, you do see them default initializing things to NULL with similar effect.
EDIT: I do not know how D implements NaNs; they may have magic to make them more sane to work with.
Re: NaNs Just Don't Get No Respect
#23The things this author likes about NaN are also properties of NULL in many environments (that NULL cannot be compared to NULL, that operating on NULL returns NULL, etc.); so while you might not see many languages default initializing things to NaN, you do see them default initializing things to NULL with similar effect.
Except this is actually worse, since there are many possible values which evaluate to NaN. EDIT: I do not know how D implements NaNs; they may have magic to make them more sane to work with.
What D does do is expose NaNs so the programmer can rely on their existence and use them in a straightforward manner.
Re: NaNs Just Don't Get No Respect
#24Earlier quoted context omitted.
Oh, I don't really have a good solution in mind--it's just annoying. I guess one option would be to just declare that all NaNs are equal--I'm pretty sure that's how bottoms work in Haskell, and it seems that NaN is essentially a floating-point version of bottom.
There are however multiple bit patterns for NaN which complicates that.
This is somewhat simpler--as long as either argument to (==) is NaN the answer is False. However, you still have to figure out that at least one bit pattern corresponds to NaN. If you want them to be equal, you would have to figure out that both are NaN. This is certainly a little more difficult, but I think it isn't much worse than the current scenario.
Re: NaNs Just Don't Get No Respect
#25Earlier quoted context omitted.
There are however multiple bit patterns for NaN which complicates that.
Yes. I was actually thinking about this. However, you have a similar problem with the current model: two NaNs can have the same bit pattern, but still have to be unequal. This is somewhat simpler--as long as either argument to (==) is NaN the answer is False. However, you still have to figure out that at least one bit pattern corresponds to NaN. If you want them to be equal, you would have to figure out that both are…
Re: NaNs Just Don't Get No Respect
#26There are much worse thing than NaN's. They are called denormals. These appear when dealing at the same time with lots of big numbers (very far away from 0) in operations with lots small numbers (close to 0). In such cases the FPU (or whatever deals with fp numbers), switches to a format that could be very inefficient producing an order of magnitude slower operations. For example when dealing with IIR filters in audi…
They're also a sign you're skirting on the limits of FP precision (or worse) so a bit of numerical analysis might still be a good idea...
Re: NaNs Just Don't Get No Respect
#27There are much worse thing than NaN's. They are called denormals. These appear when dealing at the same time with lots of big numbers (very far away from 0) in operations with lots small numbers (close to 0). In such cases the FPU (or whatever deals with fp numbers), switches to a format that could be very inefficient producing an order of magnitude slower operations. For example when dealing with IIR filters in audi…
Denormals are part of IEEE fp. If your implementation is too slow, you can often trade correctness for speed by turning them off in the C/C++ runtimes. They're also a sign you're skirting on the limits of FP precision (or worse) so a bit of numerical analysis might still be a good idea...
Re: NaNs Just Don't Get No Respect
#28Earlier quoted context omitted.
He was specifically talking about equivalence on floating point types. Integers don't have or need NaN.
I was talking about integer values (with floating point representation) being multiplied and added (and divided and floored, I suppose).
For example:
float a = 1.0;
float b = 1000.0;
for (int i = 0; i
There is no guarantee that a == b. Floats make everything more complicated, even simple addition: http://en.wikipedia.org/wiki/Kahan_summation_algorithmRe: NaNs Just Don't Get No Respect
#29Earlier quoted context omitted.
Attmepting to just use simple equivalence with any floating point type a horrible idea to begin with, even without NaNs. You should instead declare equivalence if the absolute difference between the two numbers is less than some bound based on what you're doing and not look for bit equivalency. However, you should be able to examine two NaNs and declare them "equivalent" (for certain definitions of equivalence) by in…
That's not true, there are plenty of cases where using equivalence is just fine. Integer arithmetic, and algorithms that are more reliably written not to contain any empty intervals are two examples.
What do you mean by
algorithms that are more reliably written not to contain any empty intervals are two examples.Re: NaNs Just Don't Get No Respect
#30Earlier quoted context omitted.
Would you prefer NaN be another type, like Maybe? And have to all math in a monad or with chronically repeated case analysis?It's necessary complexity, whichever way you do it.
Oh, I don't really have a good solution in mind--it's just annoying. I guess one option would be to just declare that all NaNs are equal--I'm pretty sure that's how bottoms work in Haskell, and it seems that NaN is essentially a floating-point version of bottom.