zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] zsh/mathfunc: Add log2()
Date: Sun, 12 Aug 2018 03:20:37 -0500	[thread overview]
Message-ID: <647A3A10-A3AA-4E02-98AC-38385B34D35A@dana.is> (raw)

Someone on IRC today was absolutely incensed that zsh/mathfunc doesn't provide a
log2(). I assume that's just because it's trivial to do it in 'user land'. But
it's also trivial to do it here, so....

I added an #ifdef like in workers/39064, since i read on-line that there were
still libraries missing the C99 function fairly recently. And since i added an
#ifdef i also added a test.

dana


diff --git a/Doc/Zsh/mod_mathfunc.yo b/Doc/Zsh/mod_mathfunc.yo
index 8b72de3ab..428a5be47 100644
--- a/Doc/Zsh/mod_mathfunc.yo
+++ b/Doc/Zsh/mod_mathfunc.yo
@@ -24,9 +24,9 @@ The following functions take a single floating point argument: tt(acos),
 tt(acosh), tt(asin), tt(asinh), tt(atan), tt(atanh), tt(cbrt), tt(ceil),
 tt(cos), tt(cosh), tt(erf), tt(erfc), tt(exp), tt(expm1), tt(fabs),
 tt(floor), tt(gamma), tt(j0), tt(j1), tt(lgamma), tt(log), tt(log10),
-tt(log1p), tt(logb), tt(sin), tt(sinh), tt(sqrt), tt(tan), tt(tanh),
-tt(y0), tt(y1).  The tt(atan) function can optionally take a second
-argument, in which case it behaves like the C function tt(atan2).
+tt(log1p), tt(log2), tt(logb), tt(sin), tt(sinh), tt(sqrt), tt(tan),
+tt(tanh), tt(y0), tt(y1).  The tt(atan) function can optionally take a
+second argument, in which case it behaves like the C function tt(atan2).
 The tt(ilogb) function takes a single floating point argument, but
 returns an integer.
 
diff --git a/Src/Modules/mathfunc.c b/Src/Modules/mathfunc.c
index d1c3e089a..fc2593dca 100644
--- a/Src/Modules/mathfunc.c
+++ b/Src/Modules/mathfunc.c
@@ -65,6 +65,7 @@ MF_LGAMMA,
 MF_LOG,
 MF_LOG10,
 MF_LOG1P,
+MF_LOG2,
 MF_LOGB,
 MF_NEXTAFTER,
 MF_RINT,
@@ -142,6 +143,7 @@ static struct mathfunc mftab[] = {
   NUMMATHFUNC("log", math_func, 1, 1, MF_LOG),
   NUMMATHFUNC("log10", math_func, 1, 1, MF_LOG10),
   NUMMATHFUNC("log1p", math_func, 1, 1, MF_LOG1P),
+  NUMMATHFUNC("log2", math_func, 1, 1, MF_LOG2),
   NUMMATHFUNC("logb", math_func, 1, 1, MF_LOGB),
   NUMMATHFUNC("nextafter", math_func, 2, 2, MF_NEXTAFTER),
 #ifdef HAVE_ERAND48
@@ -338,6 +340,14 @@ math_func(UNUSED(char *name), int argc, mnumber *argv, int id)
       retd = log1p(argd);
       break;
 
+  case MF_LOG2:
+#ifdef HAVE_LOG2
+      retd = log2(argd);
+#else
+      retd = log(argd) / log(2);
+#endif
+      break;
+
   case MF_LOGB:
       retd = logb(argd); 
       break;
diff --git a/Test/V03mathfunc.ztst b/Test/V03mathfunc.ztst
index d6b4e0987..9a297d69d 100644
--- a/Test/V03mathfunc.ztst
+++ b/Test/V03mathfunc.ztst
@@ -139,3 +139,9 @@ F:This test fails if your math library doesn't have erand48().
   print $g, $l
 0:Test Gamma function gamma and lgamma
 >1.00000, 0.00000
+
+  float -F 5 a b c
+  (( a = log2(0.5), b = log2(1.5), c = log2(99) ))
+  print -r - "$a, $b, $c"
+0:log2
+>-1.00000, 0.58496, 6.62936
diff --git a/configure.ac b/configure.ac
index 53c30c2cf..5e13c0f11 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1266,6 +1266,7 @@ AC_CHECK_FUNCS(strftime strptime mktime timelocal \
 	       isblank iswblank \
 	       uname \
 	       signgam tgamma \
+	       log2 \
 	       scalbn \
 	       putenv getenv setenv unsetenv xw\
 	       brk sbrk \




             reply	other threads:[~2018-08-12  8:20 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20180812082115epcas4p17b38f56487b9d30c9fcd9b450938cbf1@epcas4p1.samsung.com>
2018-08-12  8:20 ` dana [this message]
2018-08-13  8:39   ` Peter Stephenson

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

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

  git send-email \
    --in-reply-to=647A3A10-A3AA-4E02-98AC-38385B34D35A@dana.is \
    --to=dana@dana.is \
    --cc=zsh-workers@zsh.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.vuxu.org/mirror/zsh/

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 NNTP newsgroup(s).