More on vectorized comparisons

Don Clugston dac at nospam.com
Fri Aug 24 00:25:13 PDT 2012


On 24/08/12 00:13, bearophile wrote:
> Sean Cavanaugh:
>
>> Well, right now the binary operators == != >= <= > and < are required
>> to return bool instead of allowing a user defined type, which prevents
>> a lot of the sugar you would want to make the code nice to write.
>
> The hypothetical D sugar I was looking for is this, where 'a', 'b' and
> 'c' are normal dynamic arrays of doubles (not of float[4] of double[2])
> (currently this code is a syntax error):
>
> if (a[] > 0)
>      b[] += c[];
>
>
> The front-end is able to implement those two lines of code as it likes,
> like seeing those normal arrays as arrays of double[2] (or double[4] on
> more modern CPUs) and put there all the needed intrinsics or assembly
> needed to implement that semantics.
>
> So what's the problem the > operator causes in this code?
>
> Bye,
> bearophile

It's just syntax sugar for a very obscure operation, and it's somewhat 
ambiguous -- is it allowed to use short-circuit evaluation?
Mathematically, it doesn't make sense. You can compare scalars, but 
ordered comparison of vectors is a bit nonsensical, unless it is 
element-wise.
Usually, a[] > 0, a[] < 0, and a[] == 0 will all be false.

Most likely, you really meant  dot(a[]) > 0.

Something like

if ( all( a[] > 0 ) )
     b[] += c[];

is more reasonable. But an implicit 'reduce' in a vector operation has 
little to commend it, I think.



More information about the Digitalmars-d mailing list