News for hackers. Stuff that matters.
Re:Thoughts on Rust, a few thousand lines in by shepmaster on Wednesday January 09, 2019 @03:39PM (#18868613)
> the escape hatch suggested on the internet, `partial_cmp(...).unwrap_or(Ordering::Less)`

This is often a Bad Idea, as you get unstable sorts and you are right back to the same problem.

- Explanation: How do I get the minimum or maximum value of an iterator containing floating point numbers? — https://stackoverflow.com/a/50308360/155423

- Example: https://play.integer32.com/?version=stable&mode=debug&editio...

See also:

- How to do a binary search on a Vec of floats? — https://stackoverflow.com/q/28247990/155423

Instead, use a wrapper type or raise a panic.

Re:Thoughts on Rust, a few thousand lines in by rusbus on Wednesday January 09, 2019 @05:09PM (#18869474)
OP here -- this post is pretty dated. I currently use the OrderedFloat [1] crate to solve this problem which works quite well and plays nicely with NaN

https://crates.io/crates/ordered-float

Re:Thoughts on Rust, a few thousand lines in by IshKebab on Wednesday January 09, 2019 @05:21PM (#18869587)
To me this feels quite pedantic. Being able to do less than on NaNs and just having it do something vaguely sensible (as in your linked solution) is a far more common requirement than need to handle NaNs specially.

You could even say the reason NaN exists is so that you don't have to check for NaN constantly. Rust is being technically correct but practically really annoying, for basically no benefit.

Re:Thoughts on Rust, a few thousand lines in by steveklabnik on Wednesday January 09, 2019 @05:38PM (#18869736)
For what it's worth, many in the Rust community agree with you. Many people consider this a mistake, in hindsight, though not everyone.
Re:Thoughts on Rust, a few thousand lines in by stabbles on Wednesday January 09, 2019 @04:19PM (#18869010)
Isn't there a default total order for floats? E.g. -Inf < -1.0 < -0.0 < 0.0 < 1.0 < Inf < NaN?
Re:Thoughts on Rust, a few thousand lines in by qznc on Wednesday January 09, 2019 @04:31PM (#18869124)
Yes, there is: https://en.wikipedia.org/wiki/IEEE_754#Total-ordering_predic...

It is not something to use by default though. It is implemented in software and thus a lot slower than the hardware comparison.

Re:Thoughts on Rust, a few thousand lines in by dkarl on Wednesday January 09, 2019 @04:42PM (#18869227)
I'm curious as to why it isn't implemented in hardware. Is it really so rare to need to sort floats, or so common to need a different ordering when you do?
Re:Thoughts on Rust, a few thousand lines in by stabbles on Wednesday January 09, 2019 @05:00PM (#18869394)
Of course sorting floats happens a lot. In practice one rarely encounters NaN's and ±Inf's, so fast comparison for concrete values is the default. I don't know why the 'slow' total order is not implemented in hardware though.

But fortunately in comparison sort algorithms that run in O(n lg n) you can get away with doing an O(n) partitioning of the array into [-, +, NaN] and then applying a fast integer comparison operator to the negative values (-) and positive values (+).

In fact the above idea ties in neatly with QuickSort, which is already based on partitioning & sorting recursively.

Re:Thoughts on Rust, a few thousand lines in by bsder on Wednesday January 09, 2019 @07:16PM (#18870404)
> Of course sorting floats happens a lot.

Is this true?

I am actually struggling to remember the last time I did a sort with a float/double as the key--especially in a performance bounded context

... <thinking> ...

Aha. Graphic engine. Octtree with coordinates.

I really had to think about that.

So, I'm a bit skeptical of float sorting happening a "lot".

Is this perhaps an ML primitive somewhere?

Re:Thoughts on Rust, a few thousand lines in by harrisi on Wednesday January 09, 2019 @11:11PM (#18871465)
Languages that only have floats, such as JavaScript and Lua, certainly sort floats quite often.
Re:Thoughts on Rust, a few thousand lines in by achamayou on Wednesday January 09, 2019 @04:27PM (#18869079)
No, because x < NaN and x > NaN are false for any value of x.
Re:Thoughts on Rust, a few thousand lines in by stabbles on Wednesday January 09, 2019 @04:39PM (#18869197)
Yes, the literal < in the language induces a partial order by convention. What I'm getting at in my comment is that you can define a sensible total ordering.
Re:Thoughts on Rust, a few thousand lines in by wyldfire on Wednesday January 09, 2019 @05:00PM (#18869397)
The challenge becomes what code to emit when you see those operators: native target comparisons, or the software implementation of your total ordering? The latter is safe and slow and the former is fast and IMO idiomatic.

So since no one needs this often enough to emit the soft-float comparison code, we should emit the fast code. If folks need different behavior they should use different types. This is similar to the behavior with integer overflow, which you can opt into by using checked types or checked operations. Though in rust we have a convenience that the overflow-detecting code is emitted for debug targets.

Re:Thoughts on Rust, a few thousand lines in by achamayou on Thursday January 10, 2019 @01:13AM (#18871900)
It is possible to do redefine NaN as something different than what IEEE 754 contains, but it will surprising to some users, and it will come at a performance cost because you can no longer let the hardware handle all float comparisons directly.