Skip to content

Instantly share code, notes, and snippets.

@nicdoye
Last active July 2, 2017 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicdoye/5d7ebcf7bb1dfc5a2667720922eb42c2 to your computer and use it in GitHub Desktop.
Save nicdoye/5d7ebcf7bb1dfc5a2667720922eb42c2 to your computer and use it in GitHub Desktop.
Perl Integer Division Module
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;
@nicdoye
Copy link
Author

nicdoye commented Jul 2, 2017

The check_int is removed in revision 3 as it is unnecessary, because of the Moose type constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment