Skip to content

Commit

Permalink
- Changed the behavior of the // operator - it now means integer di…
Browse files Browse the repository at this point in the history
…vision.

Example:
	10 // 3 == 3

- Improved the +/- Infinity types.
- Added checks for some Number methods that require a positive number, such as fac(), fib(), lucas(), ...
- Added two more Number methods: `jacobi` and `kronecker`.
- Fixed the Number.legendre() method.
- Improved the Number.complex() and Number.i() methods -- no longer converts the number into a double.
  • Loading branch information
trizen committed Dec 27, 2015
1 parent 1a507bb commit d4199ad
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 77 deletions.
1 change: 1 addition & 0 deletions lib/Sidef/Types/Number/Complex.pm
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ package Sidef::Types::Number::Complex {

sub div {
my ($x, $y) = @_;

my $r = Math::MPC::Rmpc_init2($PREC);

if (ref($y) eq __PACKAGE__) {
Expand Down
151 changes: 120 additions & 31 deletions lib/Sidef/Types/Number/Inf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,23 @@ package Sidef::Types::Number::Inf {
ref($y) eq 'Sidef::Types::Number::Ninf' ? nan() : $x;
}

*iadd = \&add;

sub sub {
my ($x, $y) = @_;
ref($y) eq __PACKAGE__ ? nan() : $x;
}

*isub = \⊂

sub mul {
my ($x, $y) = @_;
ref($y) eq __PACKAGE__ and return $x;
$y->is_neg ? $x->neg : $x;
}

*imul = \&mul;

sub div {
my ($x, $y) = @_;
if (ref($y) eq __PACKAGE__ or ref($y) eq 'Sidef::Types::Number::Ninf') {
Expand All @@ -56,58 +62,128 @@ package Sidef::Types::Number::Inf {
$y->is_neg ? $x->neg : $x;
}

*idiv = \÷

sub is_pos {
state $x = Sidef::Types::Bool::Bool->true;
}

sub is_neg {
sub is_inf {
state $x = Sidef::Types::Bool::Bool->true;
}

sub is_ninf {
state $x = Sidef::Types::Bool::Bool->false;
}

*is_nan = \&is_ninf;
*is_neg = \&is_ninf;
*is_prime = \&is_ninf;
*is_square = \&is_ninf;
*is_sqr = \&is_ninf;
*is_power = \&is_ninf;
*is_pow = \&is_ninf;
*is_div = \&is_ninf;
*is_even = \&is_pos;
*is_odd = \&is_pos;

sub nan {
state $x = Sidef::Types::Number::Nan->new;
}

*mod = \&nan;
*fmod = \&nan;
*mod = \&nan;
*fmod = \&nan;
*bin = \&nan;
*modpow = \&nan;
*modinv = \&nan;
*and = \&nan;
*or = \&nan;
*xor = \&nan;
*legendre = \&nan;
*jacobi = \&nan;
*kronecker = \&nan;
*gcd = \&nan;
*lcm = \&nan;
*next_power2 = \&nan;
*next_pow2 = \&nan;
*next_pow = \&nan;
*next_power = \&nan;

sub as_bin {
state $x = Sidef::Types::String::String->new;
}

*as_oct = \&as_bin;
*as_hex = \&as_bin;

sub digits {
Sidef::Types::Array::Array->new;
}

sub divmod {
my ($x, $y) = @_;
($x->div($y), nan());
}

sub ninf {
state $x = Sidef::Types::Number::Ninf->new;
}

*neg = \&ninf;
*not = \&ninf;

sub min { $_[1] }
sub inf { $_[0] }

*max = \&inf;
*abs = \&inf;
*sqrt = \&inf;
*cbrt = \&inf;
*root = \&inf;
*sqr = \&inf;
*log = \&inf;
*log2 = \&inf;
*log10 = \&inf;
*exp = \&inf;
*exp2 = \&inf;
*exp10 = \&inf;
*sinh = \&inf;
*asinh = \&inf;
*cosh = \&inf;
*acosh = \&inf;
*tan = \&inf;
*sec = \&inf;
*csc = \&inf;
*cot = \&inf;
*hypot = \&inf;
*gamma = \&inf;
*lgamma = \&inf;
*digamma = \&inf;
*eint = \&inf;
*li2 = \&inf;
*inc = \&inf;
*dec = \&inf;
*max = \&inf;
*abs = \&inf;
*sqrt = \&inf;
*cbrt = \&inf;
*root = \&inf;
*sqr = \&inf;
*log = \&inf;
*log2 = \&inf;
*log10 = \&inf;
*exp = \&inf;
*exp2 = \&inf;
*exp10 = \&inf;
*sinh = \&inf;
*asinh = \&inf;
*cosh = \&inf;
*acosh = \&inf;
*tan = \&inf;
*sec = \&inf;
*csc = \&inf;
*cot = \&inf;
*hypot = \&inf;
*gamma = \&inf;
*lgamma = \&inf;
*digamma = \&inf;
*eint = \&inf;
*li2 = \&inf;
*inc = \&inf;
*dec = \&inf;
*int = \&inf;
*as_int = \&inf;
*float = \&inf;
*as_float = \&inf;
*rat = \&inf;
*length = \&inf;
*len = \&inf;
*size = \&inf;
*floor = \&inf;
*ceil = \&inf;
*factorial = \&inf;
*fac = \&inf;
*double_factorial = \&inf;
*dfac = \&inf;
*primorial = \&inf;
*fibonacci = \&inf;
*lucas = \&inf;
*shift_left = \&inf;
*shift_right = \&inf;
*rand_int = \&inf;
*rand = \&inf;

sub zero { $ZERO }

Expand All @@ -124,6 +200,8 @@ package Sidef::Types::Number::Inf {
*zeta = \&tanh;
*erf = \&tanh;

sub chr { state $x = Sidef::Types::String::String->new('') }

#
## asin(inf) = -inf*i
#
Expand Down Expand Up @@ -237,15 +315,26 @@ package Sidef::Types::Number::Inf {
*{__PACKAGE__ . '::' . '÷'} = \÷
*{__PACKAGE__ . '::' . '%'} = \&mod;
*{__PACKAGE__ . '::' . '**'} = \&pow;
*{__PACKAGE__ . '::' . '&'} = \∧
*{__PACKAGE__ . '::' . '|'} = \∨
*{__PACKAGE__ . '::' . '^'} = \&xor;
*{__PACKAGE__ . '::' . '++'} = \&inc;
*{__PACKAGE__ . '::' . '--'} = \&dec;
*{__PACKAGE__ . '::' . '=='} = \&eq;
*{__PACKAGE__ . '::' . '!='} = \≠
*{__PACKAGE__ . '::' . ''} = \≠
*{__PACKAGE__ . '::' . '>'} = \>
*{__PACKAGE__ . '::' . '>='} = \≥
*{__PACKAGE__ . '::' . ''} = \≥
*{__PACKAGE__ . '::' . '<'} = \&lt;
*{__PACKAGE__ . '::' . '<='} = \&le;
*{__PACKAGE__ . '::' . ''} = \&le;
*{__PACKAGE__ . '::' . '<=>'} = \&cmp;
*{__PACKAGE__ . '::' . '!'} = \&factorial;
*{__PACKAGE__ . '::' . '%%'} = \&is_div;
*{__PACKAGE__ . '::' . '>>'} = \&shift_right;
*{__PACKAGE__ . '::' . '<<'} = \&shift_left;
*{__PACKAGE__ . '::' . '//'} = \&div;
}
}

Expand Down
11 changes: 11 additions & 0 deletions lib/Sidef/Types/Number/Nan.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ package Sidef::Types::Number::Nan {

sub new { $NAN }

sub is_nan {
state $x = Sidef::Types::Bool::Bool->true;
}

sub is_inf {
state $x = Sidef::Types::Bool::Bool->false;
}

sub is_ninf {
state $x = Sidef::Types::Bool::Bool->false;
}
}

1
Loading

0 comments on commit d4199ad

Please sign in to comment.