Lots of bool operations shouldn't compile
Don Clugston
dac at nospam.com.au
Thu Mar 2 02:17:20 PST 2006
Kyle Furlong wrote:
> Don Clugston wrote:
>> Thomas Kuehne wrote:
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> Don Clugston schrieb am 2006-02-28:
>>>
>>> [snip]
>>>
>>>> OK, there's another bug in there, comparing an int with an imaginary
>>>> real should also not be legal.
>>>>
>>>> if (1 > -2i) { assert(0); }
>>>> if (1 > ireal.nan) { assert(0); }
>>>>
>>>> Reals and ireals cannot be compared. Ever.
>>>
>>> Sure you can. Floats and imaginary floats can both be implicitly
>>> promoted
>>> to complex floats.
>>
>> That only gives you == and !=.
>>
>> <snip>
>>> Thus
>>>
>>>> if (1 > -2i) { assert(0); }
>>>> if (1 > ireal.nan) { assert(0); }
>>>
>>> are interpreted as
>>>
>>>> if (1 + 0i > 0 - 2i) { ... }
>>>> if (1 + 0i > 0 + ireal.nan) { ... }
>>
>> Yes, but that still doesn't work, there's no > for complex numbers.
>> Is 2 - 3i > 3 - 2i ?
>
> One could speak of their magnitudes, I suppose.
>
> creal i = 2 - 3i;
> creal j = 3 - 2i;
>
> real i_m = sqrt(i * complexConjugate(i));
> real j_m = sqrt(j * complexConjugate(j));
>
> but then I guess you are just comparing reals.
And then you have the problem (as in this case) that
(i >= j) is true, and (i <= j) is true, but (i==j) is false.
Actually, though, you _could_ bring the NCEG operators into play, and
define
a > b as abs(a) > abs(b)
a >= b as abs(a) > abs(b) || a==b
a !< b as abs(a) !< abs(b)
a !<=b as abs(a) !< abs(b) && a!=b
a <> b as abs(a) != abs(b)
a !<>b as abs(a) == abs(b)
a == b as a == b
a <>=b as abs(a) <> abs(b) || a==b
a!<>=b as abs(a) == abs(b) && a!=b
which would even work correctly with NANs.
But there's no mathematical precedent for this, AFAIK.
More information about the Digitalmars-d-bugs
mailing list