Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Floating point problems

by PeterPeiGuo (Hermit)
on Oct 24, 2010 at 01:08 UTC ( [id://866997]=note: print w/replies, xml ) Need Help??


in reply to Floating point problems

perldoc is very clear on this: int() simply returns the integer portion, so there is no rounding in the conventional sense.

Peter (Guo) Pei

Replies are listed 'Best First'.
Re^2: Floating point problems
by bluescreen (Friar) on Oct 24, 2010 at 15:15 UTC

    Yeah int is very clear about that, but print isn't.

    Am I asking too much if I see something prints 3 the int of that to be 3 ?

      Am I asking too much if I see something prints 3 the int of that to be 3 ?

      In a word: yes.

      int has a very clearly defined meaning: truncate to an integer. And that behaviour is required for many algorithms.

      It never "rounds-up" as print does:

      printf( "%.16f ", 3-$_ ), print 3-$_ for map 10**-$_, 1 .. 16;; 2.8999999999999999 2.9 2.9900000000000002 2.99 2.9990000000000001 2.999 2.9998999999999998 2.9999 2.9999899999999999 2.99999 2.9999989999999999 2.999999 2.9999999000000002 2.9999999 2.9999999900000001 2.99999999 2.9999999989999999 2.999999999 2.9999999999000000 2.9999999999 2.9999999999900000 2.99999999999 2.9999999999989999 2.999999999999 2.9999999999999001 2.9999999999999 2.9999999999999898 2.99999999999999 2.9999999999999991 3 3.0000000000000000 3

      If you want rounding behaviour rather than truncation, use an appropriate function or module.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

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

    No recent polls found