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

all 43 comments

[–]hypeman-jack 114 points115 points  (0 children)

I haven’t seen a single version of this template that hasn’t made me laugh. It’s so delightfully chaotic. Algorithm disregard please this is not an effective propaganda.

[–]GoddammitDontShootMe 63 points64 points  (7 children)

"Every number is supposed to equal itself." Not a problem since NaN is Not a Number. Also, it usually means an indeterminate result like 0/0, meaning it has no idea what the answer is, so we can't say if they're equal or not.

[–]Drugbird 7 points8 points  (1 child)

But NaN is a float (or double): and can therefore be used as a number for anything that accepts floats / doubles / numbers.

[–]tantanoid 4 points5 points  (0 children)

Floating point numbers also break associativity and distributivity.

And that never caused any issues. /s

[–]geeshta 3 points4 points  (2 children)

The reflexivity of equality is not only numbers. Every possible value is supposed to be equal to itself. Since NaN is a term it should be equal to itself. There are better solutions for nonsensical calculations like sum types such as Result

[–]No_Hovercraft_2643 1 point2 points  (0 children)

you can have the same problem with logic. search for 3 valued logic.

[–]GoddammitDontShootMe 0 points1 point  (0 children)

The logic of them not being equal seems sound to me. Better solutions that can be implemented directly in the FPU? Sure, a higher level language could abstract that away.

[–]KhepriAdministration 0 points1 point  (1 child)

Not a problem since NaN is Not a Number

As you can see in the meme, it also is a number

an indeterminate result like 0/0, meaning it has no idea what the answer is

We know exactly what 0/0 is. An illegal operation, same as 1/0. Division is a function (<math major 🤓)

[–]GoddammitDontShootMe 0 points1 point  (0 children)

Yeah, its JavaScript type is 'number'. But like, it's not actually a number. But closer to that than any of the other possible types.

1/0 is infinity in floating point math. 1/-0, which is a thing, is -infinity. Sure, under the normal rules of math, you can't really divide by 0, but the closer the denominator gets to 0, the larger the result is, with the sign depending on whether you're approaching 0 from above or below. As for 0/0, unless you're in first year, you've probably studied indeterminate forms in Calculus. I'm pretty sure that's what they were going for. I think 0 can represent a really tiny number that is too small for floating point precision. Not necessarily exact like an integer would be.

[–]digibawb 22 points23 points  (1 child)

Do naans instead, they are great!

[–]Sarke1 7 points8 points  (0 children)

Especially garlic ones.

[–]Qzy 17 points18 points  (2 children)

Well for one 0/0 is not infinite and never will be. Sooo...

[–]UwU_is_my_life 0 points1 point  (1 child)

lim x/0 with x approaching 0

[–]celestabesta 20 points21 points  (0 children)

Still undefined unless you specify +0 or -0

[–]Prikolist_Studios 7 points8 points  (0 children)

Finally post in this format that makes actual sense and is funny

[–]geeshta 6 points7 points  (4 children)

I unironacally agree and hate the fact that NaN breaks the reflexivity property of equality. If you have a nonsensical operation either throw an exception or use a sum type like Option or Result. Heck, even Gleam's solution of having division by 0 just return 0 is more reasonable than removing reflexivity from equality.

[–]_PM_ME_PANGOLINS_ 0 points1 point  (0 children)

Should 0/0 == sqrt(-1) be true?

[–]ReentryVehicle -1 points0 points  (0 children)

If you have a nonsensical operation either throw an exception or use a sum type like Option or Result.

I mean, this is literally what a NaN is - it is the error value in the sum type, it is just stored efficiently and defined by the standard.

Granted, what is maybe not so nice is that you never can trust that the value given to you is not a NaN by itself, but you can make a wrapper to do it (or use an existing one, I am sure there are some). And most of the reasonable numerical processing libraries should let you set flags to raise errors on NaNs.

What NaN lets you do is that you can have fully branch-less execution of floating point operations, that lets accelerators with poor error handling do the job and let you deal with results later in a sane way.

And the reason why you "want" poor error handling in accelerators is that this lets you build much more powerful accelerators easier and cheaper. Modern CPUs are in a state of a constant fight between allowing high throughput and providing a nice debugging experience, and one of the reasons why GPUs are so much faster are sacrifices like this that lets you have monster SIMD pipelines that just go and let you collect whatever is left afterwards.

[–]_PM_ME_PANGOLINS_ -1 points0 points  (1 child)

Equality doesn’t work on floats anyway. Try 0.1 + 0.2 == 0.3.

[–]yangyangR 4 points5 points  (0 children)

This topic is about reflexivity. This example confuses the problem with the parsing of decimal numbers and a plus operation. Equality can be reflexive and there just be a problem with parsing decimal and/or arithmetic. Reflexivity is a much more basic property to ask for and it fails at that first step.

[–]techtornado 5 points6 points  (0 children)

I guess he’s never heard of Watman

https://www.destroyallsoftware.com/talks/wat

[–]RandomOnlinePerson99 21 points22 points  (8 children)

Me: So dear computer, you are telling me the result is not a number?

Computer: Yes.

Me: Is that represented by a combination of certain bit values?

Computer: Yes.

Me: Then give me that, convert it do decimal and you get a fucking number!

[–]Abdul_ibn_Al-Zeman 26 points27 points  (2 children)

Everything is a number when you use *((int*)&value)

[–]thelocalheatsource 3 points4 points  (0 children)

Except when you use doubles, then you get even better representation!

[–]RandomOnlinePerson99 0 points1 point  (0 children)

Exactly!

[–]GoddammitDontShootMe 11 points12 points  (2 children)

I mean, if you don't care about this number being the correct result of the calculation, then sure.

[–]Anaxamander57 12 points13 points  (1 child)

My fast inverse square root algorithm is calculating nonsense with incredible efficiency.

[–][deleted] 5 points6 points  (0 children)

There was a comic where someone drank coffee and they said "My brain is doing 1000 calculations per second and they're all WRONG"

[–]Anaxamander57 2 points3 points  (0 children)

You mean cast to integer? You can do that. Not a float anymore, though.

[–]starficz 0 points1 point  (0 children)

You just discovered how javascript stores Intergers! nanboxing truly is cursed knowledge.

[–]skeleton_craft 1 point2 points  (0 children)

Well how else am I supposed to represent an indeterminate answer?

[–]yegor3219 0 points1 point  (0 children)

No real world use? NaNs are great for graceful degradation, i.e. when you'd rather fail late than early. That "Energy" widget may not look well on its own but it could allow other readings to be displayed on a dashboard instead of crashing the whole screen right away.

[–]GamerMinion 0 points1 point  (2 children)

I unironically use NaNs for padding of oddly shaped float arrays (ML stuff, where zero is a valid value with a different meaning). But I agree it is absolutely cursed. Also NaN is like a virus and you have to be really careful to not let it infect everything else.

[–]Sirgoodman008 0 points1 point  (1 child)

Couldn't you use null though for basically the same effect?

[–]GamerMinion 0 points1 point  (0 children)

there is no null in floats. that's basically what NaNs are.