On Fri, 30 Nov 2001, Nicholas Clark wrote:
> On Fri, Nov 30, 2001 at 12:04:13AM +0000, Simon Cozens wrote:
> > On Thu, Nov 29, 2001 at 04:50:24PM -0500, James Mastros wrote:
> > > Why? For at least x86, your way will be a lot slower.
> >
> > Maybe I should try to find out why Perl 5 does it like that before making
> > such pronouncements. :)
> >
> > Nick, any idea why Perl's abs doesn't use abs?
> 2: abs() does integers, fabs() does doubles. I think they're both ANSI C89.
> but are labs(), llabs(), and for floating point fabsl() and fabsf()?
> [now that our IVs and NVs can be long longs and long doubles.
> and if labs() isn't C89 we can't even safely do IV is long]
>
I think it makes sense to do what simon says, as this way the size of
FLOATVAL and INTVAL can be bigger than double/long and everything will still
work without any changes.
Patch the second attached.
Alex Gough
--
All progress is initiated by challenging current conceptions,
and executed by supplanting existing institutions.
? classes/perlint.c
? classes/perlstring.c
? classes/perlnum.c
Index: core.ops
===================================================================
RCS file: /home/perlcvs/parrot/core.ops,v
retrieving revision 1.33
diff -u -r1.33 core.ops
--- core.ops 2001/11/29 04:25:02 1.33
+++ core.ops 2001/11/30 01:11:31
@@ -739,6 +739,32 @@
########################################
+=item B<abs>(i|n, i|ic|n|nc)
+
+Set $1 to absolute value of $2.
+
+=cut
+
+AUTO_OP abs(i, i|ic|n|nc) {
+ if ($2 < 0) {
+ $1 = - (INTVAL)$2;
+ }
+ else {
+ $1 = (INTVAL)$2;
+ }
+}
+
+AUTO_OP abs(n, n|nc|i|ic) {
+ if ($2 < 0) {
+ $1 = - (FLOATVAL)$2;
+ }
+ else {
+ $1 = $2;
+ }
+}
+
+########################################
+
=item B<cmod>(i, i, i)
=item B<cmod>(i, i, ic)
Index: t/op/integer.t
===================================================================
RCS file: /home/perlcvs/parrot/t/op/integer.t,v
retrieving revision 1.13
diff -u -r1.13 integer.t
--- t/op/integer.t 2001/10/14 00:43:50 1.13
+++ t/op/integer.t 2001/11/30 01:11:31
@@ -1,6 +1,6 @@
#! perl -w
-use Parrot::Test tests => 27;
+use Parrot::Test tests => 28;
output_is(<<CODE, <<OUTPUT, "set_i_ic");
# XXX: Need a test for writing outside the set of available
@@ -169,6 +169,25 @@
574908040
862362060
1724724120
+OUTPUT
+
+output_is(<<'CODE', <<'OUTPUT', "abs(i, i|ic)");
+ set I0, 1
+ abs I1, -1
+ abs I0, I0
+ set I2, -1
+ abs I2, I2
+ print I0
+ print "\n"
+ print I1
+ print "\n"
+ print I2
+ print "\n"
+ end
+CODE
+1
+1
+1
OUTPUT
output_is(<<CODE, <<OUTPUT, "sub_i");
Index: t/op/number.t
===================================================================
RCS file: /home/perlcvs/parrot/t/op/number.t,v
retrieving revision 1.9
diff -u -r1.9 number.t
--- t/op/number.t 2001/10/14 00:43:50 1.9
+++ t/op/number.t 2001/11/30 01:11:32
@@ -1,6 +1,6 @@
#! perl -w
-use Parrot::Test tests => 27;
+use Parrot::Test tests => 28;
output_is(<<CODE, <<OUTPUT, "set_n_nc");
set N0, 1.0
@@ -151,6 +151,29 @@
end
CODE
420042.000000
+OUTPUT
+
+output_is(<<'CODE', <<'OUTPUT', "abs(n, i|ic|n|nc)");
+ set I0, -1
+ abs N0, I0
+ abs N1, -1
+ set N2, -1
+ abs N2, N2
+ abs N3, -1.0
+ print N0
+ print "\n"
+ print N1
+ print "\n"
+ print N2
+ print "\n"
+ print N3
+ print "\n"
+ end
+CODE
+1.000000
+1.000000
+1.000000
+1.000000
OUTPUT
output_is(<<CODE, <<OUTPUT, "mul_i");