Skip to content

Commit

Permalink
- Added the Number.atan() and Number.atan2() methods;
Browse files Browse the repository at this point in the history
- Changed the behavior of the Math.atan(), Math.atan() and Math.asin() methods -- now they use internally `Math::Trig` instead of `Math::BigFloat`
  • Loading branch information
trizen committed Sep 10, 2015
1 parent ab705bc commit fba18cc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
29 changes: 3 additions & 26 deletions lib/Sidef/Math/Math.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,6 @@ package Sidef::Math::Math {

*PI = \π

sub atan {
my ($self, $x, $places) = @_;
Sidef::Types::Number::Number->new(
Math::BigFloat->new($x->get_value)->batan(defined($places) ? $places->get_value : ()));
}

sub atan2 {
my ($self, $x, $y, $places) = @_;
Sidef::Types::Number::Number->new(
Math::BigFloat->new($x->get_value)->batan2($y->get_value, defined($places) ? $places->get_value : ()));
}

sub cos {
my ($self, $x, $places) = @_;
Sidef::Types::Number::Number->new(
Expand All @@ -108,17 +96,6 @@ package Sidef::Math::Math {
Math::BigFloat->new($x->get_value)->bsin(defined($places) ? $places->get_value : ()));
}

sub asin {
my ($self, $x, $places) = @_;
$self->atan2(
$x,
$self->sqrt(
Sidef::Types::Number::Number->new(1)->subtract($self->pow($x, Sidef::Types::Number::Number->new(2)))
),
$places
);
}

sub log {
my ($self, $n, $base) = @_;
Sidef::Types::Number::Number->new(Math::BigFloat->new($n->get_value)->blog(defined($base) ? $base->get_value : ()));
Expand Down Expand Up @@ -322,12 +299,12 @@ package Sidef::Math::Math {

# The arcus (also known as the inverse) functions
# of the sine, cosine, and tangent
##'asin',
'asin',
'acos',
##'atan',
'atan',

# The principal value of the arc tangent of y/x
##'atan2',
'atan2',

# The arcus cofunctions of the sine, cosine, and tangent (acosec/acsc and
# acotan/acot are aliases). Note that atan2(0, 0) is not well-defined.
Expand Down
10 changes: 10 additions & 0 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ package Sidef::Types::Number::Number {
$self->new(CORE::sin($self->get_value));
}

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

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

sub log {
my ($self, $base) = @_;
$self->new($self->get_value->copy->blog(defined($base) ? $base->get_value : ()));
Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Number/NumberFast.pm
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ package Sidef::Types::Number::Number {
defined($base) ? $self->new($log / CORE::log($base->get_value)) : $self->new($log);
}

sub atan { ... }

sub ln {
my ($self) = @_;
$self->new(CORE::log($self->get_value));
Expand Down
4 changes: 2 additions & 2 deletions scripts/Expensive/mean_time.sf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#

func mean_angle(angles) {
Math.atan2(
atan2(
angles.map { Math.sin(_ * Math.PI / 180) }.sum / angles.len,
angles.map { Math.cos(_ * Math.PI / 180) }.sum / angles.len,
) * 180 / Math.PI;
Expand All @@ -27,7 +27,7 @@ func mean_time(times) {
deg2time(mean_angle(times.map {|t| time2deg(t)}));
}

mean_angle([10, 20, 30]) == 20 || die "math is broken!";
mean_angle([10, 20, 30]).fround(0) == 20 || die "math is broken!";
mean_time(["23:00:17", "23:40:20", "00:12:45", "00:17:19"]) == "23:47:43" || die "invalid return value";

say "** Test passed!";

0 comments on commit fba18cc

Please sign in to comment.