From 37ca9077224a3c0e1c2051a47c58148d826812e8 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 19 Mar 2014 14:09:08 -0700 Subject: [PATCH] Fix porting inconsistency about rounding to even. * doc/lispref/numbers.texi (Numeric Conversions, Rounding Operations): Document that 'round' and 'fround' round to even. * src/floatfns.c (emacs_rint) [!HAVE_RINT]: Round to even. This way, the unusual !HAVE_RINT case acts like the usual HAVE_RINT case, and we can fix the documentation accordingly. --- doc/lispref/ChangeLog | 6 ++++++ doc/lispref/numbers.texi | 4 ++-- src/ChangeLog | 7 +++++++ src/floatfns.c | 4 +++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 2749a521c9a..25c4744c855 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog @@ -1,3 +1,9 @@ +2014-03-19 Paul Eggert + + Fix porting inconsistency about rounding to even. + * numbers.texi (Numeric Conversions, Rounding Operations): + Document that 'round' and 'fround' round to even. + 2014-03-18 Juanma Barranquero * customize.texi (Variable Definitions): Recommend avoiding diff --git a/doc/lispref/numbers.texi b/doc/lispref/numbers.texi index 1758a44baab..5526ea0860a 100644 --- a/doc/lispref/numbers.texi +++ b/doc/lispref/numbers.texi @@ -534,8 +534,7 @@ This returns @var{number}, converted to an integer by rounding upward @defun round number &optional divisor This returns @var{number}, converted to an integer by rounding towards the nearest integer. Rounding a value equidistant between two integers -may choose the integer closer to zero, or it may prefer an even integer, -depending on your machine. +returns the even integer. @example (round 1.2) @@ -803,6 +802,7 @@ returns that value as a floating-point number. @defun fround float This function rounds @var{float} to the nearest integral value, and returns that value as a floating-point number. +Rounding a value equidistant between two integers returns the even integer. @end defun @node Bitwise Operations diff --git a/src/ChangeLog b/src/ChangeLog index 94859f1f8fd..06e4c3291b5 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2014-03-19 Paul Eggert + + Fix porting inconsistency about rounding to even. + * floatfns.c (emacs_rint) [!HAVE_RINT]: Round to even. + This way, the unusual !HAVE_RINT case acts like the usual + HAVE_RINT case, and we can fix the documentation accordingly. + 2014-03-19 Eli Zaretskii * w32fns.c (reset_modifiers): Zero out keystate[] before using it. diff --git a/src/floatfns.c b/src/floatfns.c index 4de5f480259..ac0447ce6d6 100644 --- a/src/floatfns.c +++ b/src/floatfns.c @@ -428,7 +428,9 @@ round2 (EMACS_INT i1, EMACS_INT i2) static double emacs_rint (double d) { - return floor (d + 0.5); + double d1 = d + 0.5; + double r = floor (d1); + return r - (r == d1 && fmod (r, 2) != 0); } #endif -- 2.39.2