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: Re: [BillKSmith] Number Comparison: Edit Log



FishMonger
Veteran / Moderator

Mar 21, 2018, 9:57 AM


Views: 9669
Re: [BillKSmith] Number Comparison

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)


Edit Log:
Post edited by FishMonger (Veteran) on Mar 21, 2018, 10:00 AM


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

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