unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#20185: 24.4.50; problem with built-in function "ldexp" Emacs Ver: 24.4.50.1
@ 2015-03-23 20:29 Bernard Hurley
  2015-03-24 18:57 ` Paul Eggert
  0 siblings, 1 reply; 2+ messages in thread
From: Bernard Hurley @ 2015-03-23 20:29 UTC (permalink / raw)
  To: 20185


(24.4.50) 3.2 Floating-Point Basics we read:

---------- snip ------------
 -- Function: ldexp sig &optional exp
     This function returns a floating-point number corresponding to the
     significand SIG and exponent EXP.
----------snip -------------

However it is easy to verify that the second argument is not
optional. For example:

    (ldexp 5.0)

triggers the error:

    *** Eval error ***  Wrong type argument: integerp, nil

The code for ldexp in floatfn.c reads:

---------- snip ---------------
DEFUN ("ldexp", Fldexp, Sldexp, 1, 2, 0,
       doc: /* Construct number X from significand SGNFCAND and exponent EXP.
Returns the floating point value resulting from multiplying SGNFCAND
(the significand) by 2 raised to the power of EXP (the exponent).   */)
  (Lisp_Object sgnfcand, Lisp_Object exponent)
{
  CHECK_NUMBER (exponent);
  return make_float (ldexp (XFLOATINT (sgnfcand), XINT (exponent)));
}
---------- snip -------------

The declaration conforms to that in the manual but nothing seems to have
been done to take into account what happens when `exp' is missing.  It
seems to me that there are two possibilities:

EITHER: The declaration in floatfns.c is incorrect and should read:

    DEFUN ("ldexp", Fldexp, Sldexp, 1, 1, 0,
    ....

In which case it will need to be changed and the manual will need to be
changed accordingly.


OR: `exp' is supposed to have a default value (0 say), when the code in
floatfns.c should be changed accordingly.  In this case it might still
be a good idea to update the manual to explain that it has this value.





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

* bug#20185: 24.4.50; problem with built-in function "ldexp" Emacs Ver: 24.4.50.1
  2015-03-23 20:29 bug#20185: 24.4.50; problem with built-in function "ldexp" Emacs Ver: 24.4.50.1 Bernard Hurley
@ 2015-03-24 18:57 ` Paul Eggert
  0 siblings, 0 replies; 2+ messages in thread
From: Paul Eggert @ 2015-03-24 18:57 UTC (permalink / raw)
  To: Bernard Hurley; +Cc: 20185-done

[-- Attachment #1: Type: text/plain, Size: 133 bytes --]

Thanks for catching that bug.  While fixing it I noticed some related 
minor glitches with ldexp, so I installed the attached patch.

[-- Attachment #2: 0001-Fix-minor-ldexp-issues.patch --]
[-- Type: text/x-patch, Size: 3162 bytes --]

From 68a58397370381fd9b5d08f5cc16a5afa2b5fd26 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 24 Mar 2015 11:42:53 -0700
Subject: [PATCH] Fix minor ldexp issues

* doc/lispref/numbers.texi (Float Basics): Improve ldexp documentation.
* src/floatfns.c (Fldexp): Require 2 args.  Avoid undefined behavior
if the exponent is out of 'int' range.  Improve documentation.
Fixes: bug#20185
---
 doc/lispref/ChangeLog    |  4 ++++
 doc/lispref/numbers.texi | 12 +++++++++---
 src/ChangeLog            |  7 +++++++
 src/floatfns.c           | 10 +++++-----
 4 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog
index a546306..9b1bbb3 100644
--- a/doc/lispref/ChangeLog
+++ b/doc/lispref/ChangeLog
@@ -1,3 +1,7 @@
+2015-03-24  Paul Eggert  <eggert@cs.ucla.edu>
+
+	* numbers.texi (Float Basics): Improve ldexp documentation.
+
 2015-03-23  Eli Zaretskii  <eliz@gnu.org>
 
 	* commands.texi (Event Input Misc): Fix incorrect usage of @code.
diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi
index 8d1d3a7..7b4a0a6 100644
--- a/doc/lispref/numbers.texi
+++ b/doc/lispref/numbers.texi
@@ -265,9 +265,15 @@ If @var{x} is a NaN, then @var{s} is also a NaN@.
 If @var{x} is zero, then @var{e} is 0.
 @end defun
 
-@defun ldexp sig &optional exp
-This function returns a floating-point number corresponding to the
-significand @var{sig} and exponent @var{exp}.
+@defun ldexp s e
+Given a numeric significand @var{s} and an integer exponent @var{e},
+this function returns the floating point number
+@ifnottex
+@var{s} * 2**@var{e}.
+@end ifnottex
+@tex
+@math{s 2^e}.
+@end tex
 @end defun
 
 @defun copysign x1 x2
diff --git a/src/ChangeLog b/src/ChangeLog
index 815c117..23f125c 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,10 @@
+2015-03-24  Paul Eggert  <eggert@cs.ucla.edu>
+
+	Fix minor ldexp issues
+	* floatfns.c (Fldexp): Require 2 args.  Avoid undefined behavior
+	if the exponent is out of 'int' range.  Improve documentation.
+	Fixes: bug#20185
+
 2015-03-24  Daniel Colascione  <dancol@dancol.org>
 
 	* process.c (Fprocess_running_child_p): Return number identifier of
diff --git a/src/floatfns.c b/src/floatfns.c
index c68b9bd..072e857 100644
--- a/src/floatfns.c
+++ b/src/floatfns.c
@@ -185,14 +185,14 @@ If X is zero, both parts (SGNFCAND and EXP) are zero.  */)
   return Fcons (make_float (sgnfcand), make_number (exponent));
 }
 
-DEFUN ("ldexp", Fldexp, Sldexp, 1, 2, 0,
-       doc: /* Construct number X from significand SGNFCAND and exponent EXP.
-Returns the floating point value resulting from multiplying SGNFCAND
-(the significand) by 2 raised to the power of EXP (the exponent).   */)
+DEFUN ("ldexp", Fldexp, Sldexp, 2, 2, 0,
+       doc: /* Return X * 2**EXP, as a floating point number.
+EXP must be an integer.   */)
   (Lisp_Object sgnfcand, Lisp_Object exponent)
 {
   CHECK_NUMBER (exponent);
-  return make_float (ldexp (XFLOATINT (sgnfcand), XINT (exponent)));
+  int e = min (max (INT_MIN, XINT (exponent)), INT_MAX);
+  return make_float (ldexp (XFLOATINT (sgnfcand), e));
 }
 \f
 DEFUN ("exp", Fexp, Sexp, 1, 1, 0,
-- 
2.1.0


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

end of thread, other threads:[~2015-03-24 18:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-23 20:29 bug#20185: 24.4.50; problem with built-in function "ldexp" Emacs Ver: 24.4.50.1 Bernard Hurley
2015-03-24 18:57 ` Paul Eggert

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