Reg- Floating point variables

Discussion in 'Perl Misc' started by Nithy, Nov 12, 2007.

  1. Nithy

    Nithy Guest

    Hi,
    Can anyone help me in floating point round off error. I have the
    code as shown below.

    #!/usr/bin/perl
    #This program increments the value from x.1 to x.9

    $count = 0;
    $out = 0;
    while($out == 0)
    {
    print ("count = ",$count,"\n");
    $count=$count + 0.1;
    if ($count == 0.9)
    {
    $out=1;
    }

    If I run this code, it doesn't terminate when $count reach 0.9. Its
    keep on increasing. And after 5.9 its showing 5.99999999999999 & so
    on..
    But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
    proper output. Why is it so?

    Please let me know your suggestion.

    Thanks in advance.

    Regards,
    Nithy
     
    Nithy, Nov 12, 2007
    #1
    1. Advertising

  2. Nithy

    smallpond Guest

    On Nov 12, 9:13 am, Nithy <> wrote:
    > Hi,
    > Can anyone help me in floating point round off error. I have the
    > code as shown below.
    >
    > #!/usr/bin/perl
    > #This program increments the value from x.1 to x.9
    >
    > $count = 0;
    > $out = 0;
    > while($out == 0)
    > {
    > print ("count = ",$count,"\n");
    > $count=$count + 0.1;
    > if ($count == 0.9)
    > {
    > $out=1;
    >
    > }
    >
    > If I run this code, it doesn't terminate when $count reach 0.9. Its
    > keep on increasing. And after 5.9 its showing 5.99999999999999 & so
    > on..
    > But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
    > proper output. Why is it so?
    >
    > Please let me know your suggestion.
    >
    > Thanks in advance.
    >
    > Regards,
    > Nithy




    Never try to test for exactly equal to a floating point number.
    It doesn't work. Floating point numbers are approximate.
    Change your code to
    if ($count >= 0.9)
    --S
     
    smallpond, Nov 12, 2007
    #2
    1. Advertising

  3. Nithy wrote:
    > If I run this code, it doesn't terminate when $count reach 0.9. Its
    > keep on increasing. And after 5.9 its showing 5.99999999999999 & so
    > on..
    > But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
    > proper output. Why is it so?


    You must have missed "Basics of Computer Numerics":
    Thou shalt not test for equal on floating point numbers

    'perldoc -q 999' gives a very brief introduction of why using floating point
    numbers has it quirks in Perl, too, just like in any other standard
    programming language.

    jue
     
    Jürgen Exner, Nov 12, 2007
    #3
  4. Nithy <> wrote in news:1194876827.609769.271960
    @z24g2000prh.googlegroups.com:

    > #!/usr/bin/perl
    > #This program increments the value from x.1 to x.9


    for my $i ( 1 .. 9 ) {
    $x += $i/10;
    }

    > Why is it so?


    perldoc -q 999

    Sinan

    --
    A. Sinan Unur <>
    (remove .invalid and reverse each component for email address)
    clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>
     
    A. Sinan Unur, Nov 12, 2007
    #4
  5. Nithy

    Nithy Guest

    On Nov 12, 9:39 pm, Sherman Pendley <> wrote:
    > Nithy <> writes:
    > > If I run this code, it doesn't terminate when $count reach 0.9. Its
    > > keep on increasing. And after 5.9 its showing 5.99999999999999 & so
    > > on..
    > > But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
    > > proper output. Why is it so?

    >
    > This is a FAQ. Have a look at:
    >
    > perldoc -q 999
    >
    > > Please let me know your suggestion.

    >
    > In a nutshell, some numbers cannot be exactly represented in binary, for
    > the same reason that numbers such as one-third cannot be exactly represented
    > in decimal.
    >
    > For this reason, it's best to avoid direct equality tests on floating-point
    > numbers. For example, you could rewrite your test of $count to:
    >
    > if ($count >= 0.9) { ... }
    >
    > Another common technique is to measure the difference (aka delta) between a
    > float and a known value, and consider them equal if the delta is small enough:
    >
    > if (abs(0.9 - $count) < 0.000001) { ... }
    >
    > For more (much more) detail, have a look at "What every computer scientist
    > should know about floating-point arithmetic."
    >
    > <http://docs.sun.com/source/806-3568/ncg_goldberg.html>
    >
    > sherm--
    >
    > --
    > WV News, Blogging, and Discussion:http://wv-www.com
    > Cocoa programming in Perl:http://camelbones.sourceforge.net


    Thanks Sherm..
     
    Nithy, Nov 13, 2007
    #5
  6. Nithy

    Nithy Guest

    On Nov 12, 8:30 pm, "A. Sinan Unur" <> wrote:
    > Nithy <> wrote in news:1194876827.609769.271960
    > @z24g2000prh.googlegroups.com:
    >
    > > #!/usr/bin/perl
    > > #This program increments the value from x.1 to x.9

    >
    > for my $i ( 1 .. 9 ) {
    > $x += $i/10;
    >
    > }
    > > Why is it so?

    >
    > perldoc -q 999
    >
    > Sinan
    >
    > --
    > A. Sinan Unur <>
    > (remove .invalid and reverse each component for email address)
    > clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>


    Thanks Sinan..
     
    Nithy, Nov 13, 2007
    #6
  7. Nithy

    Nithy Guest

    On Nov 12, 8:23 pm, "Jürgen Exner" <> wrote:
    > Nithy wrote:
    > > If I run this code, it doesn't terminate when $count reach 0.9. Its
    > > keep on increasing. And after 5.9 its showing 5.99999999999999 & so
    > > on..
    > > But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
    > > proper output. Why is it so?

    >
    > You must have missed "Basics of Computer Numerics":
    > Thou shalt not test for equal on floating point numbers
    >
    > 'perldoc -q 999' gives a very brief introduction of why using floating point
    > numbers has it quirks in Perl, too, just like in any other standard
    > programming language.
    >
    > jue

    Thanks Jue..
     
    Nithy, Nov 13, 2007
    #7
  8. Nithy

    Nithy Guest

    On Nov 12, 7:58 pm, smallpond <> wrote:
    > On Nov 12, 9:13 am, Nithy <> wrote:
    >
    >
    >
    > > Hi,
    > > Can anyone help me in floating point round off error. I have the
    > > code as shown below.

    >
    > > #!/usr/bin/perl
    > > #This program increments the value from x.1 to x.9

    >
    > > $count = 0;
    > > $out = 0;
    > > while($out == 0)
    > > {
    > > print ("count = ",$count,"\n");
    > > $count=$count + 0.1;
    > > if ($count == 0.9)
    > > {
    > > $out=1;

    >
    > > }

    >
    > > If I run this code, it doesn't terminate when $count reach 0.9. Its
    > > keep on increasing. And after 5.9 its showing 5.99999999999999 & so
    > > on..
    > > But If I limits it within 0.8(if $count = 0.1 - 0.7), it gives the
    > > proper output. Why is it so?

    >
    > > Please let me know your suggestion.

    >
    > > Thanks in advance.

    >
    > > Regards,
    > > Nithy

    >
    > Never try to test for exactly equal to a floating point number.
    > It doesn't work. Floating point numbers are approximate.
    > Change your code to
    > if ($count >= 0.9)
    > --S


    Thanks for your prompt response Smallpond.
     
    Nithy, Nov 13, 2007
    #8
    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,837
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,560
  3. Replies:
    4
    Views:
    1,337
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,427
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    296
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page