Skip to content

Commit

Permalink
- Added the <~> approximative comparison operator (alias for `appro…
Browse files Browse the repository at this point in the history
…x_cmp`).

Example (solution to `x` for x**3 = 10):

	say bsearch_inverse(100, {|x| x**3 <~> 10 })	#=> 2.15443469003188372...
  • Loading branch information
trizen committed Dec 21, 2019
1 parent 972ef55 commit 7f9680f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ package Sidef::Parser {
%% ≅
~~ !~
<~>
<=> =~=
<<= >>=
<< >>
Expand Down
6 changes: 6 additions & 0 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9368,6 +9368,9 @@ package Sidef::Types::Number::Number {
$max = 11465;
}

require Memoize;
Memoize::memoize('_prime_count');

my $k = 0;

while (1) {
Expand All @@ -9387,6 +9390,8 @@ package Sidef::Types::Number::Number {
}
}

Memoize::unmemoize('_prime_count');

while (!Math::Prime::Util::GMP::is_semiprime($k)) {
--$k;
}
Expand Down Expand Up @@ -13810,6 +13815,7 @@ package Sidef::Types::Number::Number {
*{__PACKAGE__ . '::' . 'δ'} = \&kronecker_delta;
*{__PACKAGE__ . '::' . '=~='} = \&approx_eq;
*{__PACKAGE__ . '::' . ''} = \&approx_eq;
*{__PACKAGE__ . '::' . '<~>'} = \&approx_cmp;
}
}

Expand Down
30 changes: 15 additions & 15 deletions lib/Sidef/Types/Number/Number.pod
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ Aliases: I<cmp>

=cut

=head2 approx_cmp

approx_cmp(a, b)
approx_cmp(a, b, k)

Approximate comparison of C<a> and C<b>.

Equivalent with:

a.round(k) <=> b.round(k)

When C<k> is omitted, it uses the default floating-point precision to deduce C<k>.

=cut

=head2 ==

a == b
Expand Down Expand Up @@ -564,21 +579,6 @@ Return the

=cut

=head2 approx_cmp

approx_cmp(a, b)
approx_cmp(a, b, k)

Approximate comparison of C<a> and C<b>.

Equivalent with:

a.round(k) <=> b.round(k)

When C<k> is omitted, it uses the default floating-point precision to deduce C<k>.

=cut

=head2 approx_ge

approx_ge(a, b)
Expand Down

0 comments on commit 7f9680f

Please sign in to comment.