comparing decimal numbers

Discussion in 'Perl Misc' started by Thens, Apr 30, 2004.

  1. Thens

    Thens Guest

    Hi,

    I have to compare two decimals numbers 1.5.0 and 2.1.1. The trouble is they originate from string. Hence I get warnings that say
    " Argument "1.5.0" isn't numeric in numeric ge" ...

    This is my code;

    ....
    ( $rel1, $rel2 ) = ( 'r1.5.0', 'r2.1.1' );
    s/^r// for ( $rel1, $rel2 );
    return $rel1 > $rel2 ? 1 : 0

    Please let me know if Iam missing something here.

    For the moment I did this to overcome the error message.

    ....
    ( $rel1, $rel2 ) = ( 'r1.5.0', 'r2.1.1' );
    s/^r// for ( $rel1, $rel2 );
    $rel1++; $rel2++; # to avoid the non numeric warnings
    return $rel1 >= $rel2 ? 1 : 0

    Thanks in advance for your help.

    Regards,
    Thens.
     
    Thens, Apr 30, 2004
    #1
    1. Advertising

  2. In article <20040430235408.43bdb167@asiclindt001>, Thens wrote:
    > Hi,
    >
    > I have to compare two decimals numbers 1.5.0 and 2.1.1. The trouble is they
    > originate from string. Hence I get warnings that say " Argument "1.5.0" isn't
    > numeric in numeric ge" ...


    No, those are strings - a decimal number does not contain more than 1 decimal
    point.

    >
    > This is my code;
    >
    > ... ( $rel1, $rel2 ) = ( 'r1.5.0', 'r2.1.1' ); s/^r// for ( $rel1, $rel2 );
    > return $rel1 > $rel2 ? 1 : 0
    >
    > Please let me know if Iam missing something here.
    >
    > For the moment I did this to overcome the error message.
    >
    > ... ( $rel1, $rel2 ) = ( 'r1.5.0', 'r2.1.1' ); s/^r// for ( $rel1, $rel2 );
    > $rel1++; $rel2++; # to avoid the non numeric warnings return $rel1 >= $rel2
    > ? 1 : 0
    >


    And what do the values of $rel1 and $rel2 end up being? Running a short bit of
    test code:

    ( $rel1, $rel2 ) = ( 'r1.5.0', 'r2.1.1' );
    s/^r// for ( $rel1, $rel2 );
    $rel1++; $rel2++; # to avoid the non numeric warnings
    print "rel1: $rel1 rel2: $rel2\n";

    rel1: 2.5 rel2: 3.1

    So, I seriously doubt this is remotely close to what you want to do. You need
    further comparison logic to compare each part.

    This seems to work for me:

    print "higher: " . rev_val('r1.5.0', 'r2.1.1') . "\n";
    print "higher: " . rev_val('r2.5.0', 'r2.1.1') . "\n";
    print "higher: " . rev_val('r1.99.0', 'r2.1.1') . "\n";
    print "higher: " . rev_val('r2.1.0', 'r2.1.1') . "\n";
    print "higher: " . rev_val('r2.1.0', 'r2.1.0') . "\n";

    sub rev_val
    {
    my ($r1, $r2) = @_;
    $r1 =~ s/^r//g;
    $r2 =~ s/^r//g;

    print "$r1, $r2: ";
    my (@v1) = split(/\./, $r1);
    my (@v2) = split(/\./, $r2);

    my $max = ($#v1 > $#v1) ? $#v1 : $#v2;

    $cnt = 0;
    while ($cnt < $max)
    {
    if ($v1[$cnt] == $v2[$cnt])
    {
    $cnt++;
    next
    }

    return ($v1[$cnt] > $v2[$cnt]) ? $r1 : $r2;
    }

    return "equal";
    }

    Output:
    1.5.0, 2.1.1: higher: 2.1.1
    2.5.0, 2.1.1: higher: 2.5.0
    1.99.0, 2.1.1: higher: 2.1.1
    2.1.0, 2.1.1: higher: equal
    2.1.0, 2.1.0: higher: equal

    Kevin
     
    Kevin Collins, Apr 30, 2004
    #2
    1. Advertising

  3. Thens <> wrote:

    > I have to compare two decimals numbers 1.5.0 and 2.1.1. The
    > trouble is they originate from string. Hence I get warnings that
    > say " Argument "1.5.0" isn't numeric in numeric ge" ...


    Those do not look like numbers to me, nor apparently do they to perl.
    They look like sequences of numbers separated by periods.

    If you remove the periods and change them to 150 and 211, will those
    numbers compare the way you want? If so ...

    > This is my code;
    >
    > ...
    > ( $rel1, $rel2 ) = ( 'r1.5.0', 'r2.1.1' );
    > s/^r// for ( $rel1, $rel2 );


    .... you could change the line above to

    tr/0-9//dc for ($rel1, $rel2);

    > return $rel1 > $rel2 ? 1 : 0
    >
    > Please let me know if Iam missing something here.
    >
    > For the moment I did this to overcome the error message.
    >
    > ...
    > ( $rel1, $rel2 ) = ( 'r1.5.0', 'r2.1.1' );
    > s/^r// for ( $rel1, $rel2 );
    > $rel1++; $rel2++; # to avoid the non numeric warnings


    Try printing out the value of $rel1 and $rel here. Is that really
    what you wanted to happen?

    > return $rel1 >= $rel2 ? 1 : 0
     
    David K. Wall, Apr 30, 2004
    #3
  4. Thens <> wrote:

    > I have to compare two decimals numbers 1.5.0 and 2.1.1.



    Those are not decimal numbers...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 30, 2004
    #4
  5. Tad McClellan wrote:

    > Thens <> wrote:
    >>I have to compare two decimals numbers 1.5.0 and 2.1.1.

    > Those are not decimal numbers...


    brave, very brave :)
     
    Mark Clements, May 1, 2004
    #5
  6. Thens

    Anno Siegel Guest

    Mark Clements <> wrote in comp.lang.perl.misc:
    > Tad McClellan wrote:
    >
    > > Thens <> wrote:
    > >>I have to compare two decimals numbers 1.5.0 and 2.1.1.

    > > Those are not decimal numbers...

    >
    > brave, very brave :)


    Indeed!

    It is true that "Dewey Decimal" notation refers to exactly this kind of
    string. But numbers they aren't. Sufficiently new Perl's could compare
    them as version strings, but I'm not sure if that's a good suggestion.

    Anno
     
    Anno Siegel, May 1, 2004
    #6
  7. Tad McClellan <> wrote:

    > Thens <> wrote:
    >
    >> I have to compare two decimals numbers 1.5.0 and 2.1.1.

    >
    >
    > Those are not decimal numbers...


    They don't look like whole numbers, either.
     
    David K. Wall, May 1, 2004
    #7
  8. David K. Wall wrote:
    > Tad McClellan <> wrote:
    >
    >> Thens <> wrote:
    >>
    >>> I have to compare two decimals numbers 1.5.0 and 2.1.1.

    >>
    >>
    >> Those are not decimal numbers...

    >
    > They don't look like whole numbers, either.


    "These aren't green cars. "
    "They don't look like a red cars, either."

    Surprise, surprise, it's a submarine.

    jue
    jue
     
    Jürgen Exner, May 1, 2004
    #8
  9. "Jürgen Exner" <> wrote:

    > David K. Wall wrote:
    >> Tad McClellan <> wrote:
    >>
    >>> Thens <> wrote:
    >>>
    >>>> I have to compare two decimals numbers 1.5.0 and 2.1.1.
    >>>
    >>>
    >>> Those are not decimal numbers...

    >>
    >> They don't look like whole numbers, either.

    >
    > "These aren't green cars. "
    > "They don't look like a red cars, either."
    >
    > Surprise, surprise, it's a submarine.


    Just as long as it's not a flame-breathing monster screaming "substr()!"
     
    David K. Wall, May 1, 2004
    #9
  10. Thens

    Thens Guest

    On Fri, 30 Apr 2004 19:15:19 GMT
    (Kevin Collins) wrote:

    > In article <20040430235408.43bdb167@asiclindt001>, Thens wrote:
    > > Hi,
    > >
    > > I have to compare two decimals numbers 1.5.0 and 2.1.1. The trouble is they
    > > originate from string. Hence I get warnings that say " Argument "1.5.0" isn't
    > > numeric in numeric ge" ...

    >
    > No, those are strings - a decimal number does not contain more than 1 decimal
    > point.


    Thanks to all those who corrected me. I figured it out now. Effect of late night coding :(

    Regards,
    Thens.
     
    Thens, May 2, 2004
    #10
    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. Ven
    Replies:
    3
    Views:
    1,328
  2. Gilbert Fine
    Replies:
    8
    Views:
    904
    Zentrader
    Aug 1, 2007
  3. Vitaliy
    Replies:
    1
    Views:
    476
    Peter Otten
    May 29, 2008
  4. D'Arcy J.M. Cain

    Comparing float and decimal

    D'Arcy J.M. Cain, Sep 23, 2008, in forum: Python
    Replies:
    16
    Views:
    496
  5. Terry Reedy

    Re: Comparing float and decimal

    Terry Reedy, Sep 23, 2008, in forum: Python
    Replies:
    5
    Views:
    408
    Terry Reedy
    Sep 26, 2008
Loading...

Share This Page