Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
... 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...)


In reply to Re: Seeing if two numbers have the same sign by graff
in thread Seeing if two numbers have the same sign by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 21:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found