Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort: Subtle bug in (Float64Array) Less #2092

Closed
FlorianUekermann opened this issue Jul 22, 2011 · 2 comments
Closed

sort: Subtle bug in (Float64Array) Less #2092

FlorianUekermann opened this issue Jul 22, 2011 · 2 comments

Comments

@FlorianUekermann
Copy link
Contributor

@FlorianUekermann FlorianUekermann commented Jul 22, 2011

What steps will reproduce the problem?
1. Compile:
package main
import "math"
import "sort"
import "fmt"
func main() {
    s:=[]float64{1,2,3,math.NaN(),1,2,3}
    sort.SortFloat64s(s)
    fmt.Println(s)
}

2. Run

What is the expected output?
[1 1 2 2 3 3 NaN]
or
[NaN 1 1 2 2 3 3]
or
something sorted with NaN at an arbitrary position, since it is not ordered anyway
(impossible with current sort interface, imho)

What do you see instead?
[1 2 3 NaN 1 2 3]
swapping numbers around in the initial vector produces all sorts of funny results.

First reproduced it with webinterface on golang.org should be version and architecture
independent

Reason is how comparison operators works with NaN and float64, (Float64Array) Less
always returns false if NaN is involved.
Fixing the case above would be trivial, just make Less return !(p[i]>p[j]) instead of
p[i]<p[j]. Unfortunately this should lead to infinite loops if more than one NaN is
involved.
Will send correct fix later.
@FlorianUekermann
Copy link
Contributor Author

@FlorianUekermann FlorianUekermann commented Jul 22, 2011

Comment 1:

This testcase works with latest version (some names in sort changed recently):
package main
import "math"
import "sort"
import "fmt"
func main() {
    s:=[]float64{1,2,3,math.NaN(),1,2,3}
    sort.Float64s(s)
    fmt.Println(s)
}
@rsc
Copy link
Contributor

@rsc rsc commented Jul 23, 2011

Comment 2:

This issue was closed by revision 480ef72.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.