floating point issue?

Discussion in 'Perl Misc' started by kevin0051, Mar 3, 2010.

  1. kevin0051

    kevin0051 Guest

    I made a perl program as follows.

    -----------------
    $AAA = 4.31;
    $AAA *= 100;
    printf ("%f\n", $AAA);
    printf ("%d\n", $AAA);
    ----------------

    The output of this program is
    431.000000
    430

    I don't know why the second output is 431 instead of 431.
    Can anyone help?

    Thanks
    Kevin
     
    kevin0051, Mar 3, 2010
    #1
    1. Advertising

  2. kevin0051

    sreservoir Guest

    On 3/2/2010 8:54 PM, kevin0051 wrote:
    > Can anyone help?


    yes

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
     
    sreservoir, Mar 3, 2010
    #2
    1. Advertising

  3. kevin0051

    sreservoir Guest

    On 3/2/2010 8:54 PM, kevin0051 wrote:
    > The output of this program is
    > 431.000000
    > 430
    >
    > I don't know why the second output is 431 instead of 431.
    > Can anyone help?


    yes

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
     
    sreservoir, Mar 3, 2010
    #3
  4. kevin0051 <> wrote:
    >I made a perl program as follows.
    >
    >-----------------
    >$AAA = 4.31;
    >$AAA *= 100;
    >printf ("%f\n", $AAA);
    >printf ("%d\n", $AAA);
    >----------------
    >
    >The output of this program is
    >431.000000
    >430
    >
    >I don't know why the second output is 431 instead of 431.


    I suppose you meant "430 instead of 431".

    >Can anyone help?


    Yes, among others your teacher for "Introduction into Fundamentals of
    Basic Computer Numerics".

    For more details please also try
    printf("%.20f\n",$AAA);
    to force 20 digits after the decimal.

    For further explanations please see "perldoc -q 999' or previous
    discussions in this NG about this ever popular topic.

    jue
     
    Jürgen Exner, Mar 3, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,671
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,495
  3. Replies:
    4
    Views:
    1,292
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,349
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    259
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page