Fixing the imaginary/complex mess

Don nospam at nospam.com
Mon May 4 02:29:29 PDT 2009


Frits van Bommel wrote:
> Don wrote:
>> In the case complex = int/complex, there's no problem. It's the case 
>> int = int/complex that doesn't make sense.
>> And that's fundamentally because cast(real)(2 + 3i) doesn't have a 
>> definite answer.
> 
> Devil's advocate: one could argue it's the same as cast(int) of a float 
> -- it returns the closest representable value, for some definition of 
> "closest".

Sure, you're at liberty to argue anything. But mathematicians don't do 
that. Nor should we. It's misleading, and just not helpful.
BTW, another equally reasonable and equally unhelpful way you could 
define cast(real)(x + iy) is as sqrt(x*x - y*y).

The most justifiable way to do it would be to say
cast(real)(x+iy) is:
  if (y==0) return x; else throw(CastError);
But again, it's just not helpful. Make it a compile-time error.



More information about the Digitalmars-d mailing list