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

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

In reply to Re: using Algorithm::Diff for floats (Updated) by BrowserUk
in thread using Algorithm::Diff for floats by jim_neophyte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.