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 430 instead of 431.
    Can anyone help?

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

  2. kevin0051

    John Bokma Guest

    John Bokma, Mar 3, 2010
    #2
    1. Advertising

  3. John Bokma wrote:
    > kevin0051 <> writes:
    >
    >> 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 430 instead of 431.
    >> Can anyone help?

    >
    > http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
    >


    This surprised me. I knew int() would truncate of course, but I thought
    printf with would round in the same way for %d as it does for %.0f,
    rather than truncate.

    Xho
    Xho Jingleheimerschmidt, Mar 3, 2010
    #3
  4. kevin0051

    Steve C Guest

    Xho Jingleheimerschmidt wrote:
    > John Bokma wrote:
    >> kevin0051 <> writes:
    >>
    >>> 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 430 instead of 431.
    >>> Can anyone help?

    >>
    >> http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
    >>

    >
    > This surprised me. I knew int() would truncate of course, but I thought
    > printf with would round in the same way for %d as it does for %.0f,
    > rather than truncate.
    >


    It shouldn't surprise you:

    perl -e '$f = 4.31; $f *= 100; $h[$f] = 0; print $#h'
    430

    I would expect any use of a scalar as an integer to truncate.
    Similarly:

    perl -e '$f = "430plusalittle"; printf "%d",$f'
    430

    Using a string as an int stops at the first non-digit.
    It should not take any of the rest of the string into account.
    In the same way, %d should ignore any part of a float other than
    the integer part.
    Steve C, Mar 3, 2010
    #4
  5. On 2010-03-03 03:25, Tad McClellan <> wrote:
    > kevin0051 <> wrote:
    >> I made a perl program as follows.

    >
    > You would have the same problem with any programming language.


    Except COBOL or PL/SQL or bc or ...


    > The problem is related to how numbers are implemented on computers
    > rather than how any particular language behaves.


    Not "number implemented on computers" in general, but specific
    implementations of numbers. Binary floating point numbers are very
    common (because almost all modern processors support them in hardware)
    but there are many other possible number implementations.

    Decimal fixed or floating point numbers avoid the problem for decimal
    fractions, which is sufficient for applications which need to represent
    decimal fractions exactly (e.g. financial applications), but they still
    have the problem for other fractions (1/3, 1/7, ...).

    Rational numbers can be stored as a pair of (potentially big) integers.

    There is no way to represent all numbers in a finite amount of memory of
    course.

    The number representation is not completely independent of the language.

    C for example mandates that float, double, etc. are floating point
    numbers. They can be binary, decimal or use some other base, but they
    can't be fractions, for example.

    COBOL provides decimal fixed point data types.

    PL/SQL has a decimal floating point type (with 38 digits).

    bc has arbitrary precision fixed point numbers.

    Perl provides several number representations as modules.

    hp
    Peter J. Holzer, Mar 5, 2010
    #5
  6. kevin0051

    sreservoir Guest

    On 3/5/2010 5:54 PM, Peter J. Holzer wrote:
    > On 2010-03-03 03:25, Tad McClellan<> wrote:
    >> kevin0051<> wrote:
    >>> I made a perl program as follows.

    >>
    >> You would have the same problem with any programming language.

    >
    > Except COBOL or PL/SQL or bc or ...


    bc is made for math and few people actually use the other two anymore.

    of course, some lisps use fractions.

    > [snipped]


    > There is no way to represent all numbers in a finite amount of memory of
    > course.


    there is no way to represent all numbers full stop.

    --

    "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 6, 2010
    #6
  7. sreservoir <> wrote:
    >On 3/5/2010 5:54 PM, Peter J. Holzer wrote:
    >> There is no way to represent all numbers in a finite amount of memory of
    >> course.

    >
    >there is no way to represent all numbers full stop.


    This is becoming philosophical now, but you can represent any number. In
    the worst case you simply use a verbal description. Granted, that
    doesn't do you much good if you want to use that number in a calculation
    on the computer, but it is possible to represent it because otherwise
    you wouldn't be able to think of this number in the first place.

    jue
    Jürgen Exner, Mar 6, 2010
    #7
  8. kevin0051

    Dr.Ruud Guest

    Jürgen Exner wrote:
    > sreservoir <> wrote:
    >> On 3/5/2010 5:54 PM, Peter J. Holzer wrote:


    >>> There is no way to represent all numbers in a finite amount of memory of
    >>> course.

    >>
    >> there is no way to represent all numbers full stop.

    >
    > This is becoming philosophical now, but you can represent any number. In
    > the worst case you simply use a verbal description. Granted, that
    > doesn't do you much good if you want to use that number in a calculation
    > on the computer, but it is possible to represent it because otherwise
    > you wouldn't be able to think of this number in the first place.


    I think you mean that number almost half way between 2 and 3.
    ;-)

    --
    Ruud
    Dr.Ruud, Mar 6, 2010
    #8
  9. "Dr.Ruud" <> wrote:
    >Jürgen Exner wrote:
    >> sreservoir <> wrote:
    >>> On 3/5/2010 5:54 PM, Peter J. Holzer wrote:

    >
    >>>> There is no way to represent all numbers in a finite amount of memory of
    >>>> course.
    >>>
    >>> there is no way to represent all numbers full stop.

    >>
    >> This is becoming philosophical now, but you can represent any number. In
    >> the worst case you simply use a verbal description. Granted, that
    >> doesn't do you much good if you want to use that number in a calculation
    >> on the computer, but it is possible to represent it because otherwise
    >> you wouldn't be able to think of this number in the first place.

    >
    >I think you mean that number almost half way between 2 and 3.
    >;-)


    No, the other one, the one that is exactly a quarter of an epsilon
    smaller.

    jue
    Jürgen Exner, Mar 6, 2010
    #9
  10. kevin0051

    sreservoir Guest

    On 3/5/2010 8:44 PM, Jürgen Exner wrote:
    > "Dr.Ruud"<> wrote:
    >> Jürgen Exner wrote:
    >>> sreservoir<> wrote:
    >>>> On 3/5/2010 5:54 PM, Peter J. Holzer wrote:

    >>
    >>>>> There is no way to represent all numbers in a finite amount of memory of
    >>>>> course.
    >>>>
    >>>> there is no way to represent all numbers full stop.
    >>>
    >>> This is becoming philosophical now, but you can represent any number. In
    >>> the worst case you simply use a verbal description. Granted, that
    >>> doesn't do you much good if you want to use that number in a calculation
    >>> on the computer, but it is possible to represent it because otherwise
    >>> you wouldn't be able to think of this number in the first place.

    >>
    >> I think you mean that number almost half way between 2 and 3.
    >> ;-)

    >
    > No, the other one, the one that is exactly a quarter of an epsilon
    > smaller.


    nah, it's actually a two ninths of an epsilon.

    --

    "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 6, 2010
    #10
  11. Jürgen Exner <> writes:
    > sreservoir <> wrote:
    >>On 3/5/2010 5:54 PM, Peter J. Holzer wrote:
    >>> There is no way to represent all numbers in a finite amount of memory of
    >>> course.

    >>
    >>there is no way to represent all numbers full stop.

    >
    > This is becoming philosophical now, but you can represent any number. In
    > the worst case you simply use a verbal description. Granted, that
    > doesn't do you much good if you want to use that number in a calculation
    > on the computer, but it is possible to represent it because otherwise
    > you wouldn't be able to think of this number in the first place.


    As long as we're being philosophical, being able to think of a number
    is not a prerequisite for that number's existence.

    The set of finite verbal descriptions is only countably infinite.
    There are uncountably infinitely many real numbers.

    There are also some lovely paradoxes, such as
    "the smallest number that cannot be described in 61 characters".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 6, 2010
    #11
    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,606
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,456
  3. Replies:
    4
    Views:
    1,270
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,284
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    239
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page