comparing decimal numbers

T

Thens

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.
 
K

Kevin Collins

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
 
D

David K. Wall

Thens said:
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?
 
A

Anno Siegel

Mark Clements said:
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
 
J

Jürgen Exner

David said:
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
 
D

David K. Wall

Jürgen Exner said:
"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()!"
 
T

Thens

On Fri, 30 Apr 2004 19:15:19 GMT
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top