[perl #60674] sign($x) always returns 1 when $x ~~ Complex

2008-11-20 Thread Moritz Lenz via RT
On Wed Nov 19 07:35:48 2008, masak wrote:
 masak what should the behaviour of sign($x) be when $x is complex?

I'd argue that it's a Failure.
If you care about complex numbers, you usually want an angle instead,
which you can get with Complex.polar. (And it's easier to give it a
another meaning later that way)

(If you wanted a complex number with magnitude one, then you should call
that method phase or so, but that would confuse most people when
talking about real numbers.)

Cheers,
Moritz

 masak rakudo: say sign($_) for 42, -42, 0+42i
 p6eval rakudo 32877: OUTPUT[1␤-1␤1␤]
 masak somehow, that last one doesn't feel right to me.
 masak I vote for either an error or $x/abs($x)
 PerlJam why would it be an error?
 masak because 'sign' could be argued to only be applicable to real
 numbers.
 masak i.e. positive numbers, negative numbers, and zero.
 PerlJam but what is it specced to do?  :-)
 masak ah.
 masak it's supposed to return the 'sign' of a number.
 masak i.e. +1, -1 or 0 for the above three groups.
 masak in that way, it's a bit like =, except it always compares to
 0
 PerlJam oh, it's wrong anyway I think.
 PerlJam rakudo: say sign(-5+3i);
 p6eval rakudo 32877: OUTPUT[1␤]
 PerlJam that can't be right.
 masak it always returns 1 on complex numbers.
 * masak reports a bug





Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex

2008-11-20 Thread TSa

HaloO,

Moritz Lenz via RT wrote:

On Wed Nov 19 07:35:48 2008, masak wrote:

masak what should the behaviour of sign($x) be when $x is complex?


I'd argue that it's a Failure.


This is a bit drastic. If one computes in the complex domain
a complex valued sign function is appropriate.

   multi sub sign(Complex $z -- Complex)
   {
   return $z.abs ?? $z / $z.abs !! 0;
   }

That is, it returns a unit complex number or zero. This nicely
fits the notion that the set {-1,1} is the zero dimensional unit
sphere with 0 as center just like the unit circle is the one
dimensional unit sphere.


Regards, TSa.
--

The unavoidable price of reliability is simplicity -- C.A.R. Hoare
Simplicity does not precede complexity, but follows it. -- A.J. Perlis
1 + 2 + 3 + 4 + ... = -1/12  -- Srinivasa Ramanujan