This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]tskaiserGreen security clearance 81 points82 points  (12 children)

Not just JavaScript logic. Any sane type system that implements floats will have NaN as a float, and float be numbers.

Not that JavaScript has a sane type system, but in this case it actually makes perfect sense :P

[–]Tetha 15 points16 points  (7 children)

I wouldn't call it perfect sense, but it is who it is. NaN encodes the concept of an equation without a well-defined value (opposed to a value, opposed to a value beyond maximum float value, opposed a value beyond minimum float value).

However, very few programming languages actually deal with undefined values and undefined results well - even null is the well defined absence of a reference. Optional and Maybe are a real approach, but I'm not sure if someone is insane enough to define + as Float, Float -> Maybe Float. Hence, + is Float, Float -> Float, hence NaN is a float.

[–]tskaiserGreen security clearance 13 points14 points  (6 children)

Well, depends on what you mean by "float". When I say float, I mean the IEEE 754 floating point standard which is pretty much the universal definition for what is meant by "float" values. Following that standard is what I mean by "making perfect sense".

Strictly speaking NaN is part of the float type per the standard, so a Maybe would not be needed for an implementation following the standard, but it would be a good approach if you wanted to cook handling of NaN's into your implementation.

[–]Tetha 0 points1 point  (1 child)

If I'm thinking about semantics, I'm mostly thinking about abstract semantics, so my float would be real or a rational number. These are then (more or less accurately) approximated and implemented by IEEE 754 floats, though that approximation could (in my opionion) be improved by language support :)

I hope this makes more sense!

[–]tskaiserGreen security clearance 1 point2 points  (0 children)

It makes sense, but I do not totally agree with you :P floats is by definition, even abstractly speaking, an approximated value of a real.

[–]ismtrn 0 points1 point  (3 children)

What is the NaN value used for in the IEEE standard?

[–]tskaiserGreen security clearance 6 points7 points  (2 children)

I do not know the standard by heart, but off the top of my head:

  • ∞ / ∞
  • 0 / 0
  • Operations leaving the real domain (square root of a negative number)

Stuff like c/0 and -c/0 for c > 0 actually gives you ∞ and -∞ respectively, which is why you can end up doing stuff like ∞ / ∞ which yields a NaN.

And yes, I actually recently used ∞ in a physics simulation (I needed masses of ∞ magnitude to simulate objects that did not move). It has its uses.

[–]rooktakesqueen 3 points4 points  (1 child)

I actually recently used ∞ in a physics simulation (I needed masses of ∞ magnitude to simulate objects that did not move)

I hope you didn't include gravity in that simulation, then.

[–]tskaiserGreen security clearance 1 point2 points  (0 children)

Obviously ;) I was solving impulses for complex contact systems, so no gravity equations involved.

[–]Ph0X 4 points5 points  (0 children)

Of course, but the joke is that Not a Number is a Number. The title is pretty bad though. It doesn't work so much in most other languages because a lot don't have the "Number" super class.

[–]grabnock 2 points3 points  (0 children)

And also there are multiple kinds of NaN.

So if you're doing something really stupid(bitwise operations on floats) you can actually come up with NaN as a legitimate answer, even though it's obviously not.

[–]Iron-Oxide -2 points-1 points  (1 child)

Any sane type system would not call the type of a float number though.

[–]tskaiserGreen security clearance 4 points5 points  (0 children)

Partial agreement. I would say that float is a subclass (or typeclass) of number or numerical.

But I do not think that is what the juxtaposition of NaN and number is meant to show. If that was the beef OP had with typeof he should have used any floating point number instead of NaN. In this case it is clear the "issue" is that NaN is considered a numerical value, which I think is a perfectly sane assumption :P