Thread overview
[Issue 4948] New: std.algorithm.sort asserts unexpectedly with certain comparators
Sep 26, 2010
Peter Alexander
Feb 06, 2011
Brad Roberts
Apr 22, 2012
SomeDude
Apr 22, 2012
SomeDude
September 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4948

           Summary: std.algorithm.sort asserts unexpectedly with certain
                    comparators
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: peter.alexander.au@gmail.com


--- Comment #0 from Peter Alexander <peter.alexander.au@gmail.com> 2010-09-26 14:10:13 PDT ---
The following code asserts unexpectedly when compiled with DMD 2.048 using no flags.

void main()
{
    alias Tuple!(int, "x", int, "y") V;
    V[] vs = [V(3, 4), V(6, 8)];
    float arg(V v) { return atan2(cast(float)v.y, cast(float)v.x); }
    bool order(V a, V b) { return arg(a) < arg(b); }
    sort!order(vs);
}

core.exception.AssertError@C:\D\dmd2\windows\bin\..\..\src\phobos\std\array.d(356): Attempting to fetch the front of an empty array

It does not assert when the arg(V) function is replaced to be something more simple, and only asserts when vs has particular values.

I suspect this has something to do with floating point inaccuracies with atan2, but haven't looked deep enough into the issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4948


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei@metalanguage.com
         AssignedTo|nobody@puremagic.com        |andrei@metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4948


Brad Roberts <braddr@puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|x86_64                      |x86


--- Comment #1 from Brad Roberts <braddr@puremagic.com> 2011-02-06 15:40:52 PST ---
Mass migration of bugs marked as x86-64 to just x86.  The platform run on isn't what's relevant, it's if the app is a 32 or 64 bit app.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4948


SomeDude <lovelydear@mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear@mailmetrash.com


--- Comment #2 from SomeDude <lovelydear@mailmetrash.com> 2012-04-22 08:40:00 PDT ---
Note that replacing
    float arg(V v) { return atan2(cast(float)v.y , cast(float)v.x); }
with
    float arg(V v) { return (cast(float)v.y * cast(float)v.x); }
or
    float arg(V v) { return (cast(float)v.y / cast(float)v.x); }
is enough to make the bug disappear...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4948


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |WORKSFORME


--- Comment #3 from Andrei Alexandrescu <andrei@metalanguage.com> 2012-04-22 09:42:49 PDT ---
Works for me on 2.059. I'll also note that arg(v[0]) == arg(v[1]).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4948


SomeDude <lovelydear@mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|WORKSFORME                  |


--- Comment #4 from SomeDude <lovelydear@mailmetrash.com> 2012-04-22 10:00:38 PDT ---
I'm sorry but I'm reopening the case: there is no reason for the sort to fail if two elements are equal. A sort that fails in this case is broken.

Besides, as I've said before, it works with a multiplication or a division, even with equals elements. It only fails with the atan2 function.

import std.stdio;
import std.typecons, std.algorithm, std.math;

void main()
{
    alias Tuple!(int, "x", int, "y") V;
    V[] vs = [V(6, 8), V(1, 2), V(6, 8), V(5,6)];
    float arg(V v) { return (cast(float)v.y * cast(float)v.x); }
    bool order(V a, V b) { return arg(a) < arg(b); }
    writeln(sort!order(vs));
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4948



--- Comment #5 from Andrei Alexandrescu <andrei@metalanguage.com> 2012-04-22 10:43:11 PDT ---
I meant to say it works for me with no assert no nothing. For the last example the output is:

Tuple!(int,"x",int,"y")(1, 2), Tuple!(int,"x",int,"y")(5, 6),
Tuple!(int,"x",int,"y")(6, 8), Tuple!(int,"x",int,"y")(6, 8)]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 22, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4948



--- Comment #6 from Andrei Alexandrescu <andrei@metalanguage.com> 2012-04-22 10:49:24 PDT ---
Rats. That's the output with the multiplication. Using atan2 the output is:

[Tuple!(int,"x",int,"y")(5, 6), Tuple!(int,"x",int,"y")(6, 8),
Tuple!(int,"x",int,"y")(6, 8), Tuple!(int,"x",int,"y")(1, 2)]

That's on OSX Lion/64 bit.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 10, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4948


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |WORKSFORME


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------