wish: a comparison operator that is no more sensitive than printf

Discussion in 'Perl Misc' started by Dan Jacobson, Mar 2, 2004.

  1. Dan Jacobson

    Dan Jacobson Guest

    perldoc perlop says Testing for exact equality of floating-point equality or
    inequality is not a good idea.

    OK, perhaps add a new "squishy comparison operator" one could use so that even
    this wouldn't overrun the last test at least on my computer:
    $i=0; while($i<1){printf "$i\n"; $i+=.1};
    $i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details

    The main frustration is that the < operator is much more sensitive to
    tiny floating trailing digits than any of the printf defaults. I wish
    there was a comparison operator "that was no more sensitive than printf".
     
    Dan Jacobson, Mar 2, 2004
    #1
    1. Advertising

  2. Dan Jacobson

    gnari Guest

    "Dan Jacobson" <> wrote in message
    news:...
    > perldoc perlop says Testing for exact equality of floating-point equality

    or
    > inequality is not a good idea.
    >
    > OK, perhaps add a new "squishy comparison operator" one could use so that

    even
    > this wouldn't overrun the last test at least on my computer:
    > $i=0; while($i<1){printf "$i\n"; $i+=.1};


    if you need exactness try to use integers

    $i=0; while($i<10){printf ($i/10)."\n"; $i+=1};

    no amount of wishing for some magic will change the
    basic reality of how floating point numbers are stored.

    gnari
     
    gnari, Mar 2, 2004
    #2
    1. Advertising

  3. Dan Jacobson

    Thomas Kratz Guest

    Dan Jacobson wrote:
    > perldoc perlop says Testing for exact equality of floating-point equality or
    > inequality is not a good idea.
    >
    > OK, perhaps add a new "squishy comparison operator" one could use so that even
    > this wouldn't overrun the last test at least on my computer:
    > $i=0; while($i<1){printf "$i\n"; $i+=.1};

    ^^^^
    I think you mean:
    $i=0; while($i<1){printf "%f\n", $i; $i+=.1}

    or else you leave the formatting to the stringification with "".

    > $i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details
    >
    > The main frustration is that the < operator is much more sensitive to
    > tiny floating trailing digits than any of the printf defaults. I wish
    > there was a comparison operator "that was no more sensitive than printf".


    Thomas


    --
    open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
    STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
    print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
    &&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..
     
    Thomas Kratz, Mar 2, 2004
    #3
  4. Dan Jacobson

    Ben Morrow Guest

    Dan Jacobson <> wrote:
    > perldoc perlop says Testing for exact equality of floating-point equality or
    > inequality is not a good idea.
    >
    > OK, perhaps add a new "squishy comparison operator" one could use so that even
    > this wouldn't overrun the last test at least on my computer:
    > $i=0; while($i<1){printf "$i\n"; $i+=.1};
    > $i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details
    >
    > The main frustration is that the < operator is much more sensitive to
    > tiny floating trailing digits than any of the printf defaults. I wish
    > there was a comparison operator "that was no more sensitive than printf".


    Ummm.... what's wrong with

    printf('%.19f', $a) < printf('%.19f', $b)

    ?

    Ben

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." - C.S.Lewis
     
    Ben Morrow, Mar 2, 2004
    #4
  5. Dan Jacobson

    Ted Zlatanov Guest

    Re: wish: a comparison operator that is no more sensitive thanprintf

    On Tue, 02 Mar 2004, wrote:

    > perldoc perlop says Testing for exact equality of floating-point
    > equality or inequality is not a good idea.
    >
    > OK, perhaps add a new "squishy comparison operator" one could use so
    > that even this wouldn't overrun the last test at least on my
    > computer:

    [...]
    > The main frustration is that the < operator is much more sensitive
    > to tiny floating trailing digits than any of the printf defaults. I
    > wish there was a comparison operator "that was no more sensitive
    > than printf".


    Right below the advice you mention is this code:

    sub fp_equal {
    my ($X, $Y, $POINTS) = @_;
    my ($tX, $tY);
    $tX = sprintf("%.${POINTS}g", $X);
    $tY = sprintf("%.${POINTS}g", $Y);
    return $tX eq $tY;
    }

    (at least since the Perl 5.6.1 docs)

    Did you not see that code? Or are you asking just because you want a
    new operator in Perl 5 (a wish very unlikely to be granted)?

    Ted
     
    Ted Zlatanov, Mar 2, 2004
    #5
  6. Dan Jacobson

    Ted Zlatanov Guest

    Re: wish: a comparison operator that is no more sensitive thanprintf

    On Tue, 2 Mar 2004, wrote:

    > if you need exactness try to use integers
    >
    > $i=0; while($i<10){printf ($i/10)."\n"; $i+=1};
    >
    > no amount of wishing for some magic will change the
    > basic reality of how floating point numbers are stored.


    I think the OP doesn't want exactness, but the illusion of it :)

    Ted
     
    Ted Zlatanov, Mar 2, 2004
    #6
  7. Ben Morrow <> wrote:

    > Dan Jacobson <> wrote:
    >> perldoc perlop says Testing for exact equality of floating-point
    >> equality or inequality is not a good idea.
    >>
    >> OK, perhaps add a new "squishy comparison operator" one could use so
    >> that even this wouldn't overrun the last test at least on my computer:
    >> $i=0; while($i<1){printf "$i\n"; $i+=.1};
    >> $i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details
    >>
    >> The main frustration is that the < operator is much more sensitive to
    >> tiny floating trailing digits than any of the printf defaults. I wish
    >> there was a comparison operator "that was no more sensitive than
    >> printf".

    >
    > Ummm.... what's wrong with
    >
    > printf('%.19f', $a) < printf('%.19f', $b)
    >
    > ?


    I think you meant sprintf(), not printf().
     
    David K. Wall, Mar 2, 2004
    #7
  8. Re: a comparison operator that is no more sensitive than printf

    Dan Jacobson wrote:
    > perldoc perlop says Testing for exact equality of floating-point
    > equality or inequality is not a good idea.


    Yeah, that is "Introduction into Computer Numerics: lesson 1"

    > OK, perhaps add a new "squishy comparison operator" one could use so
    > that even this wouldn't overrun the last test at least on my computer:
    > $i=0; while($i<1){printf "$i\n"; $i+=.1};
    > $i=0; while($i<1){printf "%.19f\n",$i; $i+=.1}; #for details
    >
    > The main frustration is that the < operator is much more sensitive to
    > tiny floating trailing digits than any of the printf defaults. I wish
    > there was a comparison operator "that was no more sensitive than
    > printf".


    I have no idea what you mean, but have you heard about epsilon environments
    or ranges?
    At least that is what people are talking about in the second half of
    "Introduction into Computer Numerics: lesson 1"

    jue
     
    Jürgen Exner, Mar 3, 2004
    #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. Soso
    Replies:
    1
    Views:
    287
    Victor Bazarov
    Sep 26, 2007
  2. SpreadTooThin
    Replies:
    3
    Views:
    262
    Nick Keighley
    Jun 25, 2009
  3. Roger Pack
    Replies:
    3
    Views:
    119
    Roger Pack
    Dec 3, 2008
  4. Steven D'Aprano
    Replies:
    0
    Views:
    97
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    87
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page