comparing floating point numbers

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default comparing floating point numbers

    Hi,

    I have a problem comparing floating point numbers. After a lot of googling and docs I found the issue to be because of the internal floating point representation.

    Here is a sample program that explains that

    #!/usr/local/bin/perl

    use strict;
    use warnings;

    my ( $num, $num1, $num2 );

    $num = 1.8;
    $num1 = ( $num * (1 + (10/100) ) ); # $num + 10% $num => 1.98

    print "Number :", sprintf "%2.20f \n" , $num;
    print "Number +10% :", sprintf "%2.20f \n", $num1;

    $num2 = 1.98;
    print "Number +10% :", sprintf "%30.20f \n", $num2;

    if ( $num1 != $num2 ) {
    print "FIRST PASS >> $num1 is not equal to $num2 !! \n";
    }
    if ( ($num1+0.1) != ($num2+.1) ) {
    print "SECOND PASS >> $num1 is not eqaul to $num2 !!\n";
    }


    This prints

    FIRST PASS >> 1.98 is not equal to 1.98 !!


    I understand this is because of the floating point representation. Then I tried to solve it by adding 0.1 at both the ends and then comparing the numbers. THis seems to have solved the problem. My question is, Is this a reliable solution.

    While searching the group archive, I found this can be achieved by checking for a small delta like

    $delta = 0.0001

    if ( abs ( $num1 - $num2 ) < $delta ) {
    # Now they are eual
    }
    ...


    I wanted to know under what circumstances may adding 0.1 at both ends can fail.

    Thanks and Regards,
    Thens.

    Thens Guest

  2. Similar Questions and Discussions

    1. Comparing structures, ignoring floating point roundoff differences
      I've got a problem where I need to compare structures that have floating point numbers stashed in them. I want to test the structures for identity...
    2. Random Floating point numbers
      I am trying to write a function that will gerenerate a random number that is of float value. Now I need it to generate between two values like so: ...
    3. calculator, digits behind floating point
      Dear forum users, i got an calculator doing all kinds of things with some values users past in text fields. In an other textfield i get the...
    4. Floating Point precision madness
      Hi All, I have the rather fun task of converting IEEE 32 bit floating Hex into decimal. From what I can tell Director has limited base handling...
    5. From floating point to fraction
      Hi I'd like to, from inside my Perl script, go from a floating point number, like 0.33333333333333333, to a fraction, in this case 1/3. Is that...
  3. #2

    Default Re: comparing floating point numbers


    "Thens" <thens@nospam.com> wrote in message
    news:20030710101239.5a111e91.thens@nospam.com...
    > Hi,
    >
    > I have a problem comparing floating point numbers. After a lot of
    googling and docs I found the issue to be because of the internal floating
    point representation.
    >
    [snipped]

    Here is what I use to compare two floating numbers.

    until ( equal( $tmp_rise_2, $tmp_rise_3, 8 ) ) {
    do stuff....
    }

    sub equal {

    #
    #
    # FUNCTIONAL SEQUENCE for equal
    #
    # _GIVEN
    #
    # Two floating point numbers and Accuracy
    #
    # _THEN
    #
    # Use sprintf to format the numbers to Accuracy
    # number of decimal places
    #
    # _RETURN
    #
    # True if the numbers are equal
    #
    my ( $A, $B, $dp ) = @_;

    return sprintf( "%.${dp}g", $A ) eq sprintf( "%.${dp}g", $B );
    }


    hope this helps

    Mothra


    Mothra Guest

  4. #3

    Default Re: comparing floating point numbers

    In article <20030710101239.5a111e91.thens@nospam.com>,
    Thens <thens@nospam.com> wrote:

    : [...]
    :
    : While searching the group archive, I found this can be achieved by
    : checking for a small delta like
    :
    : $delta = 0.0001
    :
    : if ( abs ( $num1 - $num2 ) < $delta ) {
    : # Now they are eual
    : }
    :
    : I wanted to know under what circumstances may adding 0.1 at both
    : ends can fail.

    That depends on the nature of your computation. Remember that computers
    use finite-precision arithmetic, the way you structure computations can
    hurt you, e.g., dividing by a very small number or subtracting numbers
    close values can produce serious rounding errors.

    See "What Every Computer Scientist Should Know About Floating-Point
    Arithmetic" at

    [url]http://docs.sun.com/source/806-3568/ncg_goldberg.html[/url]

    Even checking for membership in some epsilon-neighborhood isn't
    always sufficient:

    Incidentally, some people think that the solution to such
    anomalies is never to compare floating-point numbers for
    equality, but instead to consider them equal if they are within
    some error bound E. This is hardly a cure-all because it raises
    as many questions as it answers. What should the value of E be?
    If x < 0 and y > 0 are within E, should they really be
    considered to be equal, even though they have different signs?
    Furthermore, the relation defined by this rule,

    a ~ b <=> |a - b| < E

    is not an equivalence relation because a ~ b and b ~ c does
    not imply that a ~ c.

    ibid.

    Greg
    --
    While the federal government is frustrating when it treats economic
    problems with nonchalance, it is terrifying when it gets involved.
    -- Bob Novak
    Greg Bacon Guest

  5. #4

    Default Re: comparing floating point numbers

    Thens wrote:
    >
    > Hi,
    >
    > I have a problem comparing floating point numbers.
    One of the things you need to learn about floating point
    numbers is:
    NEVER compare them for equality (or inequality).


    Mike
    Michael P. Broida Guest

  6. #5

    Default Re: comparing floating point numbers

    Adding this worked for me.

    $num1 = "$num1";
    $num2 = "$num2";
    $num1 = $num1 * 1;
    $num2 = $num2 * 1;

    This seems to "reset" Perl's floating point value and lets you compare correctly.

    webmaster
    taketheheavenquiz.com
    Unregistered Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139