Skip to content

Commit

Permalink
- Added the rational-division operator (//) which returns a Math::Big…
Browse files Browse the repository at this point in the history
…Rat objects

Example:
	say 1//3;	# prints: 1/3

The "//=" operator is also available:

	var x = 12;
	x //= 42;
	say x;		# prints: 2/7

- Minor optimizations inside the Number object.
  • Loading branch information
trizen committed Nov 14, 2015
1 parent dd57db2 commit 2eac143
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ package Sidef::Deparse::Perl {
'Sidef::Types::Block::For' => 1,
},

reassign_ops => {map (("$_=" => $_), qw(+ - % * / & | ^ ** && || << >> ÷))},
reassign_ops => {map (("$_=" => $_), qw(+ - % * // / & | ^ ** && || << >> ÷))},

inc_dec_ops => {
'++' => 'inc',
Expand Down
1 change: 1 addition & 0 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ package Sidef::Parser {
++ --
+= +
-= -
//= //
/= / ÷= ÷
**= **
%= %
Expand Down
60 changes: 33 additions & 27 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ package Sidef::Types::Number::Number {

sub factorial {
my ($self) = @_;
$self->new($$self->copy->bfac);
$self->new(scalar $$self->copy->bfac);
}

*fac = \&factorial;
Expand Down Expand Up @@ -260,12 +260,12 @@ package Sidef::Types::Number::Number {

sub sqrt {
my ($self) = @_;
$self->new($$self->copy->bsqrt);
$self->new(scalar $$self->copy->bsqrt);
}

sub root {
my ($self, $n) = @_;
$self->new($$self->copy->broot($n->get_value));
$self->new(scalar $$self->copy->broot($n->get_value));
}

sub troot {
Expand All @@ -279,7 +279,7 @@ package Sidef::Types::Number::Number {

sub abs {
my ($self) = @_;
$self->new($$self->copy->babs);
$self->new(scalar $$self->copy->babs);
}

*pos = \&abs;
Expand Down Expand Up @@ -308,7 +308,7 @@ package Sidef::Types::Number::Number {

sub exp {
my ($self) = @_;
$self->new($$self->copy->bexp);
$self->new(scalar $$self->copy->bexp);
}

sub int {
Expand All @@ -320,57 +320,55 @@ package Sidef::Types::Number::Number {
*to_i = \&int;

sub max {
my ($self, $num) = @_;
my ($x, $y) = ($$self, $num->get_value);
$self->new($x > $y ? $x : $y);
my ($self, $arg) = @_;
$$self->bcmp($arg->get_value) >= 0 ? $self : $arg;
}

sub min {
my ($self, $num) = @_;
my ($x, $y) = ($$self, $num->get_value);
$self->new($x < $y ? $x : $y);
my ($self, $arg) = @_;
$$self->bcmp($arg->get_value) <= 0 ? $self : $arg;
}

sub cos {
my ($self) = @_;
$self->new($$self->copy->bcos);
$self->new(scalar $$self->copy->bcos);
}

sub sin {
my ($self) = @_;
$self->new($$self->copy->bsin);
$self->new(scalar $$self->copy->bsin);
}

sub atan {
my ($x) = @_;
Sidef::Types::Number::Number->new($x->get_value->copy->batan);
my ($self) = @_;
$self->new(scalar $$self->copy->batan);
}

sub atan2 {
my ($x, $y) = @_;
Sidef::Types::Number::Number->new($x->get_value->copy->batan2($y->get_value));
my ($self, $y) = @_;
$self->new(scalar $$self->copy->batan2($y->get_value));
}

sub log {
my ($self, $base) = @_;
$self->new($$self->copy->blog(defined($base) ? $base->get_value : ()));
$self->new(scalar $$self->copy->blog(defined($base) ? $base->get_value : ()));
}

sub ln {
my ($self) = @_;
$self->new($$self->copy->blog);
$self->new(scalar $$self->copy->blog);
}

sub log10 {
my ($self) = @_;
state $ten = Math::BigFloat->new(10);
$self->new($$self->copy->blog($ten));
$self->new(scalar $$self->copy->blog($ten));
}

sub log2 {
my ($self) = @_;
state $two = Math::BigFloat->new(2);
$self->new($$self->copy->blog($two));
$self->new(scalar $$self->copy->blog($two));
}

sub inf {
Expand All @@ -380,14 +378,14 @@ package Sidef::Types::Number::Number {

sub neg {
my ($self) = @_;
$self->new($$self->copy->bneg);
$self->new(scalar $$self->copy->bneg);
}

*negate = \&neg;

sub not {
my ($self) = @_;
$self->new($$self->copy->bnot);
$self->new(scalar $$self->copy->bnot);
}

sub sign {
Expand Down Expand Up @@ -483,12 +481,12 @@ package Sidef::Types::Number::Number {

sub ceil {
my ($self) = @_;
$self->new($$self->copy->bceil);
$self->new(scalar $$self->copy->bceil);
}

sub floor {
my ($self) = @_;
$self->new($$self->copy->bfloor);
$self->new(scalar $$self->copy->bfloor);
}

sub round {
Expand Down Expand Up @@ -524,7 +522,7 @@ package Sidef::Types::Number::Number {

sub digit {
my ($self, $n) = @_;
$self->new($$self->as_int->digit($n->get_value));
$self->new(scalar $$self->as_int->digit($n->get_value));
}

sub digits {
Expand All @@ -542,7 +540,7 @@ package Sidef::Types::Number::Number {

sub nok {
my ($self, $k) = @_;
$self->new($$self->as_int->bnok($k->get_value));
$self->new(scalar $$self->as_int->bnok($k->get_value));
}

*binomial = \&nok;
Expand Down Expand Up @@ -647,6 +645,13 @@ package Sidef::Types::Number::Number {

*nude = \&parts;

sub rdiv {
my ($self, $arg) = @_;
$self->new(scalar Math::BigRat->new($$self)->bdiv($$arg));
}

*rat_div = \&rdiv;

sub as_float {
my ($self) = @_;
$self->new($$self->as_float);
Expand Down Expand Up @@ -722,6 +727,7 @@ package Sidef::Types::Number::Number {
*{__PACKAGE__ . '::' . '<<'} = \&shift_left;
*{__PACKAGE__ . '::' . '~'} = \&not;
*{__PACKAGE__ . '::' . ':'} = \&complex;
*{__PACKAGE__ . '::' . '//'} = \&rdiv;
}
};

Expand Down

0 comments on commit 2eac143

Please sign in to comment.