Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sqrt

15 views
Skip to first unread message

Stefan Lidman

unread,
Apr 26, 2004, 9:29:05 PM4/26/04
to perl6-i...@perl.org
This adds a square root op and fixes a typo. It is also my
first patch so I might have missed something, please tell
me what I did wrong.

/Stefan

cvs server: Diffing ops
Index: ops/math.ops
===================================================================
RCS file: /cvs/public/parrot/ops/math.ops,v
retrieving revision 1.17
diff -r1.17 math.ops
853a854,873
> ########################################
>
> =item B<sqrt>(out NUM, in INT)
>
> =item B<sqrt>(out NUM, in NUM)
>
> Set $1 to the square root of $2.
>
> =cut
>
> inline op sqrt(out NUM, in INT) :base_core {
> $1 = sqrt((FLOATVAL)$2);
> goto NEXT();
> }
>
> inline op sqrt(out NUM, in NUM) :base_core {
> $1 = sqrt((FLOATVAL)$2);
> goto NEXT();
> }
>
857a878,879
>
>
1147c1169
< item B<sin>(out NUM, in NUM)
---
> =item B<sin>(out NUM, in NUM)


cvs server: Diffing t/op
Index: t/op/trans.t
===================================================================
RCS file: /cvs/public/parrot/t/op/trans.t,v
retrieving revision 1.10
diff -r1.10 trans.t
19c19
< use Parrot::Test tests => 18;
---
> use Parrot::Test tests => 19;
605a606,626
> output_is( <<"CODE", <<OUTPUT, "sqrt" );
> @{[ $fp_equality_macro ]}
> set N1, 9.0
> sqrt N2, N1
> .fp_eq (N2, 3.0, EQ1)
> print "not "
> EQ1: print "ok 1\\n"
>
> set I1, 9
> sqrt N2, I1
> .fp_eq (N2, 3.0, EQ2)
> print "not "
> EQ2: print "ok 2\\n"
>
> end
> CODE
> ok 1
> ok 2
> OUTPUT
>
>

Dan Sugalski

unread,
Apr 27, 2004, 9:59:14 AM4/27/04
to Stefan Lidman, perl6-i...@perl.org
At 3:29 AM +0200 4/27/04, Stefan Lidman wrote:
>This adds a square root op and fixes a typo. It is also my
>first patch so I might have missed something, please tell
>me what I did wrong.

Everything is right, except... you forgot either the -c or -u switch
for the diff. (And no, I don't know why the default output from diffs
is mostly useless. Just one of those things, I expect)
--
Dan

--------------------------------------"it's like this"-------------------
Dan Sugalski even samurai
d...@sidhe.org have teddy bears and even
teddy bears get drunk

Stefan Lidman

unread,
Apr 27, 2004, 10:50:43 AM4/27/04
to perl6-i...@perl.org
>Everything is right, except... you forgot either the -c or -u switch
>for the diff. (And no, I don't know why the default output from diffs
>is mostly useless. Just one of those things, I expect)

ok resend with -u switch.

/Stefan

Index: ops/math.ops
===================================================================
RCS file: /cvs/public/parrot/ops/math.ops,v
retrieving revision 1.17

diff -u -r1.17 math.ops
--- ops/math.ops 22 Apr 2004 09:17:38 -0000 1.17
+++ ops/math.ops 27 Apr 2004 14:43:47 -0000
@@ -851,10 +851,32 @@
goto NEXT();
}

+########################################
+
+=item B<sqrt>(out NUM, in INT)
+
+=item B<sqrt>(out NUM, in NUM)
+
+Set $1 to the square root of $2.
+
+=cut
+
+inline op sqrt(out NUM, in INT) :base_core {
+ $1 = sqrt((FLOATVAL)$2);
+ goto NEXT();
+}
+
+inline op sqrt(out NUM, in NUM) :base_core {
+ $1 = sqrt((FLOATVAL)$2);
+ goto NEXT();
+}
+
=back

=cut

+
+
###############################################################################

=head2 Transcendental mathematical operations
@@ -1144,7 +1166,7 @@

=item B<sin>(out NUM, in INT)

-item B<sin>(out NUM, in NUM)
+=item B<sin>(out NUM, in NUM)

Set $1 to the sine of $2 (given in radians).

Index: t/op/trans.t
===================================================================
RCS file: /cvs/public/parrot/t/op/trans.t,v
retrieving revision 1.10

diff -u -r1.10 trans.t
--- t/op/trans.t 8 Mar 2004 00:19:58 -0000 1.10
+++ t/op/trans.t 27 Apr 2004 14:43:47 -0000
@@ -16,7 +16,7 @@

=cut

-use Parrot::Test tests => 18;
+use Parrot::Test tests => 19;
use Math::Trig qw( tan sec atan asin acos asec cosh sinh tanh sech );

# This defines two macros:
@@ -603,4 +603,25 @@
ok 16
OUTPUT

+output_is( <<"CODE", <<OUTPUT, "sqrt" );
+@{[ $fp_equality_macro ]}
+ set N1, 9.0
+ sqrt N2, N1
+ .fp_eq (N2, 3.0, EQ1)
+ print "not "
+EQ1: print "ok 1\\n"
+
+ set I1, 9
+ sqrt N2, I1
+ .fp_eq (N2, 3.0, EQ2)
+ print "not "
+EQ2: print "ok 2\\n"
+
+ end
+CODE
+ok 1
+ok 2
+OUTPUT
+
+
1;

Dan Sugalski

unread,
Apr 27, 2004, 11:48:32 AM4/27/04
to Stefan Lidman, perl6-i...@perl.org
At 4:50 PM +0200 4/27/04, Stefan Lidman wrote:
> >Everything is right, except... you forgot either the -c or -u switch
>>for the diff. (And no, I don't know why the default output from diffs
>>is mostly useless. Just one of those things, I expect)
>
>ok resend with -u switch.

And it's in. Thanks!

Matt Fowles

unread,
Apr 27, 2004, 10:29:13 PM4/27/04
to Dan Sugalski, Stefan Lidman, perl6-i...@perl.org
All~

Perhaps it is just the mathematician in me speaking, but I think that a
sqrt op might be superfluous and could be replaced with exp as sqrt(x)
== exp(x,0.5). I cannot say anything about numerical stability of
precision, though, so feel free to shoot me down.

Matt

Peter Behroozi

unread,
Apr 28, 2004, 12:10:43 AM4/28/04
to Matt_...@softhome.net, perl6-i...@perl.org
On Tue, 2004-04-27 at 22:29, Matt Fowles wrote:
> All~
>
> Perhaps it is just the mathematician in me speaking, but I think that a
> sqrt op might be superfluous and could be replaced with exp as sqrt(x)
> == exp(x,0.5). I cannot say anything about numerical stability of
> precision, though, so feel free to shoot me down.
>
> Matt

Then it's the processor in me speaking that it's much faster to optimize
for the sqrt() case. At least on the x86 and some PowerPC chips,
there's an actual fsqrt op because the processor can just shift the
exponent left or right. Even if an fsqrt op doesn't exist, Newton's
method is very easy for a sqrt function. Pow() is harder to implement,
since you can't get away with Newton's method (you run into a
chicken-and-egg problem); I believe you have to take either a square
root or a square for every bit in the desired exponent and then multiply
the results together (corrections anyone?). The result is enormously
large and slow (and perhaps imprecise as well?) compared to sqrt(); even
using the native pow() it takes nearly 15x as long as the native sqrt()
on my machine.

Hence, if you are taking lots of square roots, it's definitely in your
best interests to use a sqrt() instead of pow().

Peter

Uri Guttman

unread,
Apr 28, 2004, 12:53:15 AM4/28/04
to Matt_...@softhome.net, Dan Sugalski, Stefan Lidman, perl6-i...@perl.org
>>>>> "MF" == Matt Fowles <Matt_...@softhome.net> writes:

MF> Perhaps it is just the mathematician in me speaking, but I think that
MF> a sqrt op might be superfluous and could be replaced with exp as
MF> sqrt(x) == exp(x,0.5). I cannot say anything about numerical
MF> stability of precision, though, so feel free to shoot me down.

i bet sqrt is in most math libs since a pure sqrt is a simpler and
faster algorithm than exp(x,0.5) would use. and some fpu's have sqrt as
a builtin so you want to be able to access that. sqrt is called often
enough to deserve its own fpu instruction and library entry.

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Dan Sugalski

unread,
Apr 28, 2004, 8:41:38 AM4/28/04
to Matt_...@softhome.net, Stefan Lidman, perl6-i...@perl.org
At 10:29 PM -0400 4/27/04, Matt Fowles wrote:
>All~
>
>Perhaps it is just the mathematician in me speaking, but I think
>that a sqrt op might be superfluous and could be replaced with exp
>as sqrt(x) == exp(x,0.5). I cannot say anything about numerical
>stability of precision, though, so feel free to shoot me down.

It's likely the mathematician that's not in me, but... I'd completely
forgotten about the sqrt(x) == exp(x, 0.5) equivalence. I'm likely
not alone (though for this I'm not sure anyone'd want to be seen in
my company :). Between that and built-in hardware support for square
roots specifically, I think it's worth having as a special case.

>Dan Sugalski wrote:
>>At 4:50 PM +0200 4/27/04, Stefan Lidman wrote:
>>
>>> >Everything is right, except... you forgot either the -c or -u switch
>>>
>>>>for the diff. (And no, I don't know why the default output from diffs
>>>>is mostly useless. Just one of those things, I expect)
>>>
>>>
>>>ok resend with -u switch.
>>
>>
>>And it's in. Thanks!

0 new messages