-
Notifications
You must be signed in to change notification settings - Fork 577
[PATCH] Add sign function bsgn() as a complement to babs(). #11165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
From @pjacklamThis is the standard mathematical signum function. It sets the Documentation and tests are included. dist/Math-BigInt/lib/Math/BigInt.pm | 13 +++++++++++++ Inline Patchdiff --git a/dist/Math-BigInt/lib/Math/BigInt.pm b/dist/Math-BigInt/lib/Math/BigInt.pm
index 3e22d0f..cbfd6b7 100644
--- a/dist/Math-BigInt/lib/Math/BigInt.pm
+++ b/dist/Math-BigInt/lib/Math/BigInt.pm
@@ -1013,6 +1013,18 @@ sub babs
$x;
}
+sub bsgn {
+ # Signum function.
+
+ my $self = shift;
+
+ return $self if $self->modify('bsgn');
+
+ return $self -> bone("+") if $self -> is_pos();
+ return $self -> bone("-") if $self -> is_neg();
+ return $self; # zero or NaN
+}
+
sub bneg
{
# (BINT or num_str) return BINT
@@ -3315,6 +3327,7 @@ Math::BigInt - Arbitrary size integer/float math package
$x->bneg(); # negation
$x->babs(); # absolute value
+ $x->bsgn(); # sign function (-1, 0, 1, or NaN)
$x->bnorm(); # normalize (no-op in BigInt)
$x->bnot(); # two's complement (bit wise not)
$x->binc(); # increment $x by 1
diff --git a/dist/Math-BigInt/t/bare_mbi.t b/dist/Math-BigInt/t/bare_mbi.t
index a73fd12..b5188e4 100644
--- a/dist/Math-BigInt/t/bare_mbi.t
+++ b/dist/Math-BigInt/t/bare_mbi.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3619;
+use Test::More tests => 3631;
BEGIN { unshift @INC, 't'; }
diff --git a/dist/Math-BigInt/t/bigintpm.inc b/dist/Math-BigInt/t/bigintpm.inc
index f5f0fd2..0e36e50 100644
--- a/dist/Math-BigInt/t/bigintpm.inc
+++ b/dist/Math-BigInt/t/bigintpm.inc
@@ -73,7 +73,7 @@ while (<DATA>)
} elsif ($f eq "bone") {
$try .= "\$x->bone('$args[1]');";
# some unary ops
- } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) {
+ } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|sgn|inc|dec|not|sqrt|fac)$/) {
$try .= "\$x->$f();";
} elsif ($f =~ /^(numify|length|stringify|as_hex|as_bin)$/) {
$try .= "\$x->$f();";
@@ -1201,6 +1201,13 @@ babsNaN:NaN
-1:1
+123456789:123456789
-123456789:123456789
+&bsgn
+NaN:NaN
++inf:1
+-inf:-1
+0:0
++123456789:1
+-123456789:-1
&bcmp
bcmpNaN:bcmpNaN:
bcmpNaN:0:
diff --git a/dist/Math-BigInt/t/bigintpm.t b/dist/Math-BigInt/t/bigintpm.t
index d16844b..9ecc128 100644
--- a/dist/Math-BigInt/t/bigintpm.t
+++ b/dist/Math-BigInt/t/bigintpm.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3619 + 6;
+use Test::More tests => 3631 + 6;
use Math::BigInt lib => 'Calc';
diff --git a/dist/Math-BigInt/t/sub_mbi.t b/dist/Math-BigInt/t/sub_mbi.t
index a999c09..92fc121 100644
--- a/dist/Math-BigInt/t/sub_mbi.t
+++ b/dist/Math-BigInt/t/sub_mbi.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3619
+use Test::More tests => 3631
+ 5; # +5 own tests
BEGIN { unshift @INC, 't'; }
--
1.7.4 |
From @pjacklamI have attached a new patch file. It avoids the merge conflicts that Peter |
From @pjacklam0001-Add-sign-function-bsgn-as-a-complement-to-babs.patchFrom 8045910ecb5f9213c5ce89bcfb1e440c4bbcac7b Mon Sep 17 00:00:00 2001
From: Peter John Acklam <[email protected]>
Date: Tue, 1 Mar 2011 22:35:51 +0100
Subject: [PATCH] Add sign function bsgn() as a complement to babs().
This is the standard mathematical signum function. It sets the
invocand to -1, 0, or 1, if it is real, and NaN otherwise.
Documentation and tests are included.
---
dist/Math-BigInt/lib/Math/BigInt.pm | 13 +++++++++++++
dist/Math-BigInt/t/bare_mbi.t | 2 +-
dist/Math-BigInt/t/bigintpm.inc | 9 ++++++++-
dist/Math-BigInt/t/bigintpm.t | 2 +-
dist/Math-BigInt/t/sub_mbi.t | 2 +-
5 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/dist/Math-BigInt/lib/Math/BigInt.pm b/dist/Math-BigInt/lib/Math/BigInt.pm
index bb1f518..13c9170 100644
--- a/dist/Math-BigInt/lib/Math/BigInt.pm
+++ b/dist/Math-BigInt/lib/Math/BigInt.pm
@@ -1013,6 +1013,18 @@ sub babs
$x;
}
+sub bsgn {
+ # Signum function.
+
+ my $self = shift;
+
+ return $self if $self->modify('bsgn');
+
+ return $self -> bone("+") if $self -> is_pos();
+ return $self -> bone("-") if $self -> is_neg();
+ return $self; # zero or NaN
+}
+
sub bneg
{
# (BINT or num_str) return BINT
@@ -3310,6 +3322,7 @@ Math::BigInt - Arbitrary size integer/float math package
$x->bneg(); # negation
$x->babs(); # absolute value
+ $x->bsgn(); # sign function (-1, 0, 1, or NaN)
$x->bnorm(); # normalize (no-op in BigInt)
$x->bnot(); # two's complement (bit wise not)
$x->binc(); # increment $x by 1
diff --git a/dist/Math-BigInt/t/bare_mbi.t b/dist/Math-BigInt/t/bare_mbi.t
index d7139dd..b5188e4 100644
--- a/dist/Math-BigInt/t/bare_mbi.t
+++ b/dist/Math-BigInt/t/bare_mbi.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3623;
+use Test::More tests => 3631;
BEGIN { unshift @INC, 't'; }
diff --git a/dist/Math-BigInt/t/bigintpm.inc b/dist/Math-BigInt/t/bigintpm.inc
index e52a271..478584b 100644
--- a/dist/Math-BigInt/t/bigintpm.inc
+++ b/dist/Math-BigInt/t/bigintpm.inc
@@ -73,7 +73,7 @@ while (<DATA>)
} elsif ($f eq "bone") {
$try .= "\$x->bone('$args[1]');";
# some unary ops
- } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) {
+ } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|sgn|inc|dec|not|sqrt|fac)$/) {
$try .= "\$x->$f();";
} elsif ($f =~ /^(numify|length|stringify|as_hex|as_bin)$/) {
$try .= "\$x->$f();";
@@ -1222,6 +1222,13 @@ babsNaN:NaN
-1:1
+123456789:123456789
-123456789:123456789
+&bsgn
+NaN:NaN
++inf:1
+-inf:-1
+0:0
++123456789:1
+-123456789:-1
&bcmp
bcmpNaN:bcmpNaN:
bcmpNaN:0:
diff --git a/dist/Math-BigInt/t/bigintpm.t b/dist/Math-BigInt/t/bigintpm.t
index cacdb8e..9ecc128 100644
--- a/dist/Math-BigInt/t/bigintpm.t
+++ b/dist/Math-BigInt/t/bigintpm.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3623 + 6;
+use Test::More tests => 3631 + 6;
use Math::BigInt lib => 'Calc';
diff --git a/dist/Math-BigInt/t/sub_mbi.t b/dist/Math-BigInt/t/sub_mbi.t
index 668fd19..92fc121 100644
--- a/dist/Math-BigInt/t/sub_mbi.t
+++ b/dist/Math-BigInt/t/sub_mbi.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3623
+use Test::More tests => 3631
+ 5; # +5 own tests
BEGIN { unshift @INC, 't'; }
--
1.7.4
|
The RT System itself - Status changed from 'new' to 'open' |
From @pjacklamPlease disregard my previous message. This patch should be OK and Peter |
From @pjacklam0001-Add-sign-function-bsgn-as-a-complement-to-babs.patchFrom a1ae9b2bdaf1186fbbf63fc13b8885ac618a4b43 Mon Sep 17 00:00:00 2001
From: Peter John Acklam <[email protected]>
Date: Mon, 7 Mar 2011 11:45:38 +0100
Subject: [PATCH] Add sign function bsgn() as a complement to babs().
This is the standard mathematical signum function. It sets the
invocand to -1, 0, or 1, if it is real, and NaN otherwise.
Documentation and tests are included.
---
dist/Math-BigInt/lib/Math/BigInt.pm | 13 +++++++++++++
dist/Math-BigInt/t/bare_mbi.t | 2 +-
dist/Math-BigInt/t/bigintpm.inc | 9 ++++++++-
dist/Math-BigInt/t/bigintpm.t | 2 +-
dist/Math-BigInt/t/sub_mbi.t | 2 +-
5 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/dist/Math-BigInt/lib/Math/BigInt.pm b/dist/Math-BigInt/lib/Math/BigInt.pm
index bb1f518..13c9170 100644
--- a/dist/Math-BigInt/lib/Math/BigInt.pm
+++ b/dist/Math-BigInt/lib/Math/BigInt.pm
@@ -1013,6 +1013,18 @@ sub babs
$x;
}
+sub bsgn {
+ # Signum function.
+
+ my $self = shift;
+
+ return $self if $self->modify('bsgn');
+
+ return $self -> bone("+") if $self -> is_pos();
+ return $self -> bone("-") if $self -> is_neg();
+ return $self; # zero or NaN
+}
+
sub bneg
{
# (BINT or num_str) return BINT
@@ -3310,6 +3322,7 @@ Math::BigInt - Arbitrary size integer/float math package
$x->bneg(); # negation
$x->babs(); # absolute value
+ $x->bsgn(); # sign function (-1, 0, 1, or NaN)
$x->bnorm(); # normalize (no-op in BigInt)
$x->bnot(); # two's complement (bit wise not)
$x->binc(); # increment $x by 1
diff --git a/dist/Math-BigInt/t/bare_mbi.t b/dist/Math-BigInt/t/bare_mbi.t
index d7139dd..9f2198b 100644
--- a/dist/Math-BigInt/t/bare_mbi.t
+++ b/dist/Math-BigInt/t/bare_mbi.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3623;
+use Test::More tests => 3635;
BEGIN { unshift @INC, 't'; }
diff --git a/dist/Math-BigInt/t/bigintpm.inc b/dist/Math-BigInt/t/bigintpm.inc
index e52a271..478584b 100644
--- a/dist/Math-BigInt/t/bigintpm.inc
+++ b/dist/Math-BigInt/t/bigintpm.inc
@@ -73,7 +73,7 @@ while (<DATA>)
} elsif ($f eq "bone") {
$try .= "\$x->bone('$args[1]');";
# some unary ops
- } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|inc|dec|not|sqrt|fac)$/) {
+ } elsif ($f =~ /^b(nan|floor|ceil|sstr|neg|abs|sgn|inc|dec|not|sqrt|fac)$/) {
$try .= "\$x->$f();";
} elsif ($f =~ /^(numify|length|stringify|as_hex|as_bin)$/) {
$try .= "\$x->$f();";
@@ -1222,6 +1222,13 @@ babsNaN:NaN
-1:1
+123456789:123456789
-123456789:123456789
+&bsgn
+NaN:NaN
++inf:1
+-inf:-1
+0:0
++123456789:1
+-123456789:-1
&bcmp
bcmpNaN:bcmpNaN:
bcmpNaN:0:
diff --git a/dist/Math-BigInt/t/bigintpm.t b/dist/Math-BigInt/t/bigintpm.t
index cacdb8e..6ee3eff 100644
--- a/dist/Math-BigInt/t/bigintpm.t
+++ b/dist/Math-BigInt/t/bigintpm.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3623 + 6;
+use Test::More tests => 3635 + 6;
use Math::BigInt lib => 'Calc';
diff --git a/dist/Math-BigInt/t/sub_mbi.t b/dist/Math-BigInt/t/sub_mbi.t
index 668fd19..6a3cecc 100644
--- a/dist/Math-BigInt/t/sub_mbi.t
+++ b/dist/Math-BigInt/t/sub_mbi.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl -w
use strict;
-use Test::More tests => 3623
+use Test::More tests => 3635
+ 5; # +5 own tests
BEGIN { unshift @INC, 't'; }
--
1.7.4
|
From @cpansproutOn Mon Mar 07 02:48:06 2011, pjacklam@gmail.com wrote:
Thank you. Applied as 7833bfd. |
@cpansprout - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#85216 (status was 'resolved')
Searchable as RT85216$
The text was updated successfully, but these errors were encountered: