Windows

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • comparing floating point numbers

    5 answers - 722 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    John Deighan::
    Is there a safe way to compare 2 floating point numbers in Perl? [snip]
    My debugger says that they're both '630.24' [snip]
    However, the == test fails and the != test succeeds
    can you post code with the comparison == that fails ?
    if the debugger says they're the same, they're very very probably the same.
    are you sure the variable (or whatever) you're giving to == are really what you
    want them to be?
    there are lots of ways to compare numbers, right down to looping over the bits
    and logically
    XNRing them.
    ed c
    Perl-Win32-Users mailing list
    Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    To unsubscribe:
  • No.1 | | 1637 bytes | |

    At 02:20 PM 7/24/2005, Ed Chester wrote:

    >John Deighan::

    Is there a safe way to compare 2 floating point numbers in Perl? [snip]
    My debugger says that they're both '630.24' [snip]
    However, the == test fails and the != test succeeds
    >
    >can you post code with the comparison == that fails ?
    >if the debugger says they're the same, they're very very probably the same.
    >are you sure the variable (or whatever) you're giving to == are
    >really what you
    >want them to be?
    >there are lots of ways to compare numbers, right down to looping over the bits
    >and logically
    >XNRing them.


    Sorry about the lack of sample code, but I know that people who work
    with floating point numbers know about this problem, and I was
    wondering what the best solution was. Here is sample code with
    output. Note that I'm not formatting the output or rounding or
    anything - just printing out the contents of the 2 variables.

    my $sum1 = -237.15;
    my $sum2;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;

    print("sum1 = $sum1\n");
    print("sum2 = $sum2\n");
    if ($sum1 == $sum2) {
    print("EQUAL\n");
    }
    else {
    print("NT EQUAL\n");
    }

    UTPUT:

    sum1 = -237.15
    sum2 = -237.15
    NT EQUAL

    Perl-Win32-Users mailing list
    Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    To unsubscribe:
  • No.2 | | 2202 bytes | |

    When I substituted 'eq' versus '==' it works.

    Not sure why though

    Ken

    At 03:07 PM 7/24/2005, John Deighan wrote:
    >At 02:20 PM 7/24/2005, Ed Chester wrote:
    >
    >>John Deighan::

    >Is there a safe way to compare 2 floating point numbers in Perl? [snip]
    >My debugger says that they're both '630.24' [snip]
    >However, the == test fails and the != test succeeds
    >>
    >>can you post code with the comparison == that fails ?
    >>if the debugger says they're the same, they're very very probably the same.
    >>are you sure the variable (or whatever) you're giving to == are
    >>really what you
    >>want them to be?
    >>there are lots of ways to compare numbers, right down to looping over the
    >>bits
    >>and logically
    >>XNRing them.

    >
    >Sorry about the lack of sample code, but I know that people who work with
    >floating point numbers know about this problem, and I was wondering what
    >the best solution was. Here is sample code with output. Note that I'm not
    >formatting the output or rounding or anything - just printing out the
    >contents of the 2 variables.
    >
    >my $sum1 = -237.15;
    >my $sum2;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >
    >print("sum1 = $sum1\n");
    >print("sum2 = $sum2\n");
    >if ($sum1 == $sum2) {

    print("EQUAL\n");
    }
    >else {

    print("NT EQUAL\n");
    }
    >
    >UTPUT:
    >
    >sum1 = -237.15
    >sum2 = -237.15
    >NT EQUAL
    >
    >
    >
    >Perl-Win32-Users mailing list
    >Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    >To unsubscribe:


    Perl-Win32-Users mailing list
    Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    To unsubscribe:
  • No.3 | | 1841 bytes | |

    John Deighan wrote:
    At 02:20 PM 7/24/2005, Ed Chester wrote:


    >>John Deighan::
    >>

    Is there a safe way to compare 2 floating point numbers in Perl? [snip]
    My debugger says that they're both '630.24' [snip]
    However, the == test fails and the != test succeeds
    >>
    >>can you post code with the comparison == that fails ?
    >>if the debugger says they're the same, they're very very probably the same.
    >>are you sure the variable (or whatever) you're giving to == are
    >>really what you
    >>want them to be?
    >>there are lots of ways to compare numbers, right down to looping over the bits
    >>and logically
    >>XNRing them.


    Sorry about the lack of sample code, but I know that people who work
    with floating point numbers know about this problem, and I was
    wondering what the best solution was. Here is sample code with
    output. Note that I'm not formatting the output or rounding or
    anything - just printing out the contents of the 2 variables.

    my $sum1 = -237.15;
    my $sum2;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;

    print("sum1 = $sum1\n");
    print("sum2 = $sum2\n");
    if ($sum1 == $sum2) {

    You could try something like:

    if (abs ($sum1 - $sum2) < .0001) {

    print("EQUAL\n");
    }
    else {
    print("NT EQUAL\n");
    }

    UTPUT:

    sum1 = -237.15
    sum2 = -237.15
    NT EQUAL

    Perl-Win32-Users mailing list
    Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    To unsubscribe:
  • No.4 | | 2833 bytes | |

    What kind of post is this?

    I do not see that anything was added at all. Give us all a break - don't
    bother - whatever your intentions.

    At 06:35 PM 7/24/2005, $Bill Luebkert wrote:
    >John Deighan wrote:

    At 02:20 PM 7/24/2005, Ed Chester wrote:
    >
    >
    >>John Deighan::
    >>

    Is there a safe way to compare 2 floating point numbers in Perl? [snip]
    My debugger says that they're both '630.24' [snip]
    However, the == test fails and the != test succeeds
    >>
    >>can you post code with the comparison == that fails ?
    >>if the debugger says they're the same, they're very very probably the same.
    >>are you sure the variable (or whatever) you're giving to == are
    >>really what you
    >>want them to be?
    >>there are lots of ways to compare numbers, right down to looping over

    the bits
    >>and logically
    >>XNRing them.

    >
    >

    Sorry about the lack of sample code, but I know that people who work
    with floating point numbers know about this problem, and I was
    wondering what the best solution was. Here is sample code with
    output. Note that I'm not formatting the output or rounding or
    anything - just printing out the contents of the 2 variables.

    my $sum1 = -237.15;
    my $sum2;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;
    $sum2 += -26.35;

    print("sum1 = $sum1\n");
    print("sum2 = $sum2\n");
    if ($sum1 == $sum2) {
    >
    >You could try something like:
    >

    if (abs ($sum1 - $sum2) < .0001) {

    print("EQUAL\n");
    }
    else {
    print("NT EQUAL\n");
    }

    UTPUT:

    sum1 = -237.15
    sum2 = -237.15
    NT EQUAL
    --

    Perl-Win32-Users mailing list
    Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    To unsubscribe:
    >
    >
    >


    ,-/- __ _ _ $Bill Luebkert Mailto:dbecoll (AT) adelphia (DOT) net
    (_/ / ) // // DBE Collectibles Mailto:dbe (AT) todbe (DOT) com
    / ) /--< o // // Castle of Medieval Myth & Magic
    http://www.todbe.com/
    >-/-' //_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
    >
    >Perl-Win32-Users mailing list
    >Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    >To unsubscribe:


    Perl-Win32-Users mailing list
    Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    To unsubscribe:
  • No.5 | | 2986 bytes | |

    Ken Barker graced perl with these words of wisdom:

    What kind of post is this?

    It was an informative help post, made especially informative and helpful
    by the fact that the relevant material was included at the relevant point
    in the code, as opposed to being top-posted.

    I do not see that anything was added at all.

    Then either your reading comprehension skills must not be so good, or
    you're so self-righteous about top-posting that you can't be bothered to
    look for the material of posters who insert material where it belongs. I
    can only wonder what sort of code this causes you to produce. :-)

    Give us all a break -
    don't bother - whatever your intentions.

    I'm certain that in future, $Bill won't bother responding to your pleas
    for help!

    At 06:35 PM 7/24/2005, $Bill Luebkert wrote:
    >>John Deighan wrote:


    Note that my quote accounts for one sign. This means that anything
    $Bill wrote will have two signs in front of it. If you scroll down,
    you'll see

    [much snippage]

    >my $sum1 = -237.15;
    >my $sum2;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >$sum2 += -26.35;
    >>

    >print("sum1 = $sum1\n");
    >print("sum2 = $sum2\n");
    >if ($sum1 == $sum2) {
    >>
    >>You could try something like:
    >>

    >if (abs ($sum1 - $sum2) < .0001) {


    Hmmm The four lines above all have two signs in front of them. They
    must have been written by $Bill!

    >>


    >,-/- __ _ _ $Bill Luebkert
    >Mailto:dbecoll (AT) adelphia (DOT) net
    >(_/ / ) // // DBE Collectibles Mailto:dbe (AT) todbe (DOT) com
    >/ ) /--< o // // Castle of Medieval Myth & Magic
    >http://www.todbe.com/

    /-' //_<_</_</_ http://dbecoll.tripod.com/ (My Perl/Lakers
    >>stuff)
    >>Perl-Win32-Users mailing list
    >>Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    >>To unsubscribe:


    Note that these lines form $Bill's signature, and also have two signs in
    front of them.

    Perl-Win32-Users mailing list
    Perl-Win32-Users (AT) listserv (DOT) ActiveState.com
    To unsubscribe:

    And as a service to everybody, note that these lines are appended by the
    fine folks at Active State to every post to this group. They tell the
    user how to unsubscribe from this fine mailing list, and show that one
    does not need to send an unsubscribe message to the entire group if one
    wishes to unsubscribe. :-)

Re: comparing floating point numbers


max 4000 letters.
Your nickname that display:
In order to stop the spam: 5 + 4 =
QUESTION ON "Windows"

EMSDN.COM