From 025adce2cf43f4ce9f3c543c1b8973541e1414d2 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Thu, 5 Jul 2018 13:19:32 -0600 Subject: [PATCH] Make abs handle bignums * src/floatfns.c (Fabs): Handle bignums. * test/src/floatfns-tests.el (bignum-abs): New test. --- src/floatfns.c | 19 +++++++++++++++++-- test/src/floatfns-tests.el | 4 ++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/floatfns.c b/src/floatfns.c index bd3f2dec80a..6d7fc1452d3 100644 --- a/src/floatfns.c +++ b/src/floatfns.c @@ -275,9 +275,24 @@ DEFUN ("abs", Fabs, Sabs, 1, 1, 0, doc: /* Return the absolute value of ARG. */) (register Lisp_Object arg) { - CHECK_FIXNUM_OR_FLOAT (arg); + CHECK_NUMBER (arg); - if (FLOATP (arg)) + if (BIGNUMP (arg)) + { + mpz_t val; + mpz_init (val); + mpz_abs (val, XBIGNUM (arg)->value); + arg = make_number (val); + mpz_clear (val); + } + else if (FIXNUMP (arg) && XINT (arg) == MOST_NEGATIVE_FIXNUM) + { + mpz_t val; + mpz_init_set_si (val, - MOST_NEGATIVE_FIXNUM); + arg = make_number (val); + mpz_clear (val); + } + else if (FLOATP (arg)) arg = make_float (fabs (XFLOAT_DATA (arg))); else if (XINT (arg) < 0) XSETINT (arg, - XINT (arg)); diff --git a/test/src/floatfns-tests.el b/test/src/floatfns-tests.el index c87445b6bd2..0911ff46515 100644 --- a/test/src/floatfns-tests.el +++ b/test/src/floatfns-tests.el @@ -38,4 +38,8 @@ (should (eql (float (+ most-positive-fixnum 1)) (+ (float most-positive-fixnum) 1)))) +(ert-deftest bignum-abs () + (should (= most-positive-fixnum + (- (abs most-negative-fixnum) 1)))) + (provide 'floatfns-tests) -- 2.39.2