in reply to Re^6: &1 is no faster than %2 when checking for oddness. Oh well. (best)
in thread &1 is no faster than %2 when checking for oddness. Oh well.

To make it extremely simple, consider the code:

At last, a technical argument. For safety.

This

sub odd { die 'Out of integer range' if $_[ 0 ] & 0xffffffff != $_[ 0 ]; ## Should be done by perl! return $_[ 0 ] & 1; }

Versus this

sub odd { my $num= shift @ARGV; die 'Number too big to test for oddness' if $num > 9_007_199_254_740__992; die 'Number too small to test for oddness' if $num < -9_007_199_254_740__992; die 'Cannot test #INFinity for oddness' if $num eq '1.#INF'; die 'Cannot test negative #INFinity for oddness' if $num eq '-1.#INF'; die 'Cannot test #IND for oddness' if $num eq '1.#IND'; die 'Cannot test negative #IND for oddness' if $num eq '-1.#IND'; die 'Cannot test a non number for oddness' if $num eq '1.#QNAN; die 'Cannot test negative non number for oddness' if $num eq '-1.#QNAN; die 'Cannot test a non-integer for oddness' if int( $num ) != $num; return $num % 2 ? 1 : 0; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^8: &1 is no faster than %2 when checking for oddness. Oh well. (odd)
by tye (Sage) on Nov 17, 2006 at 11:59 UTC

    I don't know what I was thinking. Clearly, the simplicity of the error code is the best measure of how useful a subroutine is. So a better oddness tester would surely be:

    sub odd { return 1 if "1" eq $_[0]; die "Can't test the oddness of anything but '1'.\n"; }

    But then, neither of these are any more complicated than your first one:

    sub odd { my $num= shift @_; die "Out of range" if 2**52 <= abs($num); return $num % 2; } sub odd { my $num= shift @_; die "Out of range" if 1 != (1+$num)-$num; return $num % 2; }

    Update: For anyone who cares, I'm done. As is, this is a lot of noise for something pretty trivial and it is going nowhere slow.

    - tye        

    A reply falls below the community's threshold of quality. You may see it by logging in.