Skip to content

Commit

Permalink
- Changed the behavior of the Math.map() method. Now it maps a given …
Browse files Browse the repository at this point in the history
…value from one range to another.

Example:
	# map the value 300 from range 0-600 to range 0-256
	say Math.map(300, 0, 600, 0, 256);	# 128

- The old Math.map() method is now available as Math.map_range()

Example:
	# map 10 values inside the range from 0 to 1
	say Math.map_range(10, 0, 1).to_a;

- Updated .pod files
  • Loading branch information
trizen committed Sep 5, 2015
1 parent 1f70d8b commit ecc1e67
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
28 changes: 19 additions & 9 deletions lib/Sidef/Math/Math.pm
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,31 @@ package Sidef::Math::Math {
*rangeSum = \&range_sum;

sub map {
my ($self, $value, $in_min, $in_max, $out_min, $out_max) = @_;

$value = $value->get_value;

$in_min = $in_min->get_value;
$in_max = $in_max->get_value;

$out_min = $out_min->get_value;
$out_max = $out_max->get_value;

Sidef::Types::Number::Number->new(($value - $in_min) * ($out_max - $out_min) / ($in_max - $in_min) + $out_min);
}

sub map_range {
my ($self, $amount, $from, $to) = @_;

$amount = $amount->get_value;
$from = $from->get_value;
$to = $to->get_value;

my $step = ($to - $from) / $amount;
$step == 0 && return (Sidef::Types::Array::Array->new());

my @values;
for (my $i = $from ; $i <= $to ; $i += $step) {
push @values, Sidef::Types::Number::Number->new($i);
}

Sidef::Types::Array::Array->new(@values);
Sidef::Types::Array::RangeNumber->new(
from => $from,
to => $to,
step => ($to - $from) / $amount,
);
}

sub number_to_percentage {
Expand Down
13 changes: 8 additions & 5 deletions lib/Sidef/Math/Math.pod
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,17 @@ Return the

=head2 map

Math.map(I<amount>, I<from>, I<to>) -> I<Array>
Math.map() -> I<Obj>

This function returns an I<amount> of numbers mapped between I<from> and I<to>,
each number having the same distance between each other.
Return the

=cut

Math.map(8, 1, 3); # returns: [1, 1.25, 1.5, 1.75, 2, 2.25, 2.5, 2.75]
=head2 map_range

Returns an object of type: C<Sidef::Types::Array::Array>
Math.map_range() -> I<Obj>

Return the

=cut

Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Types/Array/RangeNumber.pod
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ Return the

=cut

=head2 reverse

RangeNumber.reverse() -> I<Obj>

Return the

=cut

=head2 step

RangeNumber.step() -> I<Obj>
Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Types/Array/RangeString.pod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ Return the

=cut

=head2 reverse

RangeString.reverse() -> I<Obj>

Return the

=cut

=head2 to_a

RangeString.to_a() -> I<Obj>
Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Number/Complex.pod
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ Complex.nok() -> I<Obj>

Return the

Aliases: I<binomial>

=cut

=head2 not
Expand Down
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Number/Number.pod
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ Number.nok() -> I<Obj>

Return the

Aliases: I<binomial>

=cut

=head2 npow
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/String/String.pod
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ String.to() -> I<Obj>

Return the

Aliases: I<...()>, I<..^()>, I<upto>, I<upTo>
Aliases: I<...()>, I<..^()>, I<upto>, I<upTo>, I<range>

=cut

Expand Down

0 comments on commit ecc1e67

Please sign in to comment.