2007-09-06 04:25:08 +00:00
|
|
|
@c -*-texinfo-*-
|
|
|
|
@c This is part of the GNU Emacs Lisp Reference Manual.
|
2020-01-01 00:19:43 +00:00
|
|
|
@c Copyright (C) 1990--1995, 1998--1999, 2001--2020 Free Software
|
2013-01-01 09:11:05 +00:00
|
|
|
@c Foundation, Inc.
|
2007-09-06 04:25:08 +00:00
|
|
|
@c See the file elisp.texi for copying conditions.
|
2012-05-27 01:34:14 +00:00
|
|
|
@node Numbers
|
2007-09-06 04:25:08 +00:00
|
|
|
@chapter Numbers
|
|
|
|
@cindex integers
|
|
|
|
@cindex numbers
|
|
|
|
|
|
|
|
GNU Emacs supports two numeric data types: @dfn{integers} and
|
2014-03-18 04:03:59 +00:00
|
|
|
@dfn{floating-point numbers}. Integers are whole numbers such as
|
|
|
|
@minus{}3, 0, 7, 13, and 511. Floating-point numbers are numbers with
|
|
|
|
fractional parts, such as @minus{}4.5, 0.0, and 2.71828. They can
|
|
|
|
also be expressed in exponential notation: @samp{1.5e2} is the same as
|
|
|
|
@samp{150.0}; here, @samp{e2} stands for ten to the second power, and
|
2018-07-09 05:10:53 +00:00
|
|
|
that is multiplied by 1.5. Integer computations are exact.
|
|
|
|
Floating-point computations often involve rounding errors, as the
|
|
|
|
numbers have a fixed amount of precision.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@menu
|
|
|
|
* Integer Basics:: Representation and range of integers.
|
Untabify doc/lispref/*.texi.
* abbrevs.texi, commands.texi, compile.texi, debugging.texi:
* display.texi, edebug.texi, elisp.texi, eval.texi, files.texi:
* frames.texi, functions.texi, internals.texi, keymaps.texi:
* loading.texi, minibuf.texi, numbers.texi, os.texi, processes.texi:
* searching.texi, sequences.texi, strings.texi, syntax.texi:
* text.texi, tips.texi, vol1.texi, vol2.texi, windows.texi:
Untabify Texinfo files.
2010-06-23 03:36:56 +00:00
|
|
|
* Float Basics:: Representation and range of floating point.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Predicates on Numbers:: Testing for numbers.
|
|
|
|
* Comparison of Numbers:: Equality and inequality predicates.
|
Untabify doc/lispref/*.texi.
* abbrevs.texi, commands.texi, compile.texi, debugging.texi:
* display.texi, edebug.texi, elisp.texi, eval.texi, files.texi:
* frames.texi, functions.texi, internals.texi, keymaps.texi:
* loading.texi, minibuf.texi, numbers.texi, os.texi, processes.texi:
* searching.texi, sequences.texi, strings.texi, syntax.texi:
* text.texi, tips.texi, vol1.texi, vol2.texi, windows.texi:
Untabify Texinfo files.
2010-06-23 03:36:56 +00:00
|
|
|
* Numeric Conversions:: Converting float to integer and vice versa.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Arithmetic Operations:: How to add, subtract, multiply and divide.
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
* Rounding Operations:: Explicitly rounding floating-point numbers.
|
2007-09-06 04:25:08 +00:00
|
|
|
* Bitwise Operations:: Logical and, or, not, shifting.
|
|
|
|
* Math Functions:: Trig, exponential and logarithmic functions.
|
|
|
|
* Random Numbers:: Obtaining random integers, predictable or not.
|
|
|
|
@end menu
|
|
|
|
|
|
|
|
@node Integer Basics
|
|
|
|
@section Integer Basics
|
|
|
|
|
2018-03-09 04:55:55 +00:00
|
|
|
The Lisp reader reads an integer as a nonempty sequence
|
|
|
|
of decimal digits with optional initial sign and optional
|
2018-03-29 17:16:29 +00:00
|
|
|
final period.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
1 ; @r{The integer 1.}
|
|
|
|
1. ; @r{The integer 1.}
|
|
|
|
+1 ; @r{Also the integer 1.}
|
|
|
|
-1 ; @r{The integer @minus{}1.}
|
|
|
|
0 ; @r{The integer 0.}
|
|
|
|
-0 ; @r{The integer 0.}
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@cindex integers in specific radix
|
|
|
|
@cindex radix for reading an integer
|
|
|
|
@cindex base for reading an integer
|
|
|
|
@cindex hex numbers
|
|
|
|
@cindex octal numbers
|
|
|
|
@cindex reading numbers in hex, octal, and binary
|
2018-03-23 20:49:58 +00:00
|
|
|
The syntax for integers in bases other than 10 consists of @samp{#}
|
|
|
|
followed by a radix indication followed by one or more digits. The
|
|
|
|
radix indications are @samp{b} for binary, @samp{o} for octal,
|
|
|
|
@samp{x} for hex, and @samp{@var{radix}r} for radix @var{radix}.
|
|
|
|
Thus, @samp{#b@var{integer}} reads
|
2007-09-06 04:25:08 +00:00
|
|
|
@var{integer} in binary, and @samp{#@var{radix}r@var{integer}} reads
|
|
|
|
@var{integer} in radix @var{radix}. Allowed values of @var{radix} run
|
2018-03-23 20:49:58 +00:00
|
|
|
from 2 to 36, and allowed digits are the first @var{radix} characters
|
|
|
|
taken from @samp{0}--@samp{9}, @samp{A}--@samp{Z}.
|
|
|
|
Letter case is ignored and there is no initial sign or final period.
|
|
|
|
For example:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
#b101100 @result{} 44
|
|
|
|
#o54 @result{} 44
|
|
|
|
#x2c @result{} 44
|
|
|
|
#24r1k @result{} 44
|
|
|
|
@end example
|
|
|
|
|
|
|
|
To understand how various functions work on integers, especially the
|
|
|
|
bitwise operators (@pxref{Bitwise Operations}), it is often helpful to
|
|
|
|
view the numbers in their binary form.
|
|
|
|
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
In binary, the decimal integer 5 looks like this:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}000101
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
2018-08-21 14:56:47 +00:00
|
|
|
(The ellipsis @samp{@dots{}} stands for a conceptually infinite number
|
|
|
|
of bits that match the leading bit; here, an infinite number of 0
|
|
|
|
bits. Later examples also use this @samp{@dots{}} notation.)
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The integer @minus{}1 looks like this:
|
|
|
|
|
|
|
|
@example
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}111111
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
@cindex two's complement
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@minus{}1 is represented as all ones. (This is called @dfn{two's
|
2007-09-06 04:25:08 +00:00
|
|
|
complement} notation.)
|
|
|
|
|
2014-03-19 21:21:01 +00:00
|
|
|
Subtracting 4 from @minus{}1 returns the negative integer @minus{}5.
|
|
|
|
In binary, the decimal integer 4 is 100. Consequently,
|
2007-09-06 04:25:08 +00:00
|
|
|
@minus{}5 looks like this:
|
|
|
|
|
|
|
|
@example
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}111011
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
|
|
|
|
Many of the functions described in this chapter accept markers for
|
|
|
|
arguments in place of numbers. (@xref{Markers}.) Since the actual
|
|
|
|
arguments to such functions may be either numbers or markers, we often
|
|
|
|
give these arguments the name @var{number-or-marker}. When the argument
|
|
|
|
value is a marker, its position value is used and its buffer is ignored.
|
|
|
|
|
2020-02-17 21:36:50 +00:00
|
|
|
In Emacs Lisp, text characters are represented by integers. Any
|
|
|
|
integer between zero and the value of @code{(max-char)}, inclusive, is
|
|
|
|
considered to be valid as a character. @xref{Character Codes}.
|
|
|
|
|
|
|
|
Integers in Emacs Lisp are not limited to the machine word size.
|
|
|
|
Under the hood, though, there are two kinds of integers: smaller ones,
|
|
|
|
called @dfn{fixnums}, and larger ones, called @dfn{bignums}. Although
|
|
|
|
Emacs Lisp code ordinarily should not depend on whether an integer is
|
|
|
|
a fixnum or a bignum, older Emacs versions support only fixnums, some
|
|
|
|
functions in Emacs still accept only fixnums, and older Emacs Lisp
|
|
|
|
code may have trouble when given bignums. For example, while older
|
|
|
|
Emacs Lisp code could safely compare integers for numeric equality
|
|
|
|
with @code{eq}, the presence of bignums means that equality predicates
|
|
|
|
like @code{eql} and @code{=} should now be used to compare integers.
|
|
|
|
|
|
|
|
The range of values for bignums is limited by the amount of main
|
|
|
|
memory, by machine characteristics such as the size of the word used
|
|
|
|
to represent a bignum's exponent, and by the @code{integer-width}
|
|
|
|
variable. These limits are typically much more generous than the
|
|
|
|
limits for fixnums. A bignum is never numerically equal to a fixnum;
|
2020-02-17 21:54:07 +00:00
|
|
|
Emacs always represents an integer in fixnum range as a fixnum, not a
|
|
|
|
bignum.
|
2020-02-17 21:36:50 +00:00
|
|
|
|
|
|
|
The range of values for a fixnum depends on the machine. The
|
|
|
|
minimum range is @minus{}536,870,912 to 536,870,911 (30 bits; i.e.,
|
|
|
|
@ifnottex
|
|
|
|
@minus{}2**29
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{-2^{29}}
|
|
|
|
@end tex
|
|
|
|
to
|
|
|
|
@ifnottex
|
|
|
|
2**29 @minus{} 1),
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{2^{29}-1}),
|
|
|
|
@end tex
|
|
|
|
but many machines provide a wider range.
|
|
|
|
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@cindex largest fixnum
|
|
|
|
@cindex maximum fixnum
|
2007-09-06 04:25:08 +00:00
|
|
|
@defvar most-positive-fixnum
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
The value of this variable is the greatest ``small'' integer that Emacs
|
2018-07-09 05:10:53 +00:00
|
|
|
Lisp can handle. Typical values are
|
2014-03-18 04:03:59 +00:00
|
|
|
@ifnottex
|
|
|
|
2**29 @minus{} 1
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{2^{29}-1}
|
|
|
|
@end tex
|
|
|
|
on 32-bit and
|
|
|
|
@ifnottex
|
|
|
|
2**61 @minus{} 1
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{2^{61}-1}
|
|
|
|
@end tex
|
|
|
|
on 64-bit platforms.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defvar
|
|
|
|
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@cindex smallest fixnum
|
|
|
|
@cindex minimum fixnum
|
2007-09-06 04:25:08 +00:00
|
|
|
@defvar most-negative-fixnum
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
The value of this variable is the numerically least ``small'' integer
|
|
|
|
that Emacs Lisp can handle. It is negative. Typical values are
|
2014-03-18 04:03:59 +00:00
|
|
|
@ifnottex
|
|
|
|
@minus{}2**29
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{-2^{29}}
|
|
|
|
@end tex
|
|
|
|
on 32-bit and
|
|
|
|
@ifnottex
|
|
|
|
@minus{}2**61
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{-2^{61}}
|
|
|
|
@end tex
|
|
|
|
on 64-bit platforms.
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@cindex bignum range
|
|
|
|
@cindex integer range
|
2018-08-21 14:56:47 +00:00
|
|
|
@cindex number of bignum bits, limit on
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@defvar integer-width
|
2019-06-04 15:13:20 +00:00
|
|
|
The value of this variable is a nonnegative integer that controls
|
|
|
|
whether Emacs signals a range error when a large integer would be
|
|
|
|
calculated. Integers with absolute values less than
|
2018-08-21 17:05:31 +00:00
|
|
|
@ifnottex
|
|
|
|
2**@var{n},
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{2^{n}},
|
|
|
|
@end tex
|
2019-06-04 15:13:20 +00:00
|
|
|
where @var{n} is this variable's value, do not signal a range error.
|
|
|
|
Attempts to create larger integers typically signal a range error,
|
|
|
|
although there might be no signal if a larger integer can be created cheaply.
|
|
|
|
Setting this variable to a large number can be costly if a computation
|
2018-08-21 14:56:47 +00:00
|
|
|
creates huge integers.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@node Float Basics
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
@section Floating-Point Basics
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-01-21 16:04:55 +00:00
|
|
|
@cindex @acronym{IEEE} floating point
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
Floating-point numbers are useful for representing numbers that are
|
2014-03-18 04:03:59 +00:00
|
|
|
not integral. The range of floating-point numbers is
|
|
|
|
the same as the range of the C data type @code{double} on the machine
|
2019-11-10 23:01:06 +00:00
|
|
|
you are using. On all computers supported by Emacs, this is
|
|
|
|
@acronym{IEEE} binary64 floating point format, which is standardized by
|
|
|
|
@url{https://standards.ieee.org/standard/754-2019.html,,IEEE Std 754-2019}
|
|
|
|
and is discussed further in David Goldberg's paper
|
|
|
|
``@url{https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html,
|
|
|
|
What Every Computer Scientist Should Know About Floating-Point Arithmetic}''.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
The read syntax for floating-point numbers requires either a decimal
|
2014-03-18 04:03:59 +00:00
|
|
|
point, an exponent, or both. Optional signs (@samp{+} or @samp{-})
|
|
|
|
precede the number and its exponent. For example, @samp{1500.0},
|
|
|
|
@samp{+15e2}, @samp{15.0e+2}, @samp{+1500000e-3}, and @samp{.15e4} are
|
|
|
|
five ways of writing a floating-point number whose value is 1500.
|
|
|
|
They are all equivalent. Like Common Lisp, Emacs Lisp requires at
|
|
|
|
least one digit after any decimal point in a floating-point number;
|
|
|
|
@samp{1500.} is an integer, not a floating-point number.
|
|
|
|
|
|
|
|
Emacs Lisp treats @code{-0.0} as numerically equal to ordinary zero
|
2018-07-19 20:29:28 +00:00
|
|
|
with respect to numeric comparisons like @code{=}. This follows the
|
2014-03-18 04:03:59 +00:00
|
|
|
@acronym{IEEE} floating-point standard, which says @code{-0.0} and
|
|
|
|
@code{0.0} are numerically equal even though other operations can
|
|
|
|
distinguish them.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@cindex positive infinity
|
|
|
|
@cindex negative infinity
|
|
|
|
@cindex infinity
|
|
|
|
@cindex NaN
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
The @acronym{IEEE} floating-point standard supports positive
|
|
|
|
infinity and negative infinity as floating-point values. It also
|
Restore some of the quoting in the manuals
* doc/lispref/windows.texi (Coordinates and Windows)
(Coordinates and Windows):
* doc/lispref/variables.texi (Lexical Binding)
(File Local Variables):
* doc/lispref/text.texi (Format Properties):
* doc/lispref/symbols.texi (Symbol Components):
* doc/lispref/strings.texi (Creating Strings):
* doc/lispref/sequences.texi (Sequence Functions):
* doc/lispref/searching.texi (Regexp Special, Regexp Search)
(Search and Replace):
* doc/lispref/processes.texi (Bindat Spec):
* doc/lispref/os.texi (Idle Timers):
* doc/lispref/objects.texi (Basic Char Syntax):
* doc/lispref/numbers.texi (Float Basics, Random Numbers):
* doc/lispref/nonascii.texi (Character Properties):
* doc/lispref/modes.texi (Major Mode Conventions, Mode Hooks)
(Mode Line Variables):
* doc/lispref/minibuf.texi (Text from Minibuffer):
* doc/lispref/loading.texi (Autoload):
* doc/lispref/keymaps.texi (Controlling Active Maps):
* doc/lispref/frames.texi (Frame Layout, Size and Position)
(Size Parameters, Implied Frame Resizing):
* doc/lispref/files.texi (Changing Files, Magic File Names):
* doc/lispref/eval.texi (Self-Evaluating Forms):
* doc/lispref/display.texi (Progress, Abstract Display)
(Abstract Display Example, Bidirectional Display):
* doc/lispref/commands.texi (Event Mod):
* doc/emacs/windows.texi (Displaying Buffers):
* doc/emacs/trouble.texi (Bug Criteria, Checklist):
* doc/emacs/text.texi (Enriched Text):
* doc/emacs/programs.texi (MixedCase Words):
* doc/emacs/picture-xtra.texi (Insert in Picture)
(Tabs in Picture):
* doc/emacs/misc.texi (Emacs Server, Printing):
* doc/emacs/mini.texi (Minibuffer History):
* doc/emacs/maintaining.texi (Old Revisions, VC Change Log)
(Pulling / Pushing):
* doc/emacs/killing.texi (Yanking, Cut and Paste, Clipboard):
* doc/emacs/help.texi (Help, Help Echo):
* doc/emacs/glossary.texi (Glossary):
* doc/emacs/frames.texi (Mouse Commands, Creating Frames)
(Frame Commands):
* doc/emacs/files.texi (Reverting, Saving, Directories):
* doc/emacs/entering.texi (Exiting):
* doc/emacs/emacs.texi (Top):
* doc/emacs/cmdargs.texi (Window Size X, Icons X):
* doc/emacs/anti.texi (Antinews): Restore quoting of text where
appropriate or replace quoting with @dfn.
* doc/misc/ediff.texi (Window and Frame Configuration):
* doc/lispref/processes.texi (Network Feature Testing):
* doc/lispref/display.texi (Display Margins): Quote the phrase
after "a.k.a." where appropriate.
2015-09-16 09:56:45 +00:00
|
|
|
provides for a class of values called NaN, or ``not a number'';
|
2012-01-21 16:04:55 +00:00
|
|
|
numerical functions return such values in cases where there is no
|
2014-03-18 04:03:59 +00:00
|
|
|
correct answer. For example, @code{(/ 0.0 0.0)} returns a NaN@.
|
Fix bug with eql etc. on NaNs
Fix a bug where eql, sxhash-eql, memql, and make-hash-table
were not consistent on NaNs. Likewise for equal,
sxhash-equal, member, and make-hash-table. Some of these
functions ignored NaN significands, whereas others treated
them as significant. It's more logical to treat significands
as significant, and this typically makes eql a bit more
efficient on floats, with just one integer comparison instead
of one to three floating-point comparisons.
* doc/lispref/numbers.texi (Float Basics): Document that
NaNs are never numerically equal, but might be eql.
* src/fns.c (WORDS_PER_DOUBLE): Move to top level of this file.
(union double_and_words): Now named, and at the top level of this file.
(same_float): New function.
(Fmemql, Feql, internal_equal, cmpfn_eql): Use it, so that
the corresponding functions treat NaNs consistently.
(sxhash_float): Simplify based on above-mentioned changes.
* test/src/fns-tests.el (fns-tests-equality-nan): New test.
2018-07-18 10:16:54 +00:00
|
|
|
A NaN is never numerically equal to any value, not even to itself.
|
2018-07-19 20:29:28 +00:00
|
|
|
NaNs carry a sign and a significand, and non-numeric functions treat
|
|
|
|
two NaNs as equal when their
|
Fix bug with eql etc. on NaNs
Fix a bug where eql, sxhash-eql, memql, and make-hash-table
were not consistent on NaNs. Likewise for equal,
sxhash-equal, member, and make-hash-table. Some of these
functions ignored NaN significands, whereas others treated
them as significant. It's more logical to treat significands
as significant, and this typically makes eql a bit more
efficient on floats, with just one integer comparison instead
of one to three floating-point comparisons.
* doc/lispref/numbers.texi (Float Basics): Document that
NaNs are never numerically equal, but might be eql.
* src/fns.c (WORDS_PER_DOUBLE): Move to top level of this file.
(union double_and_words): Now named, and at the top level of this file.
(same_float): New function.
(Fmemql, Feql, internal_equal, cmpfn_eql): Use it, so that
the corresponding functions treat NaNs consistently.
(sxhash_float): Simplify based on above-mentioned changes.
* test/src/fns-tests.el (fns-tests-equality-nan): New test.
2018-07-18 10:16:54 +00:00
|
|
|
signs and significands agree. Significands of NaNs are
|
2018-08-01 07:49:39 +00:00
|
|
|
machine-dependent, as are the digits in their string representation.
|
Simplify, document, and port floating-point.
The porting part of this patch fixes bugs on non-IEEE platforms
with frexp, ldexp, logb.
* admin/CPP-DEFINES (HAVE_CBRT, HAVE_LOGB, logb): Remove.
* configure.ac (logb, cbrt): Do not check for these functions,
as they are not being used.
* doc/lispref/numbers.texi (Float Basics, Arithmetic Operations, Math Functions):
Document that / and mod (with floating point arguments), along
with asin, acos, log, log10, expt and sqrt, return special values
instead of signaling exceptions.
(Float Basics): Document that logb operates on the absolute value
of its argument.
(Math Functions): Document that (log ARG BASE) also returns NaN if
BASE is negative. Document that (expt X Y) returns NaN if X is a
finite negative number and Y a finite non-integer.
* etc/NEWS: Document NaNs versus signaling-error change.
* src/data.c, src/lisp.h (Qdomain_error, Qsingularity_error, Qunderflow_error):
Now static.
* src/floatfns.c: Simplify discussion of functions that Emacs doesn't
support, by removing commented-out code and briefly listing the
C89 functions excluded. The commented-out stuff was confusing
maintenance, e.g., we thought we needed cbrt but it was commented out.
(logb): Remove decl; no longer needed.
(isfinite): New macro, if not already supplied.
(isnan): Don't replace any existing macro.
(Ffrexp, Fldexp): Define even if !HAVE_COPYSIGN, as frexp and ldexp
are present on all C89 platforms.
(Ffrexp): Do not special-case zero, as frexp does the right thing
for that case.
(Flogb): Do not use logb, as it doesn't have the desired meaning
on hosts that use non-base-2 floating point. Instead, stick with
frexp, which is C89 anyway. Do not pass an infinity or a NaN to
frexp, to avoid getting an unspecified result.
2012-09-11 02:28:27 +00:00
|
|
|
|
2018-07-19 20:29:28 +00:00
|
|
|
When NaNs and signed zeros are involved, non-numeric functions like
|
|
|
|
@code{eql}, @code{equal}, @code{sxhash-eql}, @code{sxhash-equal} and
|
|
|
|
@code{gethash} determine whether values are indistinguishable, not
|
|
|
|
whether they are numerically equal. For example, when @var{x} and
|
|
|
|
@var{y} are the same NaN, @code{(equal x y)} returns @code{t} whereas
|
|
|
|
@code{(= x y)} uses numeric comparison and returns @code{nil};
|
|
|
|
conversely, @code{(equal 0.0 -0.0)} returns @code{nil} whereas
|
|
|
|
@code{(= 0.0 -0.0)} returns @code{t}.
|
Simplify, document, and port floating-point.
The porting part of this patch fixes bugs on non-IEEE platforms
with frexp, ldexp, logb.
* admin/CPP-DEFINES (HAVE_CBRT, HAVE_LOGB, logb): Remove.
* configure.ac (logb, cbrt): Do not check for these functions,
as they are not being used.
* doc/lispref/numbers.texi (Float Basics, Arithmetic Operations, Math Functions):
Document that / and mod (with floating point arguments), along
with asin, acos, log, log10, expt and sqrt, return special values
instead of signaling exceptions.
(Float Basics): Document that logb operates on the absolute value
of its argument.
(Math Functions): Document that (log ARG BASE) also returns NaN if
BASE is negative. Document that (expt X Y) returns NaN if X is a
finite negative number and Y a finite non-integer.
* etc/NEWS: Document NaNs versus signaling-error change.
* src/data.c, src/lisp.h (Qdomain_error, Qsingularity_error, Qunderflow_error):
Now static.
* src/floatfns.c: Simplify discussion of functions that Emacs doesn't
support, by removing commented-out code and briefly listing the
C89 functions excluded. The commented-out stuff was confusing
maintenance, e.g., we thought we needed cbrt but it was commented out.
(logb): Remove decl; no longer needed.
(isfinite): New macro, if not already supplied.
(isnan): Don't replace any existing macro.
(Ffrexp, Fldexp): Define even if !HAVE_COPYSIGN, as frexp and ldexp
are present on all C89 platforms.
(Ffrexp): Do not special-case zero, as frexp does the right thing
for that case.
(Flogb): Do not use logb, as it doesn't have the desired meaning
on hosts that use non-base-2 floating point. Instead, stick with
frexp, which is C89 anyway. Do not pass an infinity or a NaN to
frexp, to avoid getting an unspecified result.
2012-09-11 02:28:27 +00:00
|
|
|
|
2014-03-18 04:03:59 +00:00
|
|
|
Here are read syntaxes for these special floating-point values:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@table @asis
|
2014-03-18 04:03:59 +00:00
|
|
|
@item infinity
|
|
|
|
@samp{1.0e+INF} and @samp{-1.0e+INF}
|
|
|
|
@item not-a-number
|
|
|
|
@samp{0.0e+NaN} and @samp{-0.0e+NaN}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end table
|
|
|
|
|
2014-03-18 04:03:59 +00:00
|
|
|
The following functions are specialized for handling floating-point
|
2012-01-21 16:04:55 +00:00
|
|
|
numbers:
|
|
|
|
|
2014-03-18 04:03:59 +00:00
|
|
|
@defun isnan x
|
|
|
|
This predicate returns @code{t} if its floating-point argument is a NaN,
|
|
|
|
@code{nil} otherwise.
|
|
|
|
@end defun
|
2012-01-21 16:04:55 +00:00
|
|
|
|
2014-03-18 04:03:59 +00:00
|
|
|
@defun frexp x
|
|
|
|
This function returns a cons cell @code{(@var{s} . @var{e})},
|
|
|
|
where @var{s} and @var{e} are respectively the significand and
|
|
|
|
exponent of the floating-point number @var{x}.
|
2012-01-21 16:04:55 +00:00
|
|
|
|
2014-03-19 21:21:01 +00:00
|
|
|
If @var{x} is finite, then @var{s} is a floating-point number between 0.5
|
2014-03-18 04:03:59 +00:00
|
|
|
(inclusive) and 1.0 (exclusive), @var{e} is an integer, and
|
|
|
|
@ifnottex
|
|
|
|
@var{x} = @var{s} * 2**@var{e}.
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{x = s 2^e}.
|
|
|
|
@end tex
|
2014-03-19 21:21:01 +00:00
|
|
|
If @var{x} is zero or infinity, then @var{s} is the same as @var{x}.
|
2015-03-17 23:55:02 +00:00
|
|
|
If @var{x} is a NaN, then @var{s} is also a NaN@.
|
2014-03-19 21:21:01 +00:00
|
|
|
If @var{x} is zero, then @var{e} is 0.
|
2012-01-21 16:04:55 +00:00
|
|
|
@end defun
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2015-03-24 18:42:53 +00:00
|
|
|
@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
|
2012-01-21 16:04:55 +00:00
|
|
|
@end defun
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-01-21 16:04:55 +00:00
|
|
|
@defun copysign x1 x2
|
|
|
|
This function copies the sign of @var{x2} to the value of @var{x1},
|
2014-03-18 04:03:59 +00:00
|
|
|
and returns the result. @var{x1} and @var{x2} must be floating point.
|
2012-01-21 16:04:55 +00:00
|
|
|
@end defun
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2014-03-19 21:21:01 +00:00
|
|
|
@defun logb x
|
2019-12-28 13:19:05 +00:00
|
|
|
This function returns the binary exponent of @var{x}. More precisely,
|
|
|
|
if @var{x} is finite and nonzero, the value is the logarithm base 2 of
|
|
|
|
@math{|x|}, rounded down to an integer. If @var{x} is zero or
|
|
|
|
infinite, the value is infinity; if @var{x} is a NaN, the value is a
|
|
|
|
NaN.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
(logb 10)
|
|
|
|
@result{} 3
|
|
|
|
(logb 10.0e20)
|
|
|
|
@result{} 69
|
2019-01-07 00:23:41 +00:00
|
|
|
(logb 0)
|
|
|
|
@result{} -1.0e+INF
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Predicates on Numbers
|
|
|
|
@section Type Predicates for Numbers
|
|
|
|
@cindex predicates for numbers
|
|
|
|
|
|
|
|
The functions in this section test for numbers, or for a specific
|
|
|
|
type of number. The functions @code{integerp} and @code{floatp} can
|
|
|
|
take any type of Lisp object as argument (they would not be of much
|
|
|
|
use otherwise), but the @code{zerop} predicate requires a number as
|
|
|
|
its argument. See also @code{integer-or-marker-p} and
|
|
|
|
@code{number-or-marker-p}, in @ref{Predicates on Markers}.
|
|
|
|
|
2018-07-09 05:10:53 +00:00
|
|
|
@defun bignump object
|
|
|
|
This predicate tests whether its argument is a large integer, and
|
2018-08-14 23:06:05 +00:00
|
|
|
returns @code{t} if so, @code{nil} otherwise. Unlike small integers,
|
|
|
|
large integers can be @code{=} or @code{eql} even if they are not @code{eq}.
|
2018-07-09 05:10:53 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun fixnump object
|
|
|
|
This predicate tests whether its argument is a small integer, and
|
|
|
|
returns @code{t} if so, @code{nil} otherwise. Small integers can be
|
|
|
|
compared with @code{eq}.
|
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@defun floatp object
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
This predicate tests whether its argument is floating point
|
|
|
|
and returns @code{t} if so, @code{nil} otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun integerp object
|
|
|
|
This predicate tests whether its argument is an integer, and returns
|
|
|
|
@code{t} if so, @code{nil} otherwise.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun numberp object
|
|
|
|
This predicate tests whether its argument is a number (either integer or
|
|
|
|
floating point), and returns @code{t} if so, @code{nil} otherwise.
|
|
|
|
@end defun
|
|
|
|
|
2012-01-29 07:25:22 +00:00
|
|
|
@defun natnump object
|
2007-09-06 04:25:08 +00:00
|
|
|
@cindex natural numbers
|
2012-01-31 01:15:56 +00:00
|
|
|
This predicate (whose name comes from the phrase ``natural number'')
|
2012-01-29 07:25:22 +00:00
|
|
|
tests to see whether its argument is a nonnegative integer, and
|
|
|
|
returns @code{t} if so, @code{nil} otherwise. 0 is considered
|
|
|
|
non-negative.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2014-03-18 04:03:59 +00:00
|
|
|
@findex wholenump
|
|
|
|
@code{wholenump} is a synonym for @code{natnump}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun zerop number
|
|
|
|
This predicate tests whether its argument is zero, and returns @code{t}
|
|
|
|
if so, @code{nil} otherwise. The argument must be a number.
|
|
|
|
|
|
|
|
@code{(zerop x)} is equivalent to @code{(= x 0)}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Comparison of Numbers
|
|
|
|
@section Comparison of Numbers
|
|
|
|
@cindex number comparison
|
|
|
|
@cindex comparing numbers
|
|
|
|
|
|
|
|
To test numbers for numerical equality, you should normally use
|
2018-07-19 20:29:28 +00:00
|
|
|
@code{=} instead of non-numeric comparison predicates like @code{eq},
|
2018-08-09 23:56:53 +00:00
|
|
|
@code{eql} and @code{equal}. Distinct floating-point and large
|
|
|
|
integer objects can be numerically equal. If you use @code{eq} to
|
|
|
|
compare them, you test whether they are the same @emph{object}; if you
|
|
|
|
use @code{eql} or @code{equal}, you test whether their values are
|
2018-07-19 20:29:28 +00:00
|
|
|
@emph{indistinguishable}. In contrast, @code{=} uses numeric
|
|
|
|
comparison, and sometimes returns @code{t} when a non-numeric
|
|
|
|
comparison would return @code{nil} and vice versa. @xref{Float
|
|
|
|
Basics}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
In Emacs Lisp, if two fixnums are numerically equal, they are the
|
|
|
|
same Lisp object. That is, @code{eq} is equivalent to @code{=} on
|
|
|
|
fixnums. It is sometimes convenient to use @code{eq} for comparing
|
|
|
|
an unknown value with a fixnum, because @code{eq} does not report an
|
2012-09-30 09:18:38 +00:00
|
|
|
error if the unknown value is not a number---it accepts arguments of
|
|
|
|
any type. By contrast, @code{=} signals an error if the arguments are
|
|
|
|
not numbers or markers. However, it is better programming practice to
|
|
|
|
use @code{=} if you can, even for comparing integers.
|
|
|
|
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
Sometimes it is useful to compare numbers with @code{eql} or @code{equal},
|
|
|
|
which treat two numbers as equal if they have the same data type (both
|
2007-09-06 04:25:08 +00:00
|
|
|
integers, or both floating point) and the same value. By contrast,
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
@code{=} can treat an integer and a floating-point number as equal.
|
2007-09-06 04:25:08 +00:00
|
|
|
@xref{Equality Predicates}.
|
|
|
|
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
There is another wrinkle: because floating-point arithmetic is not
|
|
|
|
exact, it is often a bad idea to check for equality of floating-point
|
|
|
|
values. Usually it is better to test for approximate equality.
|
2007-09-06 04:25:08 +00:00
|
|
|
Here's a function to do this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(defvar fuzz-factor 1.0e-6)
|
|
|
|
(defun approx-equal (x y)
|
2014-03-19 21:21:01 +00:00
|
|
|
(or (= x y)
|
2007-09-06 04:25:08 +00:00
|
|
|
(< (/ (abs (- x y))
|
|
|
|
(max (abs x) (abs y)))
|
|
|
|
fuzz-factor)))
|
|
|
|
@end example
|
|
|
|
|
2013-12-20 18:41:31 +00:00
|
|
|
@defun = number-or-marker &rest number-or-markers
|
|
|
|
This function tests whether all its arguments are numerically equal,
|
|
|
|
and returns @code{t} if so, @code{nil} otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun eql value1 value2
|
|
|
|
This function acts like @code{eq} except when both arguments are
|
|
|
|
numbers. It compares numbers by type and numeric value, so that
|
|
|
|
@code{(eql 1.0 1)} returns @code{nil}, but @code{(eql 1.0 1.0)} and
|
2018-07-09 05:10:53 +00:00
|
|
|
@code{(eql 1 1)} both return @code{t}. This can be used to compare
|
|
|
|
large integers as well as small ones.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun /= number-or-marker1 number-or-marker2
|
|
|
|
This function tests whether its arguments are numerically equal, and
|
|
|
|
returns @code{t} if they are not, and @code{nil} if they are.
|
|
|
|
@end defun
|
|
|
|
|
2013-12-20 18:41:31 +00:00
|
|
|
@defun < number-or-marker &rest number-or-markers
|
2014-06-08 00:51:10 +00:00
|
|
|
This function tests whether each argument is strictly less than the
|
|
|
|
following argument. It returns @code{t} if so, @code{nil} otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
2013-12-20 18:41:31 +00:00
|
|
|
@defun <= number-or-marker &rest number-or-markers
|
2014-06-08 00:51:10 +00:00
|
|
|
This function tests whether each argument is less than or equal to
|
|
|
|
the following argument. It returns @code{t} if so, @code{nil} otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
2013-12-20 18:41:31 +00:00
|
|
|
@defun > number-or-marker &rest number-or-markers
|
2014-06-08 00:51:10 +00:00
|
|
|
This function tests whether each argument is strictly greater than
|
|
|
|
the following argument. It returns @code{t} if so, @code{nil} otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
2013-12-20 18:41:31 +00:00
|
|
|
@defun >= number-or-marker &rest number-or-markers
|
2014-06-08 00:51:10 +00:00
|
|
|
This function tests whether each argument is greater than or equal to
|
|
|
|
the following argument. It returns @code{t} if so, @code{nil} otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun max number-or-marker &rest numbers-or-markers
|
|
|
|
This function returns the largest of its arguments.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(max 20)
|
|
|
|
@result{} 20
|
|
|
|
(max 1 2.5)
|
|
|
|
@result{} 2.5
|
|
|
|
(max 1 3 2.5)
|
2017-03-06 23:14:32 +00:00
|
|
|
@result{} 3
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun min number-or-marker &rest numbers-or-markers
|
|
|
|
This function returns the smallest of its arguments.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(min -4 1)
|
|
|
|
@result{} -4
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun abs number
|
|
|
|
This function returns the absolute value of @var{number}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Numeric Conversions
|
|
|
|
@section Numeric Conversions
|
|
|
|
@cindex rounding in conversions
|
|
|
|
@cindex number conversions
|
|
|
|
@cindex converting numbers
|
|
|
|
|
|
|
|
To convert an integer to floating point, use the function @code{float}.
|
|
|
|
|
|
|
|
@defun float number
|
|
|
|
This returns @var{number} converted to floating point.
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
If @var{number} is already floating point, @code{float} returns
|
2007-09-06 04:25:08 +00:00
|
|
|
it unchanged.
|
|
|
|
@end defun
|
|
|
|
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
There are four functions to convert floating-point numbers to
|
2012-09-30 09:18:38 +00:00
|
|
|
integers; they differ in how they round. All accept an argument
|
|
|
|
@var{number} and an optional argument @var{divisor}. Both arguments
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
may be integers or floating-point numbers. @var{divisor} may also be
|
2007-09-06 04:25:08 +00:00
|
|
|
@code{nil}. If @var{divisor} is @code{nil} or omitted, these
|
|
|
|
functions convert @var{number} to an integer, or return it unchanged
|
|
|
|
if it already is an integer. If @var{divisor} is non-@code{nil}, they
|
|
|
|
divide @var{number} by @var{divisor} and convert the result to an
|
2014-01-02 19:17:48 +00:00
|
|
|
integer. If @var{divisor} is zero (whether integer or
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
floating point), Emacs signals an @code{arith-error} error.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@defun truncate number &optional divisor
|
|
|
|
This returns @var{number}, converted to an integer by rounding towards
|
|
|
|
zero.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(truncate 1.2)
|
|
|
|
@result{} 1
|
|
|
|
(truncate 1.7)
|
|
|
|
@result{} 1
|
|
|
|
(truncate -1.2)
|
|
|
|
@result{} -1
|
|
|
|
(truncate -1.7)
|
|
|
|
@result{} -1
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun floor number &optional divisor
|
|
|
|
This returns @var{number}, converted to an integer by rounding downward
|
|
|
|
(towards negative infinity).
|
|
|
|
|
|
|
|
If @var{divisor} is specified, this uses the kind of division
|
|
|
|
operation that corresponds to @code{mod}, rounding downward.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(floor 1.2)
|
|
|
|
@result{} 1
|
|
|
|
(floor 1.7)
|
|
|
|
@result{} 1
|
|
|
|
(floor -1.2)
|
|
|
|
@result{} -2
|
|
|
|
(floor -1.7)
|
|
|
|
@result{} -2
|
|
|
|
(floor 5.99 3)
|
|
|
|
@result{} 1
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun ceiling number &optional divisor
|
|
|
|
This returns @var{number}, converted to an integer by rounding upward
|
|
|
|
(towards positive infinity).
|
|
|
|
|
|
|
|
@example
|
|
|
|
(ceiling 1.2)
|
|
|
|
@result{} 2
|
|
|
|
(ceiling 1.7)
|
|
|
|
@result{} 2
|
|
|
|
(ceiling -1.2)
|
|
|
|
@result{} -1
|
|
|
|
(ceiling -1.7)
|
|
|
|
@result{} -1
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@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
|
2014-03-19 21:09:08 +00:00
|
|
|
returns the even integer.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
(round 1.2)
|
|
|
|
@result{} 1
|
|
|
|
(round 1.7)
|
|
|
|
@result{} 2
|
|
|
|
(round -1.2)
|
|
|
|
@result{} -1
|
|
|
|
(round -1.7)
|
|
|
|
@result{} -2
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Arithmetic Operations
|
|
|
|
@section Arithmetic Operations
|
|
|
|
@cindex arithmetic operations
|
|
|
|
|
2012-09-30 09:18:38 +00:00
|
|
|
Emacs Lisp provides the traditional four arithmetic operations
|
|
|
|
(addition, subtraction, multiplication, and division), as well as
|
|
|
|
remainder and modulus functions, and functions to add or subtract 1.
|
|
|
|
Except for @code{%}, each of these functions accepts both integer and
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
floating-point arguments, and returns a floating-point number if any
|
|
|
|
argument is floating point.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@defun 1+ number-or-marker
|
|
|
|
This function returns @var{number-or-marker} plus 1.
|
|
|
|
For example,
|
|
|
|
|
|
|
|
@example
|
|
|
|
(setq foo 4)
|
|
|
|
@result{} 4
|
|
|
|
(1+ foo)
|
|
|
|
@result{} 5
|
|
|
|
@end example
|
|
|
|
|
|
|
|
This function is not analogous to the C operator @code{++}---it does not
|
|
|
|
increment a variable. It just computes a sum. Thus, if we continue,
|
|
|
|
|
|
|
|
@example
|
|
|
|
foo
|
|
|
|
@result{} 4
|
|
|
|
@end example
|
|
|
|
|
|
|
|
If you want to increment the variable, you must use @code{setq},
|
|
|
|
like this:
|
|
|
|
|
|
|
|
@example
|
|
|
|
(setq foo (1+ foo))
|
|
|
|
@result{} 5
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun 1- number-or-marker
|
|
|
|
This function returns @var{number-or-marker} minus 1.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun + &rest numbers-or-markers
|
|
|
|
This function adds its arguments together. When given no arguments,
|
|
|
|
@code{+} returns 0.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(+)
|
|
|
|
@result{} 0
|
|
|
|
(+ 1)
|
|
|
|
@result{} 1
|
|
|
|
(+ 1 2 3 4)
|
|
|
|
@result{} 10
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun - &optional number-or-marker &rest more-numbers-or-markers
|
|
|
|
The @code{-} function serves two purposes: negation and subtraction.
|
|
|
|
When @code{-} has a single argument, the value is the negative of the
|
|
|
|
argument. When there are multiple arguments, @code{-} subtracts each of
|
|
|
|
the @var{more-numbers-or-markers} from @var{number-or-marker},
|
|
|
|
cumulatively. If there are no arguments, the result is 0.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(- 10 1 2 3 4)
|
|
|
|
@result{} 0
|
|
|
|
(- 10)
|
|
|
|
@result{} -10
|
|
|
|
(-)
|
|
|
|
@result{} 0
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun * &rest numbers-or-markers
|
|
|
|
This function multiplies its arguments together, and returns the
|
|
|
|
product. When given no arguments, @code{*} returns 1.
|
|
|
|
|
|
|
|
@example
|
|
|
|
(*)
|
|
|
|
@result{} 1
|
|
|
|
(* 1)
|
|
|
|
@result{} 1
|
|
|
|
(* 1 2 3 4)
|
|
|
|
@result{} 24
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
2015-10-21 01:16:47 +00:00
|
|
|
@defun / number &rest divisors
|
|
|
|
With one or more @var{divisors}, this function divides @var{number}
|
|
|
|
by each divisor in @var{divisors} in turn, and returns the quotient.
|
|
|
|
With no @var{divisors}, this function returns 1/@var{number}, i.e.,
|
|
|
|
the multiplicative inverse of @var{number}. Each argument may be a
|
2007-09-06 04:25:08 +00:00
|
|
|
number or a marker.
|
|
|
|
|
2012-09-30 09:18:38 +00:00
|
|
|
If all the arguments are integers, the result is an integer, obtained
|
|
|
|
by rounding the quotient towards zero after each division.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(/ 6 2)
|
|
|
|
@result{} 3
|
|
|
|
@end group
|
2012-09-30 09:18:38 +00:00
|
|
|
@group
|
2007-09-06 04:25:08 +00:00
|
|
|
(/ 5 2)
|
|
|
|
@result{} 2
|
2012-09-30 09:18:38 +00:00
|
|
|
@end group
|
|
|
|
@group
|
2007-09-06 04:25:08 +00:00
|
|
|
(/ 5.0 2)
|
|
|
|
@result{} 2.5
|
2012-09-30 09:18:38 +00:00
|
|
|
@end group
|
|
|
|
@group
|
2007-09-06 04:25:08 +00:00
|
|
|
(/ 5 2.0)
|
|
|
|
@result{} 2.5
|
2012-09-30 09:18:38 +00:00
|
|
|
@end group
|
|
|
|
@group
|
2007-09-06 04:25:08 +00:00
|
|
|
(/ 5.0 2.0)
|
|
|
|
@result{} 2.5
|
2012-09-30 09:18:38 +00:00
|
|
|
@end group
|
|
|
|
@group
|
2015-10-21 01:16:47 +00:00
|
|
|
(/ 4.0)
|
|
|
|
@result{} 0.25
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(/ 4)
|
|
|
|
@result{} 0
|
|
|
|
@end group
|
|
|
|
@group
|
2007-09-06 04:25:08 +00:00
|
|
|
(/ 25 3 2)
|
|
|
|
@result{} 4
|
2012-09-30 09:18:38 +00:00
|
|
|
@end group
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
|
|
|
(/ -17 6)
|
2012-09-30 09:18:38 +00:00
|
|
|
@result{} -2
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end example
|
2012-09-30 09:18:38 +00:00
|
|
|
|
|
|
|
@cindex @code{arith-error} in division
|
|
|
|
If you divide an integer by the integer 0, Emacs signals an
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
@code{arith-error} error (@pxref{Errors}). Floating-point division of
|
|
|
|
a nonzero number by zero yields either positive or negative infinity
|
|
|
|
(@pxref{Float Basics}).
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun % dividend divisor
|
|
|
|
@cindex remainder
|
|
|
|
This function returns the integer remainder after division of @var{dividend}
|
|
|
|
by @var{divisor}. The arguments must be integers or markers.
|
|
|
|
|
2012-09-30 09:18:38 +00:00
|
|
|
For any two integers @var{dividend} and @var{divisor},
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(+ (% @var{dividend} @var{divisor})
|
|
|
|
(* (/ @var{dividend} @var{divisor}) @var{divisor}))
|
|
|
|
@end group
|
|
|
|
@end example
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2012-09-30 09:18:38 +00:00
|
|
|
@noindent
|
2014-03-19 21:21:01 +00:00
|
|
|
always equals @var{dividend} if @var{divisor} is nonzero.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
(% 9 4)
|
|
|
|
@result{} 1
|
|
|
|
(% -9 4)
|
|
|
|
@result{} -1
|
|
|
|
(% 9 -4)
|
|
|
|
@result{} 1
|
|
|
|
(% -9 -4)
|
|
|
|
@result{} -1
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun mod dividend divisor
|
|
|
|
@cindex modulus
|
|
|
|
This function returns the value of @var{dividend} modulo @var{divisor};
|
|
|
|
in other words, the remainder after division of @var{dividend}
|
|
|
|
by @var{divisor}, but with the same sign as @var{divisor}.
|
|
|
|
The arguments must be numbers or markers.
|
|
|
|
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
Unlike @code{%}, @code{mod} permits floating-point arguments; it
|
2012-09-30 09:18:38 +00:00
|
|
|
rounds the quotient downward (towards minus infinity) to an integer,
|
|
|
|
and uses that quotient to compute the remainder.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
Simplify, document, and port floating-point.
The porting part of this patch fixes bugs on non-IEEE platforms
with frexp, ldexp, logb.
* admin/CPP-DEFINES (HAVE_CBRT, HAVE_LOGB, logb): Remove.
* configure.ac (logb, cbrt): Do not check for these functions,
as they are not being used.
* doc/lispref/numbers.texi (Float Basics, Arithmetic Operations, Math Functions):
Document that / and mod (with floating point arguments), along
with asin, acos, log, log10, expt and sqrt, return special values
instead of signaling exceptions.
(Float Basics): Document that logb operates on the absolute value
of its argument.
(Math Functions): Document that (log ARG BASE) also returns NaN if
BASE is negative. Document that (expt X Y) returns NaN if X is a
finite negative number and Y a finite non-integer.
* etc/NEWS: Document NaNs versus signaling-error change.
* src/data.c, src/lisp.h (Qdomain_error, Qsingularity_error, Qunderflow_error):
Now static.
* src/floatfns.c: Simplify discussion of functions that Emacs doesn't
support, by removing commented-out code and briefly listing the
C89 functions excluded. The commented-out stuff was confusing
maintenance, e.g., we thought we needed cbrt but it was commented out.
(logb): Remove decl; no longer needed.
(isfinite): New macro, if not already supplied.
(isnan): Don't replace any existing macro.
(Ffrexp, Fldexp): Define even if !HAVE_COPYSIGN, as frexp and ldexp
are present on all C89 platforms.
(Ffrexp): Do not special-case zero, as frexp does the right thing
for that case.
(Flogb): Do not use logb, as it doesn't have the desired meaning
on hosts that use non-base-2 floating point. Instead, stick with
frexp, which is C89 anyway. Do not pass an infinity or a NaN to
frexp, to avoid getting an unspecified result.
2012-09-11 02:28:27 +00:00
|
|
|
If @var{divisor} is zero, @code{mod} signals an @code{arith-error}
|
|
|
|
error if both arguments are integers, and returns a NaN otherwise.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(mod 9 4)
|
|
|
|
@result{} 1
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(mod -9 4)
|
|
|
|
@result{} 3
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(mod 9 -4)
|
|
|
|
@result{} -3
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(mod -9 -4)
|
|
|
|
@result{} -1
|
|
|
|
@end group
|
|
|
|
@group
|
|
|
|
(mod 5.5 2.5)
|
|
|
|
@result{} .5
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
For any two numbers @var{dividend} and @var{divisor},
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(+ (mod @var{dividend} @var{divisor})
|
|
|
|
(* (floor @var{dividend} @var{divisor}) @var{divisor}))
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
@noindent
|
|
|
|
always equals @var{dividend}, subject to rounding error if either
|
2014-03-19 21:21:01 +00:00
|
|
|
argument is floating point and to an @code{arith-error} if @var{dividend} is an
|
|
|
|
integer and @var{divisor} is 0. For @code{floor}, see @ref{Numeric
|
2007-09-06 04:25:08 +00:00
|
|
|
Conversions}.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Rounding Operations
|
|
|
|
@section Rounding Operations
|
|
|
|
@cindex rounding without conversion
|
|
|
|
|
|
|
|
The functions @code{ffloor}, @code{fceiling}, @code{fround}, and
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
@code{ftruncate} take a floating-point argument and return a floating-point
|
|
|
|
result whose value is a nearby integer. @code{ffloor} returns the
|
2007-09-06 04:25:08 +00:00
|
|
|
nearest integer below; @code{fceiling}, the nearest integer above;
|
|
|
|
@code{ftruncate}, the nearest integer in the direction towards zero;
|
|
|
|
@code{fround}, the nearest integer.
|
|
|
|
|
|
|
|
@defun ffloor float
|
|
|
|
This function rounds @var{float} to the next lower integral value, and
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
returns that value as a floating-point number.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun fceiling float
|
|
|
|
This function rounds @var{float} to the next higher integral value, and
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
returns that value as a floating-point number.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun ftruncate float
|
|
|
|
This function rounds @var{float} towards zero to an integral value, and
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
returns that value as a floating-point number.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun fround float
|
|
|
|
This function rounds @var{float} to the nearest integral value,
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
and returns that value as a floating-point number.
|
2014-03-19 21:09:08 +00:00
|
|
|
Rounding a value equidistant between two integers returns the even integer.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@node Bitwise Operations
|
|
|
|
@section Bitwise Operations on Integers
|
|
|
|
@cindex bitwise arithmetic
|
|
|
|
@cindex logical arithmetic
|
|
|
|
|
|
|
|
In a computer, an integer is represented as a binary number, a
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
sequence of @dfn{bits} (digits which are either zero or one).
|
|
|
|
Conceptually the bit sequence is infinite on the left, with the
|
|
|
|
most-significant bits being all zeros or all ones. A bitwise
|
2007-09-06 04:25:08 +00:00
|
|
|
operation acts on the individual bits of such a sequence. For example,
|
|
|
|
@dfn{shifting} moves the whole sequence left or right one or more places,
|
2015-09-15 15:46:48 +00:00
|
|
|
reproducing the same pattern moved over.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
The bitwise operations in Emacs Lisp apply only to integers.
|
|
|
|
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@defun ash integer1 count
|
|
|
|
@cindex arithmetic shift
|
|
|
|
@code{ash} (@dfn{arithmetic shift}) shifts the bits in @var{integer1}
|
|
|
|
to the left @var{count} places, or to the right if @var{count} is
|
|
|
|
negative. Left shifts introduce zero bits on the right; right shifts
|
|
|
|
discard the rightmost bits. Considered as an integer operation,
|
2018-08-21 17:05:31 +00:00
|
|
|
@code{ash} multiplies @var{integer1} by
|
|
|
|
@ifnottex
|
|
|
|
2**@var{count},
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{2^{count}},
|
|
|
|
@end tex
|
|
|
|
and then converts the result to an integer by rounding downward, toward
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
minus infinity.
|
|
|
|
|
|
|
|
Here are examples of @code{ash}, shifting a pattern of bits one place
|
|
|
|
to the left and to the right. These examples show only the low-order
|
|
|
|
bits of the binary pattern; leading bits all agree with the
|
|
|
|
highest-order bit shown. As you can see, shifting left by one is
|
|
|
|
equivalent to multiplying by two, whereas shifting right by one is
|
|
|
|
equivalent to dividing by two and then rounding toward minus infinity.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(ash 7 1) @result{} 14
|
2007-09-06 04:25:08 +00:00
|
|
|
;; @r{Decimal 7 becomes decimal 14.}
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}000111
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@result{}
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}001110
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(ash 7 -1) @result{} 3
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}000111
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@result{}
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}000011
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(ash -7 1) @result{} -14
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}111001
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@result{}
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}110010
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(ash -7 -1) @result{} -4
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}111001
|
2007-09-06 04:25:08 +00:00
|
|
|
@result{}
|
2018-08-21 14:56:47 +00:00
|
|
|
@dots{}111100
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
Here are examples of shifting left or right by two bits:
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
; @r{ binary values}
|
2018-08-21 14:56:47 +00:00
|
|
|
(ash 5 2) ; 5 = @r{@dots{}000101}
|
|
|
|
@result{} 20 ; = @r{@dots{}010100}
|
|
|
|
(ash -5 2) ; -5 = @r{@dots{}111011}
|
|
|
|
@result{} -20 ; = @r{@dots{}101100}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(ash 5 -2)
|
2018-08-21 14:56:47 +00:00
|
|
|
@result{} 1 ; = @r{@dots{}000001}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(ash -5 -2)
|
2018-08-21 14:56:47 +00:00
|
|
|
@result{} -2 ; = @r{@dots{}111110}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun lsh integer1 count
|
|
|
|
@cindex logical shift
|
|
|
|
@code{lsh}, which is an abbreviation for @dfn{logical shift}, shifts the
|
|
|
|
bits in @var{integer1} to the left @var{count} places, or to the right
|
|
|
|
if @var{count} is negative, bringing zeros into the vacated bits. If
|
|
|
|
@var{count} is negative, then @var{integer1} must be either a fixnum
|
|
|
|
or a positive bignum, and @code{lsh} treats a negative fixnum as if it
|
|
|
|
were unsigned by subtracting twice @code{most-negative-fixnum} before
|
|
|
|
shifting, producing a nonnegative result. This quirky behavior dates
|
|
|
|
back to when Emacs supported only fixnums; nowadays @code{ash} is a
|
|
|
|
better choice.
|
|
|
|
|
|
|
|
As @code{lsh} behaves like @code{ash} except when @var{integer1} and
|
|
|
|
@var{count1} are both negative, the following examples focus on these
|
|
|
|
exceptional cases. These examples assume 30-bit fixnums.
|
|
|
|
|
|
|
|
@smallexample
|
2007-09-06 04:25:08 +00:00
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
; @r{ binary values}
|
2018-08-21 14:56:47 +00:00
|
|
|
(ash -7 -1) ; -7 = @r{@dots{}111111111111111111111111111001}
|
|
|
|
@result{} -4 ; = @r{@dots{}111111111111111111111111111100}
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(lsh -7 -1)
|
2018-08-21 14:56:47 +00:00
|
|
|
@result{} 536870908 ; = @r{@dots{}011111111111111111111111111100}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@group
|
2018-08-21 14:56:47 +00:00
|
|
|
(ash -5 -2) ; -5 = @r{@dots{}111111111111111111111111111011}
|
|
|
|
@result{} -2 ; = @r{@dots{}111111111111111111111111111110}
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
(lsh -5 -2)
|
2018-08-21 14:56:47 +00:00
|
|
|
@result{} 268435454 ; = @r{@dots{}001111111111111111111111111110}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun logand &rest ints-or-markers
|
2015-09-15 15:46:48 +00:00
|
|
|
This function returns the bitwise AND of the arguments: the @var{n}th
|
|
|
|
bit is 1 in the result if, and only if, the @var{n}th bit is 1 in all
|
|
|
|
the arguments.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2015-09-15 15:46:48 +00:00
|
|
|
For example, using 4-bit binary numbers, the bitwise AND of 13 and
|
2007-09-06 04:25:08 +00:00
|
|
|
12 is 12: 1101 combined with 1100 produces 1100.
|
2015-09-15 15:46:48 +00:00
|
|
|
In both the binary numbers, the leftmost two bits are both 1
|
|
|
|
so the leftmost two bits of the returned value are both 1.
|
|
|
|
However, for the rightmost two bits, each is 0 in at least one of
|
|
|
|
the arguments, so the rightmost two bits of the returned value are both 0.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@noindent
|
|
|
|
Therefore,
|
|
|
|
|
|
|
|
@example
|
|
|
|
@group
|
|
|
|
(logand 13 12)
|
|
|
|
@result{} 12
|
|
|
|
@end group
|
|
|
|
@end example
|
|
|
|
|
|
|
|
If @code{logand} is not passed any argument, it returns a value of
|
|
|
|
@minus{}1. This number is an identity element for @code{logand}
|
|
|
|
because its binary representation consists entirely of ones. If
|
|
|
|
@code{logand} is passed just one argument, it returns that argument.
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
; @r{ binary values}
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-08-21 14:56:47 +00:00
|
|
|
(logand 14 13) ; 14 = @r{@dots{}001110}
|
|
|
|
; 13 = @r{@dots{}001101}
|
|
|
|
@result{} 12 ; 12 = @r{@dots{}001100}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
2018-08-21 14:56:47 +00:00
|
|
|
(logand 14 13 4) ; 14 = @r{@dots{}001110}
|
|
|
|
; 13 = @r{@dots{}001101}
|
|
|
|
; 4 = @r{@dots{}000100}
|
|
|
|
@result{} 4 ; 4 = @r{@dots{}000100}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
|
|
|
(logand)
|
2018-08-21 14:56:47 +00:00
|
|
|
@result{} -1 ; -1 = @r{@dots{}111111}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun logior &rest ints-or-markers
|
2015-09-15 15:46:48 +00:00
|
|
|
This function returns the bitwise inclusive OR of its arguments: the @var{n}th
|
|
|
|
bit is 1 in the result if, and only if, the @var{n}th bit is 1 in at
|
|
|
|
least one of the arguments. If there are no arguments, the result is 0,
|
2007-09-06 04:25:08 +00:00
|
|
|
which is an identity element for this operation. If @code{logior} is
|
|
|
|
passed just one argument, it returns that argument.
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
; @r{ binary values}
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-08-21 14:56:47 +00:00
|
|
|
(logior 12 5) ; 12 = @r{@dots{}001100}
|
|
|
|
; 5 = @r{@dots{}000101}
|
|
|
|
@result{} 13 ; 13 = @r{@dots{}001101}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
2018-08-21 14:56:47 +00:00
|
|
|
(logior 12 5 7) ; 12 = @r{@dots{}001100}
|
|
|
|
; 5 = @r{@dots{}000101}
|
|
|
|
; 7 = @r{@dots{}000111}
|
|
|
|
@result{} 15 ; 15 = @r{@dots{}001111}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun logxor &rest ints-or-markers
|
2015-09-15 15:46:48 +00:00
|
|
|
This function returns the bitwise exclusive OR of its arguments: the
|
|
|
|
@var{n}th bit is 1 in the result if, and only if, the @var{n}th bit is
|
|
|
|
1 in an odd number of the arguments. If there are no arguments, the
|
2007-09-06 04:25:08 +00:00
|
|
|
result is 0, which is an identity element for this operation. If
|
|
|
|
@code{logxor} is passed just one argument, it returns that argument.
|
|
|
|
|
|
|
|
@smallexample
|
|
|
|
@group
|
Avoid libgmp aborts by imposing limits
libgmp calls ‘abort’ when given numbers too big for its
internal data structures. The numeric limit is large and
platform-dependent; with 64-bit GMP 6.1.2 it is around
2**2**37. Work around the problem by refusing to call libgmp
functions with arguments that would cause an abort. With luck
libgmp will have a better way to do this in the future.
Also, introduce a variable integer-width that lets the user
control how large bignums can be. This currently defaults
to 2**16, i.e., it allows bignums up to 2**2**16. This
should be enough for ordinary computation, and should
help Emacs to avoid thrashing or hanging.
Problem noted by Pip Cet (Bug#32463#71).
* doc/lispref/numbers.texi, etc/NEWS:
Document recent bignum changes, including this one.
Improve documentation for bitwise operations, in the light
of bignums.
* src/alloc.c (make_number): Enforce integer-width.
(integer_overflow): New function.
(xrealloc_for_gmp, xfree_for_gmp):
Move here from emacs.c, as it's memory allocation.
(init_alloc): Initialize GMP here, rather than in emacs.c.
(integer_width): New var.
* src/data.c (GMP_NLIMBS_MAX, NLIMBS_LIMIT): New constants.
(emacs_mpz_size, emacs_mpz_mul)
(emacs_mpz_mul_2exp, emacs_mpz_pow_ui): New functions.
(arith_driver, Fash, expt_integer): Use them.
(expt_integer): New function, containing integer code
that was out of place in floatfns.c.
(check_bignum_size, xmalloc_for_gmp): Remove.
* src/emacs.c (main): Do not initialize GMP here.
* src/floatfns.c (Fexpt): Use expt_integer, which
now contains integer code moved from here.
* src/lisp.h (GMP_NUMB_BITS): Define if gmp.h doesn’t.
2018-08-21 09:16:50 +00:00
|
|
|
; @r{ binary values}
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2018-08-21 14:56:47 +00:00
|
|
|
(logxor 12 5) ; 12 = @r{@dots{}001100}
|
|
|
|
; 5 = @r{@dots{}000101}
|
|
|
|
@result{} 9 ; 9 = @r{@dots{}001001}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
|
|
|
|
@group
|
2018-08-21 14:56:47 +00:00
|
|
|
(logxor 12 5 7) ; 12 = @r{@dots{}001100}
|
|
|
|
; 5 = @r{@dots{}000101}
|
|
|
|
; 7 = @r{@dots{}000111}
|
|
|
|
@result{} 14 ; 14 = @r{@dots{}001110}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end group
|
|
|
|
@end smallexample
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun lognot integer
|
2015-09-15 15:46:48 +00:00
|
|
|
This function returns the bitwise complement of its argument: the @var{n}th
|
2007-09-06 04:25:08 +00:00
|
|
|
bit is one in the result if, and only if, the @var{n}th bit is zero in
|
2019-07-31 13:56:14 +00:00
|
|
|
@var{integer}, and vice-versa. The result equals @minus{}1 @minus{}
|
|
|
|
@var{integer}.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@example
|
|
|
|
(lognot 5)
|
|
|
|
@result{} -6
|
2018-08-21 14:56:47 +00:00
|
|
|
;; 5 = @r{@dots{}000101}
|
2007-09-06 04:25:08 +00:00
|
|
|
;; @r{becomes}
|
2018-08-21 14:56:47 +00:00
|
|
|
;; -6 = @r{@dots{}111010}
|
2007-09-06 04:25:08 +00:00
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
2017-09-30 18:14:12 +00:00
|
|
|
@cindex popcount
|
|
|
|
@cindex Hamming weight
|
|
|
|
@cindex counting set bits
|
|
|
|
@defun logcount integer
|
|
|
|
This function returns the @dfn{Hamming weight} of @var{integer}: the
|
|
|
|
number of ones in the binary representation of @var{integer}.
|
2017-09-30 22:36:52 +00:00
|
|
|
If @var{integer} is negative, it returns the number of zero bits in
|
|
|
|
its two's complement binary representation. The result is always
|
|
|
|
nonnegative.
|
2017-09-30 18:14:12 +00:00
|
|
|
|
|
|
|
@example
|
2018-08-21 14:56:47 +00:00
|
|
|
(logcount 43) ; 43 = @r{@dots{}000101011}
|
2017-09-30 22:36:52 +00:00
|
|
|
@result{} 4
|
2018-08-21 14:56:47 +00:00
|
|
|
(logcount -43) ; -43 = @r{@dots{}111010101}
|
2017-09-30 18:14:12 +00:00
|
|
|
@result{} 3
|
|
|
|
@end example
|
|
|
|
@end defun
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Math Functions
|
|
|
|
@section Standard Mathematical Functions
|
|
|
|
@cindex transcendental functions
|
|
|
|
@cindex mathematical functions
|
|
|
|
@cindex floating-point functions
|
|
|
|
|
Style fixes for floating-point doc.
* commands.texi, customize.texi, display.texi, elisp.texi, files.texi:
* frames.texi, hash.texi, internals.texi, keymaps.texi, lists.texi:
* minibuf.texi, nonascii.texi, numbers.texi, objects.texi, os.texi:
* processes.texi, streams.texi, strings.texi, text.texi:
* variables.texi, windows.texi:
Hyphenate "floating-point" iff it precedes a noun.
Reword to avoid nouns and hyphenation when that's easy.
Prefer "integer" to "integer number" and "is floating point"
to "is a floating point number".
Prefer "@minus{}" to "-" when it's a minus.
2014-03-18 01:19:03 +00:00
|
|
|
These mathematical functions allow integers as well as floating-point
|
2007-09-06 04:25:08 +00:00
|
|
|
numbers as arguments.
|
|
|
|
|
|
|
|
@defun sin arg
|
|
|
|
@defunx cos arg
|
|
|
|
@defunx tan arg
|
2012-09-30 09:18:38 +00:00
|
|
|
These are the basic trigonometric functions, with argument @var{arg}
|
|
|
|
measured in radians.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun asin arg
|
|
|
|
The value of @code{(asin @var{arg})} is a number between
|
|
|
|
@ifnottex
|
|
|
|
@minus{}pi/2
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{-\pi/2}
|
|
|
|
@end tex
|
|
|
|
and
|
|
|
|
@ifnottex
|
|
|
|
pi/2
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{\pi/2}
|
|
|
|
@end tex
|
Simplify, document, and port floating-point.
The porting part of this patch fixes bugs on non-IEEE platforms
with frexp, ldexp, logb.
* admin/CPP-DEFINES (HAVE_CBRT, HAVE_LOGB, logb): Remove.
* configure.ac (logb, cbrt): Do not check for these functions,
as they are not being used.
* doc/lispref/numbers.texi (Float Basics, Arithmetic Operations, Math Functions):
Document that / and mod (with floating point arguments), along
with asin, acos, log, log10, expt and sqrt, return special values
instead of signaling exceptions.
(Float Basics): Document that logb operates on the absolute value
of its argument.
(Math Functions): Document that (log ARG BASE) also returns NaN if
BASE is negative. Document that (expt X Y) returns NaN if X is a
finite negative number and Y a finite non-integer.
* etc/NEWS: Document NaNs versus signaling-error change.
* src/data.c, src/lisp.h (Qdomain_error, Qsingularity_error, Qunderflow_error):
Now static.
* src/floatfns.c: Simplify discussion of functions that Emacs doesn't
support, by removing commented-out code and briefly listing the
C89 functions excluded. The commented-out stuff was confusing
maintenance, e.g., we thought we needed cbrt but it was commented out.
(logb): Remove decl; no longer needed.
(isfinite): New macro, if not already supplied.
(isnan): Don't replace any existing macro.
(Ffrexp, Fldexp): Define even if !HAVE_COPYSIGN, as frexp and ldexp
are present on all C89 platforms.
(Ffrexp): Do not special-case zero, as frexp does the right thing
for that case.
(Flogb): Do not use logb, as it doesn't have the desired meaning
on hosts that use non-base-2 floating point. Instead, stick with
frexp, which is C89 anyway. Do not pass an infinity or a NaN to
frexp, to avoid getting an unspecified result.
2012-09-11 02:28:27 +00:00
|
|
|
(inclusive) whose sine is @var{arg}. If @var{arg} is out of range
|
|
|
|
(outside [@minus{}1, 1]), @code{asin} returns a NaN.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun acos arg
|
|
|
|
The value of @code{(acos @var{arg})} is a number between 0 and
|
|
|
|
@ifnottex
|
|
|
|
pi
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{\pi}
|
|
|
|
@end tex
|
Simplify, document, and port floating-point.
The porting part of this patch fixes bugs on non-IEEE platforms
with frexp, ldexp, logb.
* admin/CPP-DEFINES (HAVE_CBRT, HAVE_LOGB, logb): Remove.
* configure.ac (logb, cbrt): Do not check for these functions,
as they are not being used.
* doc/lispref/numbers.texi (Float Basics, Arithmetic Operations, Math Functions):
Document that / and mod (with floating point arguments), along
with asin, acos, log, log10, expt and sqrt, return special values
instead of signaling exceptions.
(Float Basics): Document that logb operates on the absolute value
of its argument.
(Math Functions): Document that (log ARG BASE) also returns NaN if
BASE is negative. Document that (expt X Y) returns NaN if X is a
finite negative number and Y a finite non-integer.
* etc/NEWS: Document NaNs versus signaling-error change.
* src/data.c, src/lisp.h (Qdomain_error, Qsingularity_error, Qunderflow_error):
Now static.
* src/floatfns.c: Simplify discussion of functions that Emacs doesn't
support, by removing commented-out code and briefly listing the
C89 functions excluded. The commented-out stuff was confusing
maintenance, e.g., we thought we needed cbrt but it was commented out.
(logb): Remove decl; no longer needed.
(isfinite): New macro, if not already supplied.
(isnan): Don't replace any existing macro.
(Ffrexp, Fldexp): Define even if !HAVE_COPYSIGN, as frexp and ldexp
are present on all C89 platforms.
(Ffrexp): Do not special-case zero, as frexp does the right thing
for that case.
(Flogb): Do not use logb, as it doesn't have the desired meaning
on hosts that use non-base-2 floating point. Instead, stick with
frexp, which is C89 anyway. Do not pass an infinity or a NaN to
frexp, to avoid getting an unspecified result.
2012-09-11 02:28:27 +00:00
|
|
|
(inclusive) whose cosine is @var{arg}. If @var{arg} is out of range
|
|
|
|
(outside [@minus{}1, 1]), @code{acos} returns a NaN.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun atan y &optional x
|
|
|
|
The value of @code{(atan @var{y})} is a number between
|
|
|
|
@ifnottex
|
|
|
|
@minus{}pi/2
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{-\pi/2}
|
|
|
|
@end tex
|
|
|
|
and
|
|
|
|
@ifnottex
|
|
|
|
pi/2
|
|
|
|
@end ifnottex
|
|
|
|
@tex
|
|
|
|
@math{\pi/2}
|
|
|
|
@end tex
|
|
|
|
(exclusive) whose tangent is @var{y}. If the optional second
|
|
|
|
argument @var{x} is given, the value of @code{(atan y x)} is the
|
|
|
|
angle in radians between the vector @code{[@var{x}, @var{y}]} and the
|
|
|
|
@code{X} axis.
|
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun exp arg
|
2012-01-21 16:04:55 +00:00
|
|
|
This is the exponential function; it returns @math{e} to the power
|
|
|
|
@var{arg}.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun log arg &optional base
|
2012-01-21 16:04:55 +00:00
|
|
|
This function returns the logarithm of @var{arg}, with base
|
|
|
|
@var{base}. If you don't specify @var{base}, the natural base
|
Simplify, document, and port floating-point.
The porting part of this patch fixes bugs on non-IEEE platforms
with frexp, ldexp, logb.
* admin/CPP-DEFINES (HAVE_CBRT, HAVE_LOGB, logb): Remove.
* configure.ac (logb, cbrt): Do not check for these functions,
as they are not being used.
* doc/lispref/numbers.texi (Float Basics, Arithmetic Operations, Math Functions):
Document that / and mod (with floating point arguments), along
with asin, acos, log, log10, expt and sqrt, return special values
instead of signaling exceptions.
(Float Basics): Document that logb operates on the absolute value
of its argument.
(Math Functions): Document that (log ARG BASE) also returns NaN if
BASE is negative. Document that (expt X Y) returns NaN if X is a
finite negative number and Y a finite non-integer.
* etc/NEWS: Document NaNs versus signaling-error change.
* src/data.c, src/lisp.h (Qdomain_error, Qsingularity_error, Qunderflow_error):
Now static.
* src/floatfns.c: Simplify discussion of functions that Emacs doesn't
support, by removing commented-out code and briefly listing the
C89 functions excluded. The commented-out stuff was confusing
maintenance, e.g., we thought we needed cbrt but it was commented out.
(logb): Remove decl; no longer needed.
(isfinite): New macro, if not already supplied.
(isnan): Don't replace any existing macro.
(Ffrexp, Fldexp): Define even if !HAVE_COPYSIGN, as frexp and ldexp
are present on all C89 platforms.
(Ffrexp): Do not special-case zero, as frexp does the right thing
for that case.
(Flogb): Do not use logb, as it doesn't have the desired meaning
on hosts that use non-base-2 floating point. Instead, stick with
frexp, which is C89 anyway. Do not pass an infinity or a NaN to
frexp, to avoid getting an unspecified result.
2012-09-11 02:28:27 +00:00
|
|
|
@math{e} is used. If @var{arg} or @var{base} is negative, @code{log}
|
|
|
|
returns a NaN.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun expt x y
|
|
|
|
This function returns @var{x} raised to power @var{y}. If both
|
2018-07-23 07:05:25 +00:00
|
|
|
arguments are integers and @var{y} is nonnegative, the result is an
|
2018-08-19 08:22:08 +00:00
|
|
|
integer; in this case, overflow signals an error, so watch out.
|
Simplify, document, and port floating-point.
The porting part of this patch fixes bugs on non-IEEE platforms
with frexp, ldexp, logb.
* admin/CPP-DEFINES (HAVE_CBRT, HAVE_LOGB, logb): Remove.
* configure.ac (logb, cbrt): Do not check for these functions,
as they are not being used.
* doc/lispref/numbers.texi (Float Basics, Arithmetic Operations, Math Functions):
Document that / and mod (with floating point arguments), along
with asin, acos, log, log10, expt and sqrt, return special values
instead of signaling exceptions.
(Float Basics): Document that logb operates on the absolute value
of its argument.
(Math Functions): Document that (log ARG BASE) also returns NaN if
BASE is negative. Document that (expt X Y) returns NaN if X is a
finite negative number and Y a finite non-integer.
* etc/NEWS: Document NaNs versus signaling-error change.
* src/data.c, src/lisp.h (Qdomain_error, Qsingularity_error, Qunderflow_error):
Now static.
* src/floatfns.c: Simplify discussion of functions that Emacs doesn't
support, by removing commented-out code and briefly listing the
C89 functions excluded. The commented-out stuff was confusing
maintenance, e.g., we thought we needed cbrt but it was commented out.
(logb): Remove decl; no longer needed.
(isfinite): New macro, if not already supplied.
(isnan): Don't replace any existing macro.
(Ffrexp, Fldexp): Define even if !HAVE_COPYSIGN, as frexp and ldexp
are present on all C89 platforms.
(Ffrexp): Do not special-case zero, as frexp does the right thing
for that case.
(Flogb): Do not use logb, as it doesn't have the desired meaning
on hosts that use non-base-2 floating point. Instead, stick with
frexp, which is C89 anyway. Do not pass an infinity or a NaN to
frexp, to avoid getting an unspecified result.
2012-09-11 02:28:27 +00:00
|
|
|
If @var{x} is a finite negative number and @var{y} is a finite
|
|
|
|
non-integer, @code{expt} returns a NaN.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
|
|
|
@defun sqrt arg
|
2014-03-19 21:21:01 +00:00
|
|
|
This returns the square root of @var{arg}. If @var{arg} is finite
|
|
|
|
and less than zero, @code{sqrt} returns a NaN.
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|
|
|
|
|
2012-01-21 16:04:55 +00:00
|
|
|
In addition, Emacs defines the following common mathematical
|
|
|
|
constants:
|
|
|
|
|
|
|
|
@defvar float-e
|
|
|
|
The mathematical constant @math{e} (2.71828@dots{}).
|
|
|
|
@end defvar
|
|
|
|
|
|
|
|
@defvar float-pi
|
|
|
|
The mathematical constant @math{pi} (3.14159@dots{}).
|
|
|
|
@end defvar
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@node Random Numbers
|
|
|
|
@section Random Numbers
|
|
|
|
@cindex random numbers
|
|
|
|
|
2012-09-30 09:18:38 +00:00
|
|
|
A deterministic computer program cannot generate true random
|
|
|
|
numbers. For most purposes, @dfn{pseudo-random numbers} suffice. A
|
|
|
|
series of pseudo-random numbers is generated in a deterministic
|
|
|
|
fashion. The numbers are not truly random, but they have certain
|
|
|
|
properties that mimic a random series. For example, all possible
|
|
|
|
values occur equally often in a pseudo-random series.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
Restore some of the quoting in the manuals
* doc/lispref/windows.texi (Coordinates and Windows)
(Coordinates and Windows):
* doc/lispref/variables.texi (Lexical Binding)
(File Local Variables):
* doc/lispref/text.texi (Format Properties):
* doc/lispref/symbols.texi (Symbol Components):
* doc/lispref/strings.texi (Creating Strings):
* doc/lispref/sequences.texi (Sequence Functions):
* doc/lispref/searching.texi (Regexp Special, Regexp Search)
(Search and Replace):
* doc/lispref/processes.texi (Bindat Spec):
* doc/lispref/os.texi (Idle Timers):
* doc/lispref/objects.texi (Basic Char Syntax):
* doc/lispref/numbers.texi (Float Basics, Random Numbers):
* doc/lispref/nonascii.texi (Character Properties):
* doc/lispref/modes.texi (Major Mode Conventions, Mode Hooks)
(Mode Line Variables):
* doc/lispref/minibuf.texi (Text from Minibuffer):
* doc/lispref/loading.texi (Autoload):
* doc/lispref/keymaps.texi (Controlling Active Maps):
* doc/lispref/frames.texi (Frame Layout, Size and Position)
(Size Parameters, Implied Frame Resizing):
* doc/lispref/files.texi (Changing Files, Magic File Names):
* doc/lispref/eval.texi (Self-Evaluating Forms):
* doc/lispref/display.texi (Progress, Abstract Display)
(Abstract Display Example, Bidirectional Display):
* doc/lispref/commands.texi (Event Mod):
* doc/emacs/windows.texi (Displaying Buffers):
* doc/emacs/trouble.texi (Bug Criteria, Checklist):
* doc/emacs/text.texi (Enriched Text):
* doc/emacs/programs.texi (MixedCase Words):
* doc/emacs/picture-xtra.texi (Insert in Picture)
(Tabs in Picture):
* doc/emacs/misc.texi (Emacs Server, Printing):
* doc/emacs/mini.texi (Minibuffer History):
* doc/emacs/maintaining.texi (Old Revisions, VC Change Log)
(Pulling / Pushing):
* doc/emacs/killing.texi (Yanking, Cut and Paste, Clipboard):
* doc/emacs/help.texi (Help, Help Echo):
* doc/emacs/glossary.texi (Glossary):
* doc/emacs/frames.texi (Mouse Commands, Creating Frames)
(Frame Commands):
* doc/emacs/files.texi (Reverting, Saving, Directories):
* doc/emacs/entering.texi (Exiting):
* doc/emacs/emacs.texi (Top):
* doc/emacs/cmdargs.texi (Window Size X, Icons X):
* doc/emacs/anti.texi (Antinews): Restore quoting of text where
appropriate or replace quoting with @dfn.
* doc/misc/ediff.texi (Window and Frame Configuration):
* doc/lispref/processes.texi (Network Feature Testing):
* doc/lispref/display.texi (Display Margins): Quote the phrase
after "a.k.a." where appropriate.
2015-09-16 09:56:45 +00:00
|
|
|
@cindex seed, for random number generation
|
|
|
|
Pseudo-random numbers are generated from a @dfn{seed value}. Starting from
|
2012-09-30 09:18:38 +00:00
|
|
|
any given seed, the @code{random} function always generates the same
|
|
|
|
sequence of numbers. By default, Emacs initializes the random seed at
|
|
|
|
startup, in such a way that the sequence of values of @code{random}
|
|
|
|
(with overwhelming likelihood) differs in each Emacs run.
|
Better seed support for (random).
* doc/lispref/numbers.texi (Random Numbers): Document new behavior of
the calls (random) and (random STRING).
* etc/NEWS: Document new behavior of (random), (random "string").
* lisp/play/5x5.el, lisp/play/animate.el, lisp/play/cookie1.el:
* lisp/play/dissociate.el, lisp/play/doctor.el, lisp/play/dunnet.el:
* lisp/play/gomoku.el, lisp/play/landmark.el, lisp/play/mpuz.el:
* lisp/play/tetris.el, lisp/play/zone.el:
* lisp/calc/calc-comb.el (math-init-random-base):
* lisp/play/blackbox.el (bb-init-board):
* lisp/play/life.el (life):
* lisp/server.el (server-use-tcp):
* lisp/type-break.el (type-break):
Remove unnecessary call to (random t).
* lisp/net/sasl.el (sasl-unique-id-function):
Change (random t) to (random), now that the latter is more random.
* lisp/play/life.el (life-initialized): Remove no-longer-needed var.
* lisp/gnus/gnus-sync.el (gnus-sync-lesync-setup):
* lisp/gnus/message.el (message-canlock-generate, message-unique-id):
Change (random t) to (random), now that the latter is more random.
* lisp/org/org-id.el (org-id-uuid):
Change (random t) to (random), now that the latter is more random.
* src/emacs.c (main): Call init_random.
* src/fns.c (Frandom): Set the seed from a string argument, if given.
Remove long-obsolete Gentzel cruft.
* src/lisp.h, src/sysdep.c (seed_random): Now takes address and size, not long.
(init_random): New function.
2012-09-01 01:04:26 +00:00
|
|
|
|
2012-09-30 09:18:38 +00:00
|
|
|
Sometimes you want the random number sequence to be repeatable. For
|
Better seed support for (random).
* doc/lispref/numbers.texi (Random Numbers): Document new behavior of
the calls (random) and (random STRING).
* etc/NEWS: Document new behavior of (random), (random "string").
* lisp/play/5x5.el, lisp/play/animate.el, lisp/play/cookie1.el:
* lisp/play/dissociate.el, lisp/play/doctor.el, lisp/play/dunnet.el:
* lisp/play/gomoku.el, lisp/play/landmark.el, lisp/play/mpuz.el:
* lisp/play/tetris.el, lisp/play/zone.el:
* lisp/calc/calc-comb.el (math-init-random-base):
* lisp/play/blackbox.el (bb-init-board):
* lisp/play/life.el (life):
* lisp/server.el (server-use-tcp):
* lisp/type-break.el (type-break):
Remove unnecessary call to (random t).
* lisp/net/sasl.el (sasl-unique-id-function):
Change (random t) to (random), now that the latter is more random.
* lisp/play/life.el (life-initialized): Remove no-longer-needed var.
* lisp/gnus/gnus-sync.el (gnus-sync-lesync-setup):
* lisp/gnus/message.el (message-canlock-generate, message-unique-id):
Change (random t) to (random), now that the latter is more random.
* lisp/org/org-id.el (org-id-uuid):
Change (random t) to (random), now that the latter is more random.
* src/emacs.c (main): Call init_random.
* src/fns.c (Frandom): Set the seed from a string argument, if given.
Remove long-obsolete Gentzel cruft.
* src/lisp.h, src/sysdep.c (seed_random): Now takes address and size, not long.
(init_random): New function.
2012-09-01 01:04:26 +00:00
|
|
|
example, when debugging a program whose behavior depends on the random
|
|
|
|
number sequence, it is helpful to get the same behavior in each
|
|
|
|
program run. To make the sequence repeat, execute @code{(random "")}.
|
|
|
|
This sets the seed to a constant value for your particular Emacs
|
|
|
|
executable (though it may differ for other Emacs builds). You can use
|
|
|
|
other strings to choose various seed values.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
|
|
|
@defun random &optional limit
|
|
|
|
This function returns a pseudo-random integer. Repeated calls return a
|
|
|
|
series of pseudo-random integers.
|
|
|
|
|
2018-08-18 22:39:05 +00:00
|
|
|
If @var{limit} is a positive fixnum, the value is chosen to be
|
2012-09-30 09:18:38 +00:00
|
|
|
nonnegative and less than @var{limit}. Otherwise, the value might be
|
2018-08-18 22:39:05 +00:00
|
|
|
any fixnum, i.e., any integer from @code{most-negative-fixnum} through
|
|
|
|
@code{most-positive-fixnum} (@pxref{Integer Basics}).
|
2007-09-06 04:25:08 +00:00
|
|
|
|
2014-03-19 21:21:01 +00:00
|
|
|
If @var{limit} is @code{t}, it means to choose a new seed as if Emacs
|
2016-01-19 05:31:09 +00:00
|
|
|
were restarting, typically from the system entropy. On systems
|
|
|
|
lacking entropy pools, choose the seed from less-random volatile data
|
|
|
|
such as the current time.
|
2007-09-06 04:25:08 +00:00
|
|
|
|
Better seed support for (random).
* doc/lispref/numbers.texi (Random Numbers): Document new behavior of
the calls (random) and (random STRING).
* etc/NEWS: Document new behavior of (random), (random "string").
* lisp/play/5x5.el, lisp/play/animate.el, lisp/play/cookie1.el:
* lisp/play/dissociate.el, lisp/play/doctor.el, lisp/play/dunnet.el:
* lisp/play/gomoku.el, lisp/play/landmark.el, lisp/play/mpuz.el:
* lisp/play/tetris.el, lisp/play/zone.el:
* lisp/calc/calc-comb.el (math-init-random-base):
* lisp/play/blackbox.el (bb-init-board):
* lisp/play/life.el (life):
* lisp/server.el (server-use-tcp):
* lisp/type-break.el (type-break):
Remove unnecessary call to (random t).
* lisp/net/sasl.el (sasl-unique-id-function):
Change (random t) to (random), now that the latter is more random.
* lisp/play/life.el (life-initialized): Remove no-longer-needed var.
* lisp/gnus/gnus-sync.el (gnus-sync-lesync-setup):
* lisp/gnus/message.el (message-canlock-generate, message-unique-id):
Change (random t) to (random), now that the latter is more random.
* lisp/org/org-id.el (org-id-uuid):
Change (random t) to (random), now that the latter is more random.
* src/emacs.c (main): Call init_random.
* src/fns.c (Frandom): Set the seed from a string argument, if given.
Remove long-obsolete Gentzel cruft.
* src/lisp.h, src/sysdep.c (seed_random): Now takes address and size, not long.
(init_random): New function.
2012-09-01 01:04:26 +00:00
|
|
|
If @var{limit} is a string, it means to choose a new seed based on the
|
|
|
|
string's contents.
|
|
|
|
|
2007-09-06 04:25:08 +00:00
|
|
|
@end defun
|