# [Edinburgh-pm] Spot the bug

Wim Vanderbauwhede wim.vanderbauwhede at gmail.com
Sun Apr 28 12:14:39 PDT 2013

```Nice one!
It's the binding of the xor, the correct code is return
((\$self->true_count(\$idx) > 0) xor (\$self->false_count(\$idx) > 0));
return a xor b is equivalent to return(a) xor b.

Wim

On 28 April 2013 18:57, Miles Gould <miles at assyrian.org.uk> wrote:

> Hi all,
>
> I recently encountered some Perl behaviour that surprised me, and thought
> you might be amused and/or able to provide further insight.
>
> Here's the buggy function:
>
> sub is_unate {
>     my (\$self, \$idx) = @_;
>     return (\$self->true_count(\$idx) > 0) xor (\$self->false_count(\$idx) >
> 0);
> }
>
> and here are the tests which allowed me to diagnose the problem:
>
> is(\$cubes->true_count(1), 1);
> is(\$cubes->true_count(2), 1);
> is(\$cubes->true_count(3), 1);
> is(\$cubes->true_count(4), 1);
> is(\$cubes->true_count(5), 1);
>
> is(\$cubes->false_count(1), 1);
> is(\$cubes->false_count(2), 0);
> is(\$cubes->false_count(3), 1);
> is(\$cubes->false_count(4), 1);
> is(\$cubes->false_count(5), 0);
>
> ok(!\$cubes->is_unate(1));    # fails
> ok(\$cubes->is_unate(2));
> ok(!\$cubes->is_unate(3));    # fails
> ok(!\$cubes->is_unate(4));    # fails
> ok(\$cubes->is_unate(5));
>
> All tests pass apart from the ones marked "fails".
>
> So, my challenges:
>
> 1) Can you spot the bug in my code, and find a fix for it?
> 2) Can you explain why it makes sense for Perl to behave this way?
>
> Miles
> ______________________________**_________________
> Edinburgh-pm mailing list
> Edinburgh-pm at pm.org
> http://mail.pm.org/mailman/**listinfo/edinburgh-pm<http://mail.pm.org/mailman/listinfo/edinburgh-pm>
>

--
If it's pointless, what's the point?
If there is a point to it, what's the point?
(Tibor Fischer, "The Thought Gang")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/edinburgh-pm/attachments/20130428/aa4f935f/attachment.html>
```