Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: int($x) gives strange result

by Anonymous Monk
on Aug 24, 2009 at 06:40 UTC ( #790755=note: print w/replies, xml ) Need Help??


in reply to int($x) gives strange result

Those results are correct.

perldoc -f int

Returns the integer portion of EXPR. If EXPR is omitted, uses $_. You should not use this function for rounding: one because it truncates towards 0, and two because machine representations of floating point numbers can sometimes produce counterintuitive results. For example, "int(-6.725/0.025)" produces -268 rather than the correct -269; that's because it's really more like -268.99999999999994315658 instead. Usually, the "sprintf", "printf", or the "POSIX::floor" and "POSIX::ceil" functions will serve you better than will int().

What Every Computer Scientist Should Know About Floating-Point Arithmetic

print int( 1.15 * 100 ),"\n"; print int( 1.15001 * 100 ), "\n"; print int( 1.14999 * 100 ), "\n"; __END__ 114 115 114

Replies are listed 'Best First'.
Re^2: int($x) gives strange result
by ikegami (Pope) on Aug 24, 2009 at 17:35 UTC
    That's misleading. floor and ceil suffer the same problem as int in this area.
    $ perl -MPOSIX -wle'$x = 6.725/0.025; print for $x, int $x, floor $x' 269 268 268

      It's not really fair to argue that a post is misleading when the post just quotes the function documentation in it's entirety, and emphasizes (bolded) the part of the documentation that is relevant to the discussion. That doesn't imply the rest of the documentation is on point -- if anything it suggests that the rest of the documentation is not on point, otherwise all/none of it would have been emphasized.

        The OP didn't give any indication that he didn't want to truncate towards zero, so the bolded passage didn't apply, so it never occurred to me that the Anonymous Monk altered the quote to add his own emphasis.

        In fact, it had not even occurred to me until you mentioned it. I took it at face value when he said it was from perldoc.

        In any case, I immediately clarified myself when confusion arose.

        I never said it was. In response to the post, I said it was misleading. I didn't say anything about that line specifically. In fact, I went on to show what was misleading, and it had nothing to do with that line. The "POSIX::floor" and "POSIX::ceil" functions will not serve you better than will int().

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://790755]
help
Chatterbox?
LanX MANAGER: there is a shrimp croquette in my soup ...
thezip throws a croquette at the manager

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-07-16 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (346 votes). Check out past polls.

    Notices?