Commit 202bd7bf authored by Paul Eggert's avatar Paul Eggert
Browse files

Fix logb on zero, infinite, NaN args

Change logb to return -infinity, +infinity, and NaN respectively.
Formerly logb returned an extreme fixnum to represent
infinity, but this is no longer the right thing to do now that
we have bignums and there is no extreme integer.
* doc/lispref/numbers.texi (Float Basics), etc/NEWS: Document.
* src/floatfns.c (Flogb): Implement this.
parent b0b483d7
Pipeline #470 failed with stage
in 22 minutes and 9 seconds
...@@ -313,14 +313,18 @@ and returns the result. @var{x1} and @var{x2} must be floating point. ...@@ -313,14 +313,18 @@ and returns the result. @var{x1} and @var{x2} must be floating point.
@defun logb x @defun logb x
This function returns the binary exponent of @var{x}. More This function returns the binary exponent of @var{x}. More
precisely, the value is the logarithm base 2 of @math{|x|}, rounded precisely, if @var{x} is finite and nonzero, the value is the
down to an integer. logarithm base 2 of @math{|x|}, rounded down to an integer.
If @var{x} is zero, infinite, or a NaN, the value is minus infinity,
plus infinity, or a NaN respectively.
@example @example
(logb 10) (logb 10)
@result{} 3 @result{} 3
(logb 10.0e20) (logb 10.0e20)
@result{} 69 @result{} 69
(logb 0)
@result{} -1.0e+INF
@end example @end example
@end defun @end defun
......
...@@ -1112,6 +1112,10 @@ old-style backquotes as new-style, bind the new variable ...@@ -1112,6 +1112,10 @@ old-style backquotes as new-style, bind the new variable
integer, Emacs now signals an error if the number is too large for the integer, Emacs now signals an error if the number is too large for the
implementation to format. implementation to format.
** logb now returns infinity when given an infinite or zero argument,
and returns a NaN when given a NaN. Formerly, it returned an extreme
fixnum for such arguments.
--- ---
** Some functions and variables obsolete since Emacs 22 have been removed: ** Some functions and variables obsolete since Emacs 22 have been removed:
archive-mouse-extract, assoc-ignore-case, assoc-ignore-representation, archive-mouse-extract, assoc-ignore-case, assoc-ignore-representation,
......
...@@ -306,27 +306,22 @@ This is the same as the exponent of a float. */) ...@@ -306,27 +306,22 @@ This is the same as the exponent of a float. */)
if (FLOATP (arg)) if (FLOATP (arg))
{ {
double f = XFLOAT_DATA (arg); double f = XFLOAT_DATA (arg);
if (f == 0) if (f == 0)
value = MOST_NEGATIVE_FIXNUM; return make_float (-HUGE_VAL);
else if (isfinite (f)) if (!isfinite (f))
{ return f < 0 ? make_float (-f) : arg;
int ivalue; int ivalue;
frexp (f, &ivalue); frexp (f, &ivalue);
value = ivalue - 1; value = ivalue - 1;
}
else
value = MOST_POSITIVE_FIXNUM;
} }
else if (BIGNUMP (arg)) else if (!FIXNUMP (arg))
value = mpz_sizeinbase (XBIGNUM (arg)->value, 2) - 1; value = mpz_sizeinbase (XBIGNUM (arg)->value, 2) - 1;
else else
{ {
eassert (FIXNUMP (arg)); EMACS_INT i = XFIXNUM (arg);
EMACS_INT i = eabs (XFIXNUM (arg)); if (i == 0)
value = (i == 0 return make_float (-HUGE_VAL);
? MOST_NEGATIVE_FIXNUM value = EMACS_UINT_WIDTH - 1 - ecount_leading_zeros (eabs (i));
: EMACS_UINT_WIDTH - 1 - ecount_leading_zeros (i));
} }
return make_fixnum (value); return make_fixnum (value);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment