Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl oddities

by hv (Parson)
on Mar 01, 2005 at 12:46 UTC ( #435389=note: print w/ replies, xml ) Need Help??


in reply to Perl oddities

I find the behaviour of int() odd: I want it to be like floor(), since then I can easily make ceil() and other useful functions for myself without loading the expensive POSIX module:

sub ceil { -int(-shift) }
I've never been able to see a good reason for the existing "truncate towards zero" behaviour.

Hugo


Comment on Re: Perl oddities
Select or Download Code
Re^2: Perl oddities
by Roy Johnson (Monsignor) on Mar 01, 2005 at 18:21 UTC
    It seems pretty sensible to me to say that the integer portion of -3.5 is -3.

    It's still pretty easy to write ceil and floor:

    sub ceil { my $n = shift; int($n) + ($n > int($n)); } sub floor { my $n = shift; int($n) - ($n < int($n)); }

    Caution: Contents may have been coded under pressure.
        As someone who has just posted a couple of times about undocumented behavior, I will not protest too loudly about being called on this one. Still, it's a convention that has been relied on and seems unlikely to change.

        The obvious fix is:

        sub ceil { my $n = shift; int($n) + ($n > int($n) ? 1 : 0); }

        Caution: Contents may have been coded under pressure.
        We will try to nail those down a little better in Perl 6. Returning the null string for false was a premature optimization on my part, and a false economy (as it were), so the standard boolean operators will return 0 and 1 in Perl 6 so that when you print them out, they look like something rather than nothing. Also, since Perl 6 distinguishes boolean from numeric and string contexts, it can optimize away the actual production of a 0 or 1 value in many cases.

        That's not to say that we're ever going to require people to use 0 and 1 for boolean values. Perl 6 will be just like Perl 5 in letting you treat just about anything as a boolean value. So non-zero numbers are still true, as are strings that aren't null or "0". And maybe we can fix the "0" problem too.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-24 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (244 votes), past polls