Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Seeing if two numbers have the same sign

by graff (Chancellor)
on Jan 12, 2008 at 18:37 UTC ( [id://662091]=note: print w/replies, xml ) Need Help??


in reply to Seeing if two numbers have the same sign

... return if $x > 0 and $y < 0 or $x < 0 and $y > 0; ...
Is the code I've written clear, or could it be improved?

IMHO, that line of code is not clear, and cries out for improvement -- at the very least, there should be parens to eliminate any hint of ambiguity or confusion in the sequence of evaluation:

return if ($x > 0 and $y < 0) or ($x < 0 and $y > 0);
There could be other possible ways to evaluate the four conditions when parens are not provided; some would be nonsensical, and others would simply give the wrong result. Personally, my memorization of precedence rules in perl is not so detailed that I can reliably parse that line in my head the same way perl would. (And the habit of omitting parens is not readily portable across languages.)

In any case, as others point out, there are better ways to improve it.

I'm not interested in the theological debates concerning the sign of zero, though :)

(Did you add that comment after seeing some of the replies, or have people been talking about this despite your clear statement at the beginning not to do that? If the latter, how rude of them! But since your code seems to make an assertion about the sign of zero that some would disagree with, you have to expect some reactions.)

I'm actually kind of surprised at the divergence of views on this point among some of the monks. Every numeric value (even zero) has a sign bit, which is either "on" or "off", and for zero, it happens to be "off". Since your approach would return "true" given "0" and "-1", you are actually testing for something other than "sign". (I'm not sure what this "other thing" would be called...)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://662091]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2026-03-08 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.