Last active
July 2, 2017 19:51
-
-
Save nicdoye/5d7ebcf7bb1dfc5a2667720922eb42c2 to your computer and use it in GitHub Desktop.
Perl Integer Division Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Math::IntegerDivision; | |
use Carp; | |
use Moose; | |
has 'numerator' => ( | |
is => 'rw', | |
isa => 'Int' | |
); | |
has 'denominator' => ( | |
is => 'rw', | |
isa => 'Int' | |
); | |
sub divide { | |
my ( $self ) = @_; | |
unless( $self->denominator ) { | |
carp("Division by Zero"); | |
return; | |
} | |
my $answer = ( $self->numerator - $self->numerator % $self->denominator ) / $self->denominator; | |
# When only one is negative, the result is out by one | |
# when it's not a perfect divisor. | |
return (( $self->numerator < 0 ^ $self->denominator < 0 ) && ( $self->numerator % $self->denominator )) ? | |
$answer + 1 : | |
$answer; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
check_int
is removed in revision 3 as it is unnecessary, because of theMoose
type constraints.