unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] Add support for log2.
@ 2013-06-20  0:03 Rüdiger Sonderfeld
  2013-06-20  2:15 ` Stefan Monnier
  0 siblings, 1 reply; 6+ messages in thread
From: Rüdiger Sonderfeld @ 2013-06-20  0:03 UTC (permalink / raw)
  To: emacs-devel

log2(3) is a new function in C99.  I think it makes sense adding
support for it in Emacs because of the improved accuracy and
logarithmus dualis is common in computer science and information
theory.  The following code snipped (from jlf) shows the improved
accuracy for larger numbers:

  (mapcar (lambda (n)
            (let ((float-log (log (expt 2 n) 2)))
              (list (if (> float-log n) '> '≯)
                    (if (= float-log n) '= '≠)
                    (if (< float-log n) '< '≮))))
          (number-sequence 0 31))

A feature test is added to configure.ac and a fallback for legacy
systems included.

* src/floatfns.c (Flog): Add special case for `log2'.
  (Flog2): New function.

* configure.ac: Test for `log2'.

Signed-off-by: Rüdiger Sonderfeld <ruediger@c-plusplus.de>
---
 configure.ac   |  2 +-
 src/floatfns.c | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/configure.ac b/configure.ac
index a16a52d..6e5c888 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3235,7 +3235,7 @@ gai_strerror mkstemp getline getdelim sync \
 difftime posix_memalign \
 getpwent endpwent getgrent endgrent \
 touchlock \
-cfmakeraw cfsetspeed copysign __executable_start)
+cfmakeraw cfsetspeed copysign __executable_start log2)
 
 ## Eric Backus <ericb@lsid.hp.com> says, HP-UX 9.x on HP 700 machines
 ## has a broken `rint' in some library versions including math library
diff --git a/src/floatfns.c b/src/floatfns.c
index d7514ec..a7e03e7 100644
--- a/src/floatfns.c
+++ b/src/floatfns.c
@@ -241,12 +241,29 @@ The function returns the cons cell (SGNFCAND . EXP).
 
       if (b == 10.0)
 	d = log10 (d);
+#ifdef HAVE_LOG2
+      else if (b == 2.0)
+        d = log2 (d);
+#endif
       else
 	d = log (d) / log (b);
     }
   return make_float (d);
 }
 
+DEFUN ("log2", Flog2, Slog2, 1, 1, 0,
+       doc: /* Return the logarithm base 2 of ARG.  */)
+  (Lisp_Object arg)
+{
+  double d = extract_float (arg);
+#ifdef HAVE_LOG2
+  d = log2 (d);
+#else
+  d = log (d) / log (2.0);
+#endif
+  return make_float(d);
+}
+
 DEFUN ("log10", Flog10, Slog10, 1, 1, 0,
        doc: /* Return the logarithm base 10 of ARG.  */)
   (Lisp_Object arg)
@@ -553,6 +570,7 @@ The function returns the cons cell (SGNFCAND . EXP).
   defsubr (&Sexp);
   defsubr (&Sexpt);
   defsubr (&Slog);
+  defsubr (&Slog2);
   defsubr (&Slog10);
   defsubr (&Ssqrt);
 
-- 
1.8.3.1




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

end of thread, other threads:[~2013-06-20 14:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-20  0:03 [PATCH] Add support for log2 Rüdiger Sonderfeld
2013-06-20  2:15 ` Stefan Monnier
2013-06-20  2:36   ` Paul Eggert
2013-06-20 12:56     ` Rüdiger Sonderfeld
2013-06-20 14:27       ` Paul Eggert
2013-06-20 14:36         ` Rüdiger Sonderfeld

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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