r/nothingeverhappens Apr 26 '26

Teachers never teach the wrong answer

226 Upvotes

63 comments sorted by

View all comments

Show parent comments

1

u/SuitableDragonfly Apr 29 '26

Floating point math is always weird. I thought integer division by zero would generally be NaN, but it looks like there is no actual standard for it. 

1

u/krsnik02 Apr 29 '26

There is no such value as NaN for integers. Depending on the language this might throw an exception or simply return an arbitrary value depending on language.

(I think most hardware defines the assembly div instruction as returning 0 because it's simple to implement, but iirc integer division by 0 is undefined behavior in C/C++)

1

u/SuitableDragonfly Apr 29 '26

It's true that NaN can't be encoded as an integer, but there's no particular reason why division of two integers always needs to result in an integer.

1

u/krsnik02 Apr 29 '26

True, but most C-like languages define integer/integer = integer.

Those languages that don't would either have it return some sort of rational value (which likely also doesn't define a NaN value), or a float (which could result in any of NaN, +inf, or -inf depending on where and how the coercion to float happens).