www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Reappearing error with sorting

reply nnk <nospam nospam.com> writes:
It is quite strange, but the following code sometimes produces a
"core.exception.AssertError C:\d\dmd2\src\phobos\std\array.d(253): Attempting
to fetch the front of an empty array" while trying to sort the array and
sometimes runs just fine:

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

struct Point {
    double x, y;
    static Point opCall( double x, double y ) {
        Point nPoint;
        nPoint.x = x;
        nPoint.y = y;
        return nPoint;
    }
    double angle() {
        return (atan2( y, x ) / PI * 180);
    }
}

void printAngles( Point[] contour ) {
    foreach( Point p; contour )
        writef( "%0.1f ", p.angle() );
    writeln();
}

int main()
{
    Point[] path;
    path.length = 5;
    foreach( inout p; path )
        p = Point( uniform( 0, 100 ), uniform( 0, 100 ) );

    printAngles( path );
    sort!("a.angle() < b.angle()")(path);
    printAngles( path );

    return 0;
}

Using "a.x < b.x" as a sorting criterium produces normal behavior, so I guess
the problem is caused by using a function there. I'm new to D, and so I have
no idea, where exactly the source of this problem lies. Can somebody point me
in the right direction, please?

I'm using the version 2.037 of the compiler with phobos.
Dec 25 2009
parent div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

nnk wrote:
 It is quite strange, but the following code sometimes produces a
 "core.exception.AssertError C:\d\dmd2\src\phobos\std\array.d(253): Attempting
 to fetch the front of an empty array" while trying to sort the array and
 sometimes runs just fine:
 
 import std.stdio,
        std.random,
        std.math,
        std.algorithm;
 
 struct Point {
     double x, y;
     static Point opCall( double x, double y ) {
         Point nPoint;
         nPoint.x = x;
         nPoint.y = y;
         return nPoint;
     }
     double angle() {
         return (atan2( y, x ) / PI * 180);
     }
 }
 
 void printAngles( Point[] contour ) {
     foreach( Point p; contour )
         writef( "%0.1f ", p.angle() );
     writeln();
 }
 
 int main()
 {
     Point[] path;
     path.length = 5;
     foreach( inout p; path )
         p = Point( uniform( 0, 100 ), uniform( 0, 100 ) );
 
     printAngles( path );
     sort!("a.angle() < b.angle()")(path);
     printAngles( path );
 
     return 0;
 }
 
 Using "a.x < b.x" as a sorting criterium produces normal behavior, so I guess
 the problem is caused by using a function there. I'm new to D, and so I have
 no idea, where exactly the source of this problem lies. Can somebody point me
 in the right direction, please?
 
 I'm using the version 2.037 of the compiler with phobos.
It's a compiler bug. change Point.angle to double angle() { double rv = atan2( y, x ) / PI * 180; return rv; } and the problem goes away. However use a real or the direct return and it happens. double angle() { real rv = atan2( y, x ) / PI * 180; return rv; } Some how it must be returning NaN, which will then bugger the sort. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLNh4cT9LetA9XoXwRAniHAKCu/h1oeyHgvI9Fhnpw714zoQeq+ACePCM+ WSYB5f3UNzhjQEyJ0/eeUTg= =Qmst -----END PGP SIGNATURE-----
Dec 26 2009