summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2018-07-05 13:17:36 -0600
committerTom Tromey <tom@tromey.com>2018-07-12 22:12:27 -0600
commit872faabbd8cb0f5518777b2d4fe7de187f684a92 (patch)
treeb09cc3fa24e232c156f7991008d080fd5c77d860
parentd14808cd271abf6a723bf495a6a01c14d18b5893 (diff)
downloademacs-872faabbd8cb0f5518777b2d4fe7de187f684a92.tar.gz
emacs-872faabbd8cb0f5518777b2d4fe7de187f684a92.tar.bz2
emacs-872faabbd8cb0f5518777b2d4fe7de187f684a92.zip
Allow conversion of bignums to floats
* src/floatfns.c (extract_float, Ffloat): Handle bignums. * src/lisp.h (XFLOATINT): Handle bignums. * test/src/floatfns-tests.el (bignum-to-float): New test.
-rw-r--r--src/floatfns.c6
-rw-r--r--src/lisp.h2
-rw-r--r--test/src/floatfns-tests.el4
3 files changed, 10 insertions, 2 deletions
diff --git a/src/floatfns.c b/src/floatfns.c
index 766044ba35c..bd3f2dec80a 100644
--- a/src/floatfns.c
+++ b/src/floatfns.c
@@ -67,7 +67,7 @@ CHECK_FLOAT (Lisp_Object x)
double
extract_float (Lisp_Object num)
{
- CHECK_FIXNUM_OR_FLOAT (num);
+ CHECK_NUMBER (num);
return XFLOATINT (num);
}
@@ -289,8 +289,10 @@ DEFUN ("float", Ffloat, Sfloat, 1, 1, 0,
doc: /* Return the floating point number equal to ARG. */)
(register Lisp_Object arg)
{
- CHECK_FIXNUM_OR_FLOAT (arg);
+ CHECK_NUMBER (arg);
+ if (BIGNUMP (arg))
+ return make_float (mpz_get_d (XBIGNUM (arg)->value));
if (FIXNUMP (arg))
return make_float ((double) XINT (arg));
else /* give 'em the same float back */
diff --git a/src/lisp.h b/src/lisp.h
index be679320494..63b057073d0 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -2919,6 +2919,8 @@ CHECK_FIXNAT (Lisp_Object x)
INLINE double
XFLOATINT (Lisp_Object n)
{
+ if (BIGNUMP (n))
+ return mpz_get_d (XBIGNUM (n)->value);
return FLOATP (n) ? XFLOAT_DATA (n) : XINT (n);
}
diff --git a/test/src/floatfns-tests.el b/test/src/floatfns-tests.el
index cb173eea76d..c87445b6bd2 100644
--- a/test/src/floatfns-tests.el
+++ b/test/src/floatfns-tests.el
@@ -34,4 +34,8 @@
(should-error (ftruncate 0) :type 'wrong-type-argument)
(should-error (fround 0) :type 'wrong-type-argument))
+(ert-deftest bignum-to-float ()
+ (should (eql (float (+ most-positive-fixnum 1))
+ (+ (float most-positive-fixnum) 1))))
+
(provide 'floatfns-tests)