Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
On Thu, Nov 20, 2008 at 4:25 PM, Wolfgang Laun [EMAIL PROTECTED] wrote: So, calling sqrt with a real 0, should not come back with a complex number. Again, I think this should depend on context. In Perl5, simply use'ing Math::Complex changes the behavior of sqrt such that sqrt(-1) returns i. That fits with the fact that 1/2 returns 0.5, and not zero (unlike certain other languages); I don't have to coerce one of the operands to a float to get a float result. I want to keep that sort of DWIMmishness - if I'm computing with complex numbers, reals should be autopromoted without my having to convert them manually. So simply making sqrt a multi doesn't quite suffice. Calling sqrt with a complex z where Re(z) 0 and Im(z)=0 should return a complex number. Agreed. (Side topic: what about autodemotion? Should calling sqrt with a complex z where Re(z) = 0 and Im(z) = 0 return a complex or a real?) Calling sgn() with a complex - why not give me what I'm (obviously) asking for? My only objection to that behavior was that I want to avoid surprise interactions; a lot of code assumes that sgn() can only return one of three values. As long as it's sufficiently unlikely that a Complex will show up when the programmer isn't expecting it, I'm fine with just having sgn() return 0 for 0 and z/abs(z) for everything else. -- Mark J. Reed [EMAIL PROTECTED]
RE: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
There is a definition for the signum function for a complex argument. sign( z ) = z / |z| for all z != 0 sign( 0 ) = 0 See e.g. http://en.wikipedia.org/wiki/Sign_function Shouldn't be too difficult to implement. -Original Message- From: Carl Mäsak [mailto:[EMAIL PROTECTED] Sent: Mittwoch, 19. November 2008 22:24 To: [EMAIL PROTECTED] Subject: Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex Moritz (), Carl (): masak what should the behaviour of sign($x) be when $x is complex? I'd argue that it's a Failure. Aye.
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
Wolfgang (): There is a definition for the signum function for a complex argument. sign( z ) = z / |z| for all z != 0 sign( 0 ) = 0 See e.g. http://en.wikipedia.org/wiki/Sign_function Shouldn't be too difficult to implement. It isn't, and note that I also proposed it in my first email. I guess the question is more about the programmer's expectations. Is this a case where we serve the programmer better by returning Failure, or by generalizing the Csign function to the complex plane?
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
On Thu, Nov 20, 2008 at 8:34 AM, Carl Mäsak [EMAIL PROTECTED] wrote: I guess the question is more about the programmer's expectations. Is this a case where we serve the programmer better by returning Failure, or by generalizing the Csign function to the complex plane? This is parallel to the case for sqrt($x) for $x0. Which does not by default return Failure, but neither does it return a complex result - it returns NaN. Is that the spec'ed behavior? On the one hand, as long as sqrt() *doesn't* uncomplainingly return complex numbers for negative inputs, it would seem that one is rather less likely to have a complex number when expecting a real than to have a negative when expecting a positive. So having sgn() return complex results for complex inputs is somewhat safer than having sqrt() return complex results for negative inputs. On the other hand, a lot of code will be expecting the return value of sign() to always be one(-1,0,1), and might break horribly if that's not the case. I think the most sensible thing is to be consistent. sgn() fails for non-real input as long as sqrt() returns NaN for negative input. Change the latter behavior (via a pragma or whatever) so that sqrt() returns complex numbers, and then sgn() should start behaving on such numbers. -- Mark J. Reed [EMAIL PROTECTED]
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
Mark (): I think the most sensible thing is to be consistent. sgn() fails for non-real input as long as sqrt() returns NaN for negative input. Change the latter behavior (via a pragma or whatever) so that sqrt() returns complex numbers, and then sgn() should start behaving on such numbers. I like that. Both sign() and sqrt() will then behave like they usually do, without complex surprises. But for those who want the generalized behvaiour, it's only a pragma away. // Carl
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
On Thu, Nov 20, 2008 at 04:31:22PM +0100, Carl Mäsak wrote: : Mark (): : I think the most sensible thing is to be consistent. sgn() fails for : non-real input as long as sqrt() returns NaN for negative input. : Change the latter behavior (via a pragma or whatever) so that sqrt() : returns complex numbers, and then sgn() should start behaving on such : numbers. : : I like that. Both sign() and sqrt() will then behave like they usually : do, without complex surprises. But for those who want the generalized : behvaiour, it's only a pragma away. Doesn't really need a pragma, just import an appropriate multi. Larry
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
Mark (): I think the most sensible thing is to be consistent. sgn() fails for non-real input as long as sqrt() returns NaN for negative input. Change the latter behavior (via a pragma or whatever) so that sqrt() returns complex numbers, and then sgn() should start behaving on such numbers. I like that. Both sign() and sqrt() will then behave like they usually do, without complex surprises. But for those who want the generalized behvaiour, it's only a pragma away. // Carl Rather than a pragma, wouldn't it make more sense to have multi sub sgn(Num) - Num multi sub sqrt(Num) - Num behave appropriately for real numbers and multi sub sgn(Complex) - Complex multi sub sqrt(Complex) - Complex behave appropriately for complex numbers? So people who want sqrt(-1) be return i must pass in Complex.new(-1,0) or whatever the right syntax is.
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
I'd rather retain the dwimmishness of p5. $ perl -MMath::Complex -le 'print sqrt(-1)' i Note that I didn't have to pass in Math::Complex-make(-1,0). Just -1. On 11/20/08, Chris Dolan [EMAIL PROTECTED] wrote: Mark (): I think the most sensible thing is to be consistent. sgn() fails for non-real input as long as sqrt() returns NaN for negative input. Change the latter behavior (via a pragma or whatever) so that sqrt() returns complex numbers, and then sgn() should start behaving on such numbers. I like that. Both sign() and sqrt() will then behave like they usually do, without complex surprises. But for those who want the generalized behvaiour, it's only a pragma away. // Carl Rather than a pragma, wouldn't it make more sense to have multi sub sgn(Num) - Num multi sub sqrt(Num) - Num behave appropriately for real numbers and multi sub sgn(Complex) - Complex multi sub sqrt(Complex) - Complex behave appropriately for complex numbers? So people who want sqrt(-1) be return i must pass in Complex.new(-1,0) or whatever the right syntax is. -- Sent from Gmail for mobile | mobile.google.com Mark J. Reed [EMAIL PROTECTED]
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
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
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
If a programmer calls a function with an argument that has a well-established type, s/he may very well expect a result according to that type, (considering that overloading isn't just a word for not caring about type). So, calling sqrt with a real 0, should not come back with a complex number. Calling sqrt with a complex z where Re(z) 0 and Im(z)=0 should return a complex number. Calling sgn() with a complex - why not give me what I'm (obviously) asking for? On Thu, Nov 20, 2008 at 4:31 PM, Carl Mäsak [EMAIL PROTECTED] wrote: Mark (): I think the most sensible thing is to be consistent. sgn() fails for non-real input as long as sqrt() returns NaN for negative input. Change the latter behavior (via a pragma or whatever) so that sqrt() returns complex numbers, and then sgn() should start behaving on such numbers. I like that. Both sign() and sqrt() will then behave like they usually do, without complex surprises. But for those who want the generalized behvaiour, it's only a pragma away. // Carl
Re: [perl #60674] sign($x) always returns 1 when $x ~~ Complex
Moritz (), Carl (): masak what should the behaviour of sign($x) be when $x is complex? I'd argue that it's a Failure. Aye. [...]