unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Kevin Ryde <user42@zip.com.au>
Subject: magnitude and make-polar
Date: Sat, 05 Jul 2003 10:02:23 +1000	[thread overview]
Message-ID: <87el15suwg.fsf@zip.com.au> (raw)

[-- Attachment #1: Type: text/plain, Size: 434 bytes --]

        * numbers.c (s_scm_make_polar): Use sincos, when available.
        (scm_magnitude): Use hypot.
        * configure.in (AC_CHECK_FUNCS): Add sincos.
        * tests/numbers.test (make-polar, magnitude): Add tests.

glibc sincos is supposedly more efficient than separate sin and cos
calls.

hypot should give greater accuracy than a sqrt expression.  It dates
back at least to v7 unix and so shouldn't need a configure test.


[-- Attachment #2: numbers.c.sh.diff --]
[-- Type: text/plain, Size: 915 bytes --]

--- numbers.c.~1.192.~	2003-06-21 10:06:44.000000000 +1000
+++ numbers.c	2003-07-01 17:08:54.000000000 +1000
@@ -3852,8 +3852,15 @@
 #define FUNC_NAME s_scm_make_polar
 {
   struct dpair xy;
+  double s, c;
   scm_two_doubles (x, y, FUNC_NAME, &xy);
-  return scm_make_complex (xy.x * cos (xy.y), xy.x * sin (xy.y));
+#if HAVE_SINCOS
+  sincos (xy.y, &s, &c);
+#else
+  s = sin (xy.y);
+  c = cos (xy.y);
+#endif
+  return scm_make_complex (xy.x * c, xy.x * s);
 }
 #undef FUNC_NAME
 
@@ -3925,9 +3932,7 @@
   } else if (SCM_REALP (z)) {
     return scm_make_real (fabs (SCM_REAL_VALUE (z)));
   } else if (SCM_COMPLEXP (z)) {
-    double r = SCM_COMPLEX_REAL (z);
-    double i = SCM_COMPLEX_IMAG (z);
-    return scm_make_real (sqrt (i * i + r * r));
+    return scm_make_real (hypot (SCM_COMPLEX_REAL (z), SCM_COMPLEX_IMAG (z)));
   } else {
     SCM_WTA_DISPATCH_1 (g_magnitude, z, SCM_ARG1, s_magnitude);
   }

[-- Attachment #3: configure.in.sh.diff --]
[-- Type: text/plain, Size: 765 bytes --]

--- configure.in.~1.222.~	2003-06-21 10:15:31.000000000 +1000
+++ configure.in	2003-07-01 17:18:45.000000000 +1000
@@ -759,10 +759,12 @@
 
 AC_CHECK_HEADERS(floatingpoint.h ieeefp.h nan.h)
 
-# asinh, acosh, atanh and trunc (all in -lm) are only C99 standard and older
-# systems generally don't have them.
+# Reasons for testing:
+#   asinh, acosh, atanh, trunc - C99 standard, generally not available on
+#                                older systems
+#   sincos - GLIBC extension
 #
-AC_CHECK_FUNCS(asinh acosh atanh copysign finite isinf isnan trunc)
+AC_CHECK_FUNCS(asinh acosh atanh copysign finite isinf isnan sincos trunc)
 
 # When testing for the presence of alloca, we need to add alloca.o
 # explicitly to LIBOBJS to make sure that it is translated to

[-- Attachment #4: numbers.test.sh.diff --]
[-- Type: text/plain, Size: 1106 bytes --]

--- numbers.test.~1.26.~	2003-06-21 10:17:40.000000000 +1000
+++ numbers.test	2003-07-01 17:06:53.000000000 +1000
@@ -1940,6 +1940,21 @@
 ;;; make-polar
 ;;;
 
+(with-test-prefix "make-polar"
+  (define pi 3.14159265358979323846)
+  (define (almost= x y)
+    (> 0.01 (magnitude (- x y))))
+  
+  (pass-if (= 0 (make-polar 0 0)))
+  (pass-if (= 0 (make-polar 0 123.456)))
+  (pass-if (= 1 (make-polar 1 0)))
+  (pass-if (= -1 (make-polar -1 0)))
+  
+  (pass-if (almost= 0+i (make-polar 1 (* 0.5 pi))))
+  (pass-if (almost= -1  (make-polar 1 (* 1.0 pi))))
+  (pass-if (almost= 0-i (make-polar 1 (* 1.5 pi))))
+  (pass-if (almost= 1   (make-polar 1 (* 2.0 pi)))))
+
 ;;;
 ;;; real-part
 ;;;
@@ -1952,6 +1967,17 @@
 ;;; magnitude
 ;;;
 
+(with-test-prefix "magnitude"
+  (pass-if (= 0 (magnitude 0)))
+  (pass-if (= 1 (magnitude 1)))
+  (pass-if (= 1 (magnitude -1)))
+  (pass-if (= 1 (magnitude 0+i)))
+  (pass-if (= 1 (magnitude 0-i)))
+  (pass-if (= 5 (magnitude 3+4i)))
+  (pass-if (= 5 (magnitude 3-4i)))
+  (pass-if (= 5 (magnitude -3+4i)))
+  (pass-if (= 5 (magnitude -3-4i))))
+
 ;;;
 ;;; angle
 ;;;

[-- Attachment #5: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel

             reply	other threads:[~2003-07-05  0:02 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-05  0:02 Kevin Ryde [this message]
2003-07-08  0:30 ` magnitude and make-polar Kevin Ryde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87el15suwg.fsf@zip.com.au \
    --to=user42@zip.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).