Programming  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:

• ```str = 'The range for double is:\n\t%g to %g and\n\t %g to  %g';
sprintf(str, -realmax, -realmin, realmin, realmax)

ans =
The range for double is:
-1.79769e+308 to -2.22507e-308 and
2.22507e-308 to  1.79769e+308
```

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

• ```realmax + .0001e+308
ans =
Inf

-realmax - .0001e+308
ans =
-Inf
```

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

• ```x = 25.783;
```

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

• ```whos x
Name      Size                   Bytes  Class

x         1x1                        8  double array
```

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

• ```isfloat(x)
ans =
1
```

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

• ```x = 5.73 * 10^300;
```

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:

• ```y = int64(-589324077574);          % Create a 64-bit integer

x = double(y)                      % Convert to double
x =
-5.8932e+011

whos x
Name      Size                   Bytes  Class

x         1x1                        8  double array
```

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:

• ```str = 'The range for single is:\n\t%g to %g and\n\t %g to  %g';
sprintf(str, -realmax('single'), -realmin('single'), ...
realmin('single'), realmax('single'))

ans =
The range for single is:
-3.40282e+038 to -1.17549e-038 and
1.17549e-038 to  3.40282e+038
```

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

• ```realmax('single') + .0001e+038
ans =
Inf

-realmax('single') - .0001e+038
ans =
-Inf
```

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:

• ```x = single(25.783);
```

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`:

• ```whos x
Name      Size                   Bytes  Class

x         1x1                        4  single array
```

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

• ```isfloat(x)
ans =
1
```

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:

• ```y = int64(-589324077574);          % Create a 64-bit integer

x = single(y)                      % Convert to single
x =
-5.8932e+011

whos x
Name      Size                   Bytes  Class

x         1x1                        4  single array
```

Floating-Point Functions

See Floating-Point Functions for a list of functions most commonly used with floating-point numbers in MATLAB. Numeric Types Complex Numbers © 1994-2005 The MathWorks, Inc.