emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add support for log2.


From: Rüdiger Sonderfeld
Subject: [PATCH] Add support for log2.
Date: Thu, 20 Jun 2013 02:03:50 +0200
User-agent: KMail/4.10.3 (Linux/3.8.0-23-generic; KDE/4.10.3; x86_64; ; )

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 <address@hidden>
---
 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 <address@hidden> 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]