CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Number Comparison

 



abs52
Novice

Mar 20, 2018, 7:34 AM

Post #1 of 11 (9474 views)
Number Comparison Can't Post

Hi All,

I have this code, but it seems that the output is strage


Code
my $hello = "384.99" ; 
my $hello2 = "384.128";
if ( $hello >= $hello2 ) {
print "$hello newer than or same as wanted $hello2, not doing anything\n";
}


But 384.128 is greater than 384.99, is it how perl handles the . numbers ?

Any ideas would be great

Thanks


ogit2
Novice

Mar 20, 2018, 8:06 AM

Post #2 of 11 (9472 views)
Re: [abs52] Number Comparison [In reply to] Can't Post

Hi

Are you trying to do a string comparison, string length comparison or number comparison? Can you confirm? In string comparisons lt, gt, eq, le, ge are used instead of symbols. However this will still gives the same result.

Also try this in Excel, and you will see the same result.



Code
#!C:\Perl\bin\perl.exe 

use strict;
use warnings;
my $hello = "384.99";
my $hello2 = "384.128";
if ( $hello ge $hello2 )
{
print "$hello newer than or same as wanted $hello2, not doing anything\n";
}



(This post was edited by ogit2 on Mar 20, 2018, 8:15 AM)


abs52
Novice

Mar 20, 2018, 8:15 AM

Post #3 of 11 (9468 views)
Re: [ogit2] Number Comparison [In reply to] Can't Post

Hi

I am trying to do string comparison
Using gt or ge still give the same result

Thanks


ogit2
Novice

Mar 20, 2018, 8:19 AM

Post #4 of 11 (9466 views)
Re: [abs52] Number Comparison [In reply to] Can't Post

Excel gives the same result as Perl. Where are you getting a different result?


abs52
Novice

Mar 20, 2018, 8:25 AM

Post #5 of 11 (9463 views)
Re: [ogit2] Number Comparison [In reply to] Can't Post

sorry, i only tired in perl , not excel

I get the same in excel


FishMonger
Veteran / Moderator

Mar 20, 2018, 9:34 AM

Post #6 of 11 (9456 views)
Re: [abs52] Number Comparison [In reply to] Can't Post

Why are you wanting to do string comparison on numbers?
Why aren't you doing numerical comparison?


abs52
Novice

Mar 20, 2018, 9:37 AM

Post #7 of 11 (9454 views)
Re: [FishMonger] Number Comparison [In reply to] Can't Post

TBH it not my code so i am open to suggestion,
It was written like it is above but i can change it, i just want it so that i can get the correct value


FishMonger
Veteran / Moderator

Mar 20, 2018, 10:16 AM

Post #8 of 11 (9448 views)
Re: [abs52] Number Comparison [In reply to] Can't Post

You can't test floating point numbers for equality. You need to convert them to integers then test.

Here is one method, there are other methods to do the comparison.

Code
#!/usr/bin/perl 

use warnings;
use strict;

my $hello = 384.99;
my $hello2 = 384.128;

if ( $hello * 1000 >= $hello2 * 1000) {
print "$hello newer than or same as wanted $hello2\n";
}
else {
print "$hello older than wanted $hello2\n";
}



(This post was edited by FishMonger on Mar 20, 2018, 10:17 AM)


BillKSmith
Veteran

Mar 21, 2018, 8:59 AM

Post #9 of 11 (9428 views)
Re: [FishMonger] Number Comparison [In reply to] Can't Post

I am unable to duplicate the original problem.

I offer the following example of a comparison which fails to get the expected result due to the limitations of floating point arithmetic.

Test 1 Demonstrates the problem.

Test 2 Demonstrates that multiplying each number by a floating point 1000 does not fix it.

Test 3 Demonstrates that multiplying each input by a decimal 1000 (converting them to integers) makes the addition and the comparison work as expected.



Code
C:\Users\Bill\forums\guru>type abs52.pl 
use strict;
use warnings;
{
my $hello = "384.99" ;
my $hello2 = "384.128";
if ( $hello >= $hello2 ) {
print "$hello newer than or same as wanted $hello2, not doing anything\n";

}
}

use Test::Simple tests=>3;
{
my $num1 = 19.08;
my $num2 = 2.01;
my $sum = $num1 + $num2;
my $expected = 21.09;
ok($sum >= $expected, "Raw floating point compare");
ok($sum*1000 >= $expected*1000, "Scale by 1000");
}

{
my $num1 = 19080;
my $num2 = 2010;
my $sum = $num1 + $num2;
my $expected = 21090;
ok($sum == $expected, "Pure integer")
}

C:\Users\Bill\forums\guru>perl abs52.pl
1..3
384.99 newer than or same as wanted 384.128, not doing anything
not ok 1 - Raw floating point compare
# Failed test 'Raw floating point compare'
# at abs52.pl line 17.
not ok 2 - Scale by 1000
# Failed test 'Scale by 1000'
# at abs52.pl line 18.
ok 3 - Pure integer
# Looks like you failed 2 tests of 3.

Good Luck,
Bill


FishMonger
Veteran / Moderator

Mar 21, 2018, 9:57 AM

Post #10 of 11 (9425 views)
Re: [BillKSmith] Number Comparison [In reply to] Can't Post

Your second test is flawed because it doesn't compare the same vars/values as the original.

I've added a new test to demonstrate.

Code
!/usr/bin/perl 

use warnings;
use strict;
use Test::Simple tests=>4;

{
my $hello = "384.99" ;
my $hello2 = "384.128";
if ( $hello >= $hello2 ) {
print "$hello newer than or same as wanted $hello2, not doing anything\n";

}
}


{
my $num1 = 19.08;
my $num2 = 2.01;
my $sum = $num1 + $num2;
my $expected = 21.09;
ok($sum >= $expected, "Raw floating point compare");
ok($sum*1000 >= $expected*1000, "Scale modified values by 1000");
}

{
my $num1 = 19.08;
my $num2 = 2.01;
ok($num1*1000 >= $num2*1000, "Scale by 1000");
}

{
my $num1 = 19080;
my $num2 = 2010;
my $sum = $num1 + $num2;
my $expected = 21090;
ok($sum == $expected, "Pure integer")
}


c:\test>compare.pl

Code
1..4 
384.99 newer than or same as wanted 384.128, not doing anything
not ok 1 - Raw floating point compare
# Failed test 'Raw floating point compare'
# at C:\test\Perl-2.pl line 22.
not ok 2 - Scale modified values by 1000
# Failed test 'Scale modified values by 1000'
# at C:\test\Perl-2.pl line 23.
ok 3 - Scale by 1000
ok 4 - Pure integer
# Looks like you failed 2 tests of 4.


NOTE:
The scale by number needs to meet or exceed the amount of decimal points required. We scaled by 1000 but could also dropped that to 100 since the test is only using 2 decimal places.


(This post was edited by FishMonger on Mar 21, 2018, 10:00 AM)


BillKSmith
Veteran

Mar 21, 2018, 1:08 PM

Post #11 of 11 (9414 views)
Re: [FishMonger] Number Comparison [In reply to] Can't Post

My test 1 was designed to demonstrate a surprising result. We usually expect the computer to get the same result that we would ourselves. In fact, it is almost never exactly the same. The error is extremely small, but in this case, the computed sum was slightly less than the actual sum. The 'failure' of the test is correct.

Failure of my Test 2 was intended to show that it was to late to fix the problem with scaling.

Success of my Test 3 is due to the fact that floating point arithmetic is exact for sufficiently small integers.

There is no surprise in your new test. If it failed to work with no scaling at all, I would be shopping for a new computer.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives