[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] BooleanUGens (was Re: conditional statement in SynthDef)




On 6 Sep 2005, at 01:55, James Harkins wrote:

Almost:

0 === 0.0
false

hjh

Oops! That's what I get for posting just before bed...

I suppose that one could implement an alternate equivalency method for UGen and SimpleNumber (isEqual?), which for the latter would be a wrapper for ==.

Or optimise the existing code to either test if a and b are Numbers, or order them so that a and b are tested one at a time and use the asFloat === route. Don't know which is faster, but either way it shouldn't be too much of a hit.

Something like the following might be okay. In cases where both operands are UGens this could actually be faster by reducing the number of ifs.

<x-tad-smaller> *new1 { </x-tad-smaller><x-tad-smaller>arg</x-tad-smaller><x-tad-smaller> rate, selector, a, b;


</x-tad-smaller><x-tad-smaller>// eliminate degenerate cases</x-tad-smaller><x-tad-smaller>
if(a.isNumber, {
if (selector == </x-tad-smaller><x-tad-smaller>'*'</x-tad-smaller><x-tad-smaller>, {
if (a == 0.0, { ^0.0 });
if (a == 1.0, { ^b });
if (a == -1.0, { ^b.neg });
},{
if (selector == </x-tad-smaller><x-tad-smaller>'+'</x-tad-smaller><x-tad-smaller>, {
if (a == 0.0, { ^b });
},{
if (selector == </x-tad-smaller><x-tad-smaller>'-'</x-tad-smaller><x-tad-smaller>, {
if (a == 0.0, { ^b.neg });
})})});
}, {
if(b.isNumber, {
if (selector == </x-tad-smaller><x-tad-smaller>'*'</x-tad-smaller><x-tad-smaller>, {
if (b == 0.0, { ^0.0 });
if (b == 1.0, { ^a });
if (b == -1.0, { ^a.neg });
},{
if (selector == </x-tad-smaller><x-tad-smaller>'+'</x-tad-smaller><x-tad-smaller>, {
if (b == 0.0, { ^a });
},{
if (selector == </x-tad-smaller><x-tad-smaller>'-'</x-tad-smaller><x-tad-smaller>, {
if (b == 0.0, { ^a });
},{
if (selector == </x-tad-smaller><x-tad-smaller>'/'</x-tad-smaller><x-tad-smaller>, {
if (b == 1.0, { ^a });
if (b == -1.0, { ^a.neg });
if (b.rate == </x-tad-smaller><x-tad-smaller>'scalar'</x-tad-smaller><x-tad-smaller>, { ^a * b.reciprocal });
})})})});
})});



^</x-tad-smaller><x-tad-smaller>super</x-tad-smaller><x-tad-smaller>.new1(rate, selector, a, b)
}</x-tad-smaller>


Personally I dislike the fact that == and != don't function consistently with the other boolean operators on UGens, so I'd prefer something like this to the 'just document it' option, but it does make me wonder if there aren't other places where this particular polymorphism is being relied upon. I suppose this is the problem of having the boolean operators return a different type for UGens.

S.