Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: strange arithmetic

by kennethk (Abbot)
on Jan 09, 2015 at 20:42 UTC ( [id://1112799]=note: print w/replies, xml ) Need Help??


in reply to strange arithmetic

I don't think either explanation above is terribly clear, so I'll expound a little. You are getting bitten by order of operations. As described in Terms and List Operators (Leftward),
print ($foo & 255) + 1, "\n";
probably doesn't do what you expect at first glance. The parentheses enclose the argument list for print which is evaluated (printing the result of $foo & 255 ). Then one is added to the return value of print (usually 1). The result is something like this:
1 + 1, "\n"; # Obviously not what you meant.
To do what you meant properly, you must write:
print(($foo & 255) + 1, "\n");
Your code
print (1-1/2000)**$i;
is functionally equivalent to
(print (1-1/2000))**$i;
and not
print ((1-1/2000)**$i);
as you might think. The parentheses bind tighter to the print than to the exponentiation operator.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^2: strange arithmetic
by morgon (Priest) on Jan 09, 2015 at 21:37 UTC
    Thanks for the clarification, but the above postings were indeed clear enough.

    Had I simply used warnings (as suggested) I would not have come here - oh well...

    With regard to "print" I always thought that if you use parenthesis around the arguments you should not have blanks between "print" and the opening parenthesis.
    So in my ideal world Perl would distinguish between print($whatever) and print ($whatever), treating the parenthesis in the first case as part of the print-call while in the second case as part of the expression to be printed.

    But that of course is only my taste and might open another can of worms...

      In Perl, functions, including built in functions like print, act like named list operators. So Perl will look for a list of terms and expressions following the name of the function. Because, Perl, ( and ) are used as list delimiters (much like quotes delimit strings), if the first thing Perl sees after the function name is (, Perl interprets that as the start of a list. The matching ) will end that list. With out the parentheses, Perl has to look for other syntactic clues to determine the end of the list. The main reason for using parentheses around the arguments to a function is to disambiguate the end of the argument list.

      Making a special case for the presence of space between the function name and a ( would re-introduce the ambiguity and break a lot of existing code. Actually, it would be worse because if that ( were the beginning of a list, the matching ), while ending that list, would not be the end of arguments, which would be more confusing.

      Bottom line, far better to use warnings; (and use strict;).

Log In?
Username:
Password:

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

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

    No recent polls found