unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#74031: [PATCH] srfi-64: Accept complex numbers in test-approximate.
@ 2024-10-26 16:07 Tomas Volf
  2024-10-26 17:22 ` Ludovic Courtès
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Tomas Volf @ 2024-10-26 16:07 UTC (permalink / raw)
  To: 74031; +Cc: Tomas Volf

The specification mandates reals, but the reference implementation
supports complex numbers.  So as implementation extension, support them
as well.

* module/srfi/srfi-64.scm (within-epsilon): Support complex arguments.
---
Proposal for how to extend test-approximate to handle complex arguments.
However it differs from the original one.  That one expected `error' to be a
real number, and used it for comparing both real parts and imaginary parts.

To me, that seems weird.  I would consider it useful to be able to have
different errors for real and imaginary parts.

However I cannot remember the last time I have used complex numbers, so I am not
sure I am qualified to have an opinion here.  What do other people think?

 module/srfi/srfi-64.scm | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/module/srfi/srfi-64.scm b/module/srfi/srfi-64.scm
index 1f60a72e5..5fc23e28a 100644
--- a/module/srfi/srfi-64.scm
+++ b/module/srfi/srfi-64.scm
@@ -776,8 +776,16 @@ Test whether result of @var{test-expr} matches @var{expected} using

 (define (within-epsilon ε)
   (λ (expected actual)
-    (and (>= actual (- expected ε))
-         (<= actual (+ expected ε)))))
+    (let ((e-r (real-part expected))
+          (e-i (imag-part expected))
+          (a-r (real-part actual))
+          (a-i (imag-part actual))
+          (ε-r (real-part ε))
+          (ε-i (imag-part ε)))
+      (and (>= a-r (- e-r ε-r))
+           (<= a-r (+ e-r ε-r))
+           (>= a-i (- e-i ε-i))
+           (<= a-i (+ e-i ε-i))))))

 (define-syntax %test-approximate
   (λ (x)
@@ -808,6 +816,10 @@ Test whether result of @var{test-expr} matches @var{expected} using
 Test whether result of @var{test-expr} is within @var{error} of
 @var{expected}.

+As implementation extension, complex numbers are supported as well.  It tests
+whether real parts are within @code{(real-part @var{error})}, and imaginary
+parts within @code{(imag-part @var{error})}.
+
 @end defspec")

 (define-syntax %test-error
--
2.46.0





^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-10-26 21:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-26 16:07 bug#74031: [PATCH] srfi-64: Accept complex numbers in test-approximate Tomas Volf
2024-10-26 17:22 ` Ludovic Courtès
2024-10-26 18:46   ` lloda
2024-10-26 20:26     ` Tomas Volf
2024-10-26 18:35 ` lloda
2024-10-26 20:19 ` bug#74031: [PATCH v2] " Tomas Volf
2024-10-26 21:06 ` bug#74031: [PATCH v3] " Tomas Volf

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).