www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5636] New: Array ops broken for comparisons

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5636

           Summary: Array ops broken for comparisons
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: simen.kjaras gmail.com



PST ---
void main( ) {
    auto a = [1,2,3,4,5];
    auto b = [5,4,3,2,1];

    bool[] c = new bool[a.length];
    foreach ( i, ref e; c ) {
        e = a[i] < b[i];
    }
    assert( c == [true, true, false, false, false] );

    c[] = a[] < b[];
    assert( c != [true, true, false, false, false] );
}

One would believe the two results should be equal, but they're not.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 21 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5636




PST ---
Actually, thinking about this some more, I see what is wrong. It is actually
doing:

bool tmp = a[] < b[];
c[] = tmp;

I am not sure about the details of array comparison like that, a fact I feel
makes this bug report more correct.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 21 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5636


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



More context, links to examples from NumPy and AVX instructions that make some
vectors operations even more useful: 

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=130273

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 21 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5636




Thank you for adding this bug report/enhancement request, I was going to add it
myself in a day or two :-)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 21 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5636


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com
           Platform|Other                       |All
         OS/Version|Windows                     |All
           Severity|normal                      |enhancement



This is working as designed, although it's not particularly intuitive.

It might be worth bringing this up on the newsgroup to see what kind of support
it has.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 04 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5636




A discussion thread:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=160067

One of the messages:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=160128


Vector operations like a[]<b[] are meant to return an array of bools. To see
how this is useful you probably must think in terms of vector-style
programming. In NumPy the use of arrays of booleans is common:

 from numpy import *
 a = array([3,6,8,9])
 a == 6
array([False, True, False, False], dtype=bool)
 a >= 7
array([False, False, True, True], dtype=bool)
 a < 5
array([ True, False, False, False], dtype=bool)

 sum( (a%2) == 0 )
2
 b = array([2,6,7,10])
 a == b
array([False, True, False, False], dtype=bool)
 a < b
array([False, False, False, True], dtype=bool) They are sometimes used as masks, it's useful if you have a Vector type that supports multi-index syntax: b = scipy.array([True, False, True, False]) Using the new CPU AVX registers you are able to perform a loop and work on the items of an array in parallel until all the booleans of an array are false. See this, especially Listing 5: http://software.intel.com/en-us/articles/introduction-to-intel-advanced-vector-extensions/ http://www.cs.uaf.edu/2011/spring/cs641/lecture/04_12_AVX.html Vector comparisons have a natural hardware implementation with AVX/AVX2 instructions like _mm256_cmp_ps. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 06 2012