My first attempt at the overloading was crap.
I think that this is an improvement. By overloading the 'cmp' operator only, overload should (and appears to) use it to autogenerate appropriate overloads for 'eq' & 'ne'.
#! perl -slw use warnings; use strict; use Data::Dumper; use Algorithm::Diff qw( diff ); { package myFloats; use overload 'cmp' => \&cmp, '""' => \&stringy; sub new{ return bless \$_[ 1 ], $_[ 0 ] } sub cmp{ printf "$_[ 0 ] cmp $_[ 1 ] : "; my $rv = abs( ${ $_[ 0 ] } - ${ $_[ 1 ] } ); printf "(%32.32f) : %d\n", $rv, !($rv <= 0.15)||0; return ! ( $rv <= 0.15 ); } sub stringy{ ${ $_[ 0 ] } } } my @a = map{ myFloats->new( $_ ) } qw( 1.0 2.1 3.2 4.0 4.9 5.8 ); my @b = map{ myFloats->new( $_ ) } qw( 1.0 2.0 3.0 4.0 5.0 6.0 ); my @d = diff( \@a, \@b ); print "\n@a\n@b\n"; print "@$_" for map{ @$_ } @d;
However, even having re-read the A::D docs to understand the format of the output, I still do not understand the results I am getting?
P:\test>377814 1.0 cmp 1.0 : (0.00000000000000000000000000000000) : 0 2.1 cmp 2.0 : (0.10000000000000009000000000000000) : 0 3.2 cmp 3.0 : (0.20000000000000018000000000000000) : 1 5.8 cmp 6.0 : (0.20000000000000018000000000000000) : 1 1.0 2.1 3.2 4.0 4.9 5.8 1.0 2.0 3.0 4.0 5.0 6.0 - 2 3.2 + 2 3.0 - 4 4.9 + 4 5.0 - 5 5.8 + 5 6.0
When called, the overloaded 'cmp' operator seems to be returning the appropriate results and duly, the output reflects this by not replacing 2.1 with 2.0. However, it then goes on to replace 4.9 with 5.0 which is strange as it never seems to actually compare these two values.
I'm not sure if this represents a bug in A::D, overload or my use of one, the other or both? It's a mystery, but I enjoy those? I'll update if I track it down.
Update: Indeed, there does appear to be a bug in cpan:Algorithm::Diff. Moving to using Algorithm::DiffOld renders the following results which is much more what I would have expected.
1:24:24.44 C:\Perl\test>377814 1.0 cmp 1.0 : (0.00000000000000000000000000000000) : 0 2.1 cmp 2.0 : (0.10000000000000009000000000000000) : 0 3.2 cmp 3.0 : (0.20000000000000018000000000000000) : 1 5.8 cmp 6.0 : (0.20000000000000018000000000000000) : 1 3.2 cmp 6.0 : (2.79999999999999980000000000000000) : 1 3.2 cmp 5.0 : (1.79999999999999980000000000000000) : 1 3.2 cmp 4.0 : (0.79999999999999982000000000000000) : 1 3.2 cmp 3.0 : (0.20000000000000018000000000000000) : 1 4.0 cmp 6.0 : (2.00000000000000000000000000000000) : 1 4.0 cmp 5.0 : (1.00000000000000000000000000000000) : 1 4.0 cmp 4.0 : (0.00000000000000000000000000000000) : 0 4.0 cmp 3.0 : (1.00000000000000000000000000000000) : 1 4.9 cmp 6.0 : (1.09999999999999960000000000000000) : 1 4.9 cmp 5.0 : (0.09999999999999964500000000000000) : 0 4.9 cmp 4.0 : (0.90000000000000036000000000000000) : 1 4.9 cmp 3.0 : (1.90000000000000040000000000000000) : 1 5.8 cmp 6.0 : (0.20000000000000018000000000000000) : 1 5.8 cmp 5.0 : (0.79999999999999982000000000000000) : 1 5.8 cmp 4.0 : (1.79999999999999980000000000000000) : 1 5.8 cmp 3.0 : (2.79999999999999980000000000000000) : 1 1.0 2.1 3.2 4.0 4.9 5.8 1.0 2.0 3.0 4.0 5.0 6.0 - 2 3.2 + 2 3.0 - 5 5.8 + 5 6.0
In reply to Re: using Algorithm::Diff for floats (Updated)
by BrowserUk
in thread using Algorithm::Diff for floats
by jim_neophyte
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |