Next: , Previous: Integer Basics, Up: Numbers

3.2 Floating Point Basics

Floating point numbers are useful for representing numbers that are not integral. The precise range of floating point numbers is machine-specific; it is the same as the range of the C data type double on the machine you are using. Emacs uses the IEEE floating point standard, which is supported by all modern computers.

The read syntax for floating point numbers requires either a decimal point (with at least one digit following), an exponent, or both. For example, ‘1500.0’, ‘15e2’, ‘15.0e2’, ‘1.5e3’, and ‘.15e4’ are five ways of writing a floating point number whose value is 1500. They are all equivalent. You can also use a minus sign to write negative floating point numbers, as in ‘-1.0’.

Emacs Lisp treats -0.0 as equal to ordinary zero (with respect to equal and =), even though the two are distinguishable in the IEEE floating point standard.

The IEEE floating point standard supports positive infinity and negative infinity as floating point values. It also provides for a class of values called NaN or “not-a-number”; numerical functions return such values in cases where there is no correct answer. For example, (/ 0.0 0.0) returns a NaN. (NaN values can also carry a sign, but for practical purposes there's no significant difference between different NaN values in Emacs Lisp.)

When a function is documented to return a NaN, it returns an implementation-defined value when Emacs is running on one of the now-rare platforms that do not use IEEE floating point. For example, (log -1.0) typically returns a NaN, but on non-IEEE platforms it returns an implementation-defined value.

Here are the read syntaxes for these special floating point values:

positive infinity
negative infinity
0.0e+NaN’ or ‘-0.0e+NaN’.
— Function: isnan number

This predicate tests whether its argument is NaN, and returns t if so, nil otherwise. The argument must be a number.

The following functions are specialized for handling floating point numbers:

— Function: frexp x

This function returns a cons cell (sig . exp), where sig and exp are respectively the significand and exponent of the floating point number x:

          x = sig * 2^exp

sig is a floating point number between 0.5 (inclusive) and 1.0 (exclusive). If x is zero, the return value is (0 . 0).

— Function: ldexp sig &optional exp

This function returns a floating point number corresponding to the significand sig and exponent exp.

— Function: copysign x1 x2

This function copies the sign of x2 to the value of x1, and returns the result. x1 and x2 must be floating point numbers.

— Function: logb number

This function returns the binary exponent of number. More precisely, the value is the logarithm of |number| base 2, rounded down to an integer.

          (logb 10)
               ⇒ 3
          (logb 10.0e20)
               ⇒ 69