From: "Rüdiger Sonderfeld" <ruediger@c-plusplus.de>
To: emacs-devel@gnu.org
Subject: [PATCH] Add support for log2.
Date: Thu, 20 Jun 2013 02:03:50 +0200 [thread overview]
Message-ID: <1783344.43FasZRPlO@descartes> (raw)
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
next reply other threads:[~2013-06-20 0:03 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-20 0:03 Rüdiger Sonderfeld [this message]
2013-06-20 2:15 ` [PATCH] Add support for log2 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
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/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1783344.43FasZRPlO@descartes \
--to=ruediger@c-plusplus.de \
--cc=emacs-devel@gnu.org \
/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.
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).