You can now use floating point numbers in Emacs, if you define the macro
LISP_FLOAT_TYPE
when you compile Emacs.
The printed representation for floating point numbers requires either a decimal point surrounded by digits, or an exponent, or both. For example, `1500.0', `15e2', `15.0e2' and `1.5e3' are four ways of writing a floating point number whose value is 1500.
The existing predicate numberp
now returns t
if the
argument is any kind of number--either integer or floating. The new
predicates integerp
and floatp
check for specific types of
numbers.
You can do arithmetic on floating point numbers with the ordinary
arithmetic functions, +
, -
, *
and /
. If you
call one of these functions with both integers and floating point
numbers among the arguments, the arithmetic is done in floating point.
The same applies to the numeric comparison functions such as =
and <
. The remainder function %
does not accept floating
point arguments, and neither do the bitwise boolean operations such as
logand
or the shift functions such as ash
.
There is a new arithmetic function, abs
, which returns the absolute
value of its argument. It handles both integers and floating point
numbers.
To convert an integer to floating point, use the function float
.
There are four functions to convert floating point numbers to integers;
they differ in how they round. truncate
rounds toward 0,
floor
rounds down, ceil
rounds up, and round
produces the nearest integer.
You can use logb
to extract the binary exponent of a floating
point number. More precisely, it is the logarithm base 2, rounded down
to an integer.
Emacs has several new mathematical functions that accept any kind of number as argument, but always return floating point numbers.
cos
sin
tan
acos
asin
atan
exp
log
log10
expt
sqrt
The new function string-to-number
now parses a string containing
either an integer or a floating point number, returning the number.
The format
function now handles the specifications `%e',
`%f' and `%g' for printing floating point numbers; likewise
message
.
The new variable float-output-format
controls how Lisp prints
floating point numbers. Its value should be nil
or a string.
If it is a string, it should contain a `%'-spec like those accepted
by printf
in C, but with some restrictions. It must start with
the two characters `%.'. After that comes an integer which is the
precision specification, and then a letter which controls the format.
The letters allowed are `e', `f' and `g'. Use `e' for exponential notation (`dig.digitseexpt'). Use `f' for decimal point notation (`digits.digits'). Use `g' to choose the shorter of those two formats for the number at hand.
The precision in any of these cases is the number of digits following the decimal point. With `e', a precision of 0 means to omit the decimal point. 0 is not allowed with `f' or `g'.
A value of nil
means to use the format `%.20g'.
No matter what the value of float-output-format
, printing ensures
that the result fits the syntax rules for a floating point number. If
it doesn't fit (for example, if it looks like an integer), it is
modified to fit. By contrast, the format
function formats
floating point numbers without requiring the output to fit the
syntax rules for floating point number.