Programming Previous page   Next Page

Floating-Point Numbers

MATLAB represents floating-point numbers in either double-precision or single-precision format. The default is double precision, but you can make any number single precision with a simple conversion function.

This section covers

Double-Precision Floating Point

MATLAB constructs the double data type according to IEEE Standard 754 for double precision. Any value stored as a double requires 64 bits, formatted as shown in the table below:

Bits
Usage
63
Sign (0 = positive, 1 = negative)
62 to 52
Exponent, biased by 1023
51 to 0
Fraction f of the number 1.f

Maximum and Minimum Double-Precision Values.   The MATLAB functions realmax and realmin return the maximum and minimum values that you can represent with the double data type:

Numbers larger than realmax or smaller than -realmax are assigned the values of positive and negative infinity respectively:

Creating Double-Precision Data.   Since the default numeric type for MATLAB is double, you can create a double with a simple assignment statement:

The whos function shows that MATLAB has created a 1-by-1 array of type double for the value you just stored in x:

Use the isfloat function if you just want to verify that x is a floating-point number:

The next statement creates a much larger value. This value requires a double; you could not store this using the single-precision data type:

Converting to Double Precision.   You can convert other numeric data, characters or strings, and logical data to double precision using the MATLAB function, double. This example converts a signed integer to double-precision floating point:

Single-Precision Floating Point

MATLAB constructs the single data type according to IEEE Standard 754 for single precision. Any value stored as a single requires 32 bits, formatted as shown in the table below:

Bits
Usage
31
Sign (0 = positive, 1 = negative)
30 to 23
Exponent, biased by 127
22 to 0
Fraction f of the number 1.f

Maximum and Minimum Single-Precision Values.   The MATLAB functions realmax and realmin, when called with the argument 'single', return the maximum and minimum values that you can represent with the single data type:

Numbers larger than realmax('single') or smaller than -realmax('single') are assigned the values of positive and negative infinity respectively:

Creating Single-Precision Data.   Because MATLAB stores numeric data as a double by default, you need to use the single conversion function to create a single-precision number:

The whos function shows that MATLAB has created a 1-by-1 array of type single. This value requires only 4 bytes compared with the 8 bytes used to store a double:

Use the isfloat function if you just want to verify that x is a floating-point number:

Converting to Single Precision.   You can convert other numeric data, characters or strings, and logical data to single precision using the MATLAB function, single. This example converts a signed integer to single-precision floating point:

Floating-Point Functions

See Floating-Point Functions for a list of functions most commonly used with floating-point numbers in MATLAB.


Previous page  Numeric Types Complex Numbers Next page

© 1994-2005 The MathWorks, Inc.