Floating point

Discussion in 'Perl Misc' started by Sajan, Jan 26, 2004.

  1. Sajan

    Sajan Guest

    Hi
    I am new to perl.
    I have a problem with floating point arithmetic.

    I am using a a variable $time_now which i want to increment by 0.001
    everytime in a while loop until the $tot_time is equal to $time_now.

    I am using

    $accuracy = 0.001;
    $time_now = 0.0;
    $tot_time = 10.0;

    while ($time_now <= $tot_time)
    {
    #couple of calculations based on $time_now;
    $time_now = $time_now + $accuracy;
    }

    now the problem i am facing is

    $time_now becomes 0.0019999999993, 0.0029999999993 etc
    but for my calculations i expect only 0.001, 0.002 etc upto 10.0.
    In short i dont expect such an accurate precision

    Please let me know how to overcome this.

    Thanking you for your help.

    Sajan.
    Sajan, Jan 26, 2004
    #1
    1. Advertising

  2. Sajan wrote:

    > Please let me know how to overcome this.


    Why not simply use an integer counter a la
    $time_count = 0;

    let it rund till $time_count < 10000 (e.g.)

    and then just add this up with $time_count++ and finally multiply by
    $accuracy to get the wanted value.

    HTH
    CA
    Carsten Aulbert, Jan 26, 2004
    #2
    1. Advertising

  3. Also sprach Sajan:

    > I am new to perl.
    > I have a problem with floating point arithmetic.
    >
    > I am using a a variable $time_now which i want to increment by 0.001
    > everytime in a while loop until the $tot_time is equal to $time_now.
    >
    > I am using
    >
    > $accuracy = 0.001;
    > $time_now = 0.0;
    > $tot_time = 10.0;
    >
    > while ($time_now <= $tot_time)
    > {
    > #couple of calculations based on $time_now;
    > $time_now = $time_now + $accuracy;
    > }
    >
    > now the problem i am facing is
    >
    > $time_now becomes 0.0019999999993, 0.0029999999993 etc
    > but for my calculations i expect only 0.001, 0.002 etc upto 10.0.
    > In short i dont expect such an accurate precision


    Non-precision rather. Floating point numbers are usually just an
    approximation. That means there might not be (and usually there is no)
    exact representation of 0.001.

    > Please let me know how to overcome this.


    You can ask perl to round the number to the desired amount of decimals:

    while ($time_now <= $tot_time) {
    $time_now += $accuracy;
    printf "%.3f\n", $time_now;
    }

    With 'sprintf' you can assign the rounded number:

    my $rounded = sprintf "%.3f", $time_now;

    The implication of all that is that you should never do

    if ($num == 0.001)

    because most of the time this condition wont be true. Instead, use
    a delta to allow a certain tolerance:

    my $delta = 1E-8;
    if ( abs($num - 0.001) <= $delta )

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Jan 26, 2004
    #3
  4. Sajan wrote:
    > Hi
    > I am new to perl.


    It appears you are new to programming, too, because your problem is in no
    way specific to Perl.

    > I have a problem with floating point arithmetic.
    > I am using a a variable $time_now which i want to increment by 0.001
    > everytime in a while loop until the $tot_time is equal to $time_now.


    You must have missed the first law of computer numerics
    Thou shalt not use floating point for precision calculations.
    In particular comparing for equality is just stupid.

    > $time_now becomes 0.0019999999993, 0.0029999999993 etc
    > but for my calculations i expect only 0.001, 0.002 etc upto 10.0.
    > In short i dont expect such an accurate precision
    >
    > Please let me know how to overcome this.


    You use an epsilon interval. Further details see any book about Introduction
    to Programming (if it's worth its money), about computer numerics, or just
    "perldoc -q 9999".

    jue
    Jürgen Exner, Jan 26, 2004
    #4
  5. Sajan

    Guest

    (Sajan) wrote:
    > Hi
    > I am new to perl.
    > I have a problem with floating point arithmetic.
    >
    > I am using a a variable $time_now which i want to increment by 0.001
    > everytime in a while loop until the $tot_time is equal to $time_now.
    >
    > I am using
    >
    > $accuracy = 0.001;
    > $time_now = 0.0;
    > $tot_time = 10.0;
    >
    > while ($time_now <= $tot_time)
    > {
    > #couple of calculations based on $time_now;
    > $time_now = $time_now + $accuracy;
    > }
    >
    > now the problem i am facing is
    >
    > $time_now becomes 0.0019999999993, 0.0029999999993 etc
    > but for my calculations i expect only 0.001, 0.002 etc upto 10.0.
    > In short i dont expect such an accurate precision
    >
    > Please let me know how to overcome this.


    Change you units from seconds to milliseconds (or from milliseconds
    to microseconds, or whatever). If you need to multiply/divide by 1000
    after/before input/ouput, do that. But in the loop, just deal with
    integers.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service New Rate! $9.95/Month 50GB
    , Jan 26, 2004
    #5
    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,611
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,458
  3. Replies:
    4
    Views:
    1,270
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    239
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page