6 ms·
Haskell is so much better, and you would learn that with about 5 minutes learning about the number types
by JoelMcCracken 7y ago
Haskell is so much better, and you would learn that with about 5 minutes learning about the number types
- eru 7y agoHaskell still doesn't track NaN in the type system.
- JoelMcCracken 7y agoYou're right, it does, and it exists at the value level if you are using floating point. But in Haskell you have, for example, Rational numbers to avoid many such problems. I was, however, surprised to not see any kind of a (significant) `safeDiv` function on hackage, and no NonZero newtype (outside of quickcheck). But, for this kind of thing its so easy to roll yourself, I am guessing this is what people do (and I have seen).
- ska 7y ago> But in Haskell you have, for example, Rational numbers to avoid many such problems. Hey, it's for sure nice to have a rich numeric stack (see also some lisps), but in numeric work "use something else" is very rarely a practical answer to issues with IEEE-754.
- JoelMcCracken 7y agoSure, it is, but the thing is that a hugely significant amount of the time I've seen those NaNs happen is because of a lack of understanding/care on the part of the devs, and not because they are doing scientific computing. There seem to be a large disconnect here. A big chunk of people don't need the speed that FP math brings, what they need is exactness. And, for most languages with a C-based lineage, actually correct floating point math support seems to fit in the "someday/maybe" categories.
- eru 7y agoYes, rational numbers are the better default choice. You should get floats only when you ask for them.
- JoelMcCracken 7y agoUnfortunately, for many languages, its the only real choice (without introducing some new significant dependency!)
- ridiculous_fish 7y agoHaskell's intro page starts with the infamous minimalist quicksort, which mishandles NaN! https://wiki.haskell.org/Introduction#Quicksort_in_Haskell https://wiki.haskell.org/Introduction#Quicksort_in_Haskell
- saagarjha 7y agoHow should it handle NaN? It's not clear where an incomparable value should go when sorting.
- ken 7y agoWhy is NaN even a concept in modern languages? We don’t have a special “not a string” that all string functions return when they have no useful value to return.
- saagarjha 7y agoBecause it's part of IEEE 754.
- ken 7y agoSo? There’s lots of old standards that don’t fit well with modern programming languages, so we abandoned them, or only use the parts that still make sense.
- saagarjha 7y agoIEEE 754 is one of the standards like two's complement arithmetic which are widely used and have a number of clear advantages, so we continue to use them.
- mac01021 7y agoBecause what is the CPU supposed to do if you divide zero by zero or take the square root of -1? It's actually not exactly a decision being made by the language designers. The IEEE standard governs the floating point logic embedded in ALU hardware. Programming languages tend to expose floating point math in its rawest form for performance reasons.