Numbers & Display Formats
By default every number in MATLAB is a double: a 64-bit floating-point value with about 15–16 significant decimal digits. Whole numbers, decimals and huge or tiny values all use the same type, so 3, 3.0 and 3e0 are identical.
a = 3;
b = 3.0;
c = 3e0;
disp(isequal(a, b, c))
disp(class(a))
big = 6.022e23
tiny = 1.6e-191
double
big = 6.0220e+23
tiny = 1.6000e-19
e notation means "times ten to the power": 6.022e23 is 6.022 × 10²³.
Display formats
The format command changes how numbers are displayed, never how they are stored or computed. The default, format short, shows about 5 significant digits:
x = 1/3;
format short
x
format long
x
format short
disp(pi * 1000)x = 0.3333
x = 0.333333333333333
3141.6
| Command | Effect |
|---|---|
format short |
About 5 significant digits (default) |
format long |
About 15–16 significant digits |
format short g / format long g |
Pick fixed or exponential, whichever is shorter |
format short e / format long e |
Always exponential notation |
format bank |
Two decimals (money) |
format rat |
Rational approximation, e.g. 1/3 |
format |
Back to the default |
format rat
disp(0.75)
disp(pi)
format bank
disp(1234.5678)
format3/4
355/113
1234.57
Tip: When you need a specific number of decimals in printed results, prefer
fprintf('%.4f', x)overformat. It is explicit, doesn't change global state, and looks the same in MATLAB and Octave.
Floating-point surprises
Doubles store numbers in binary, and most decimal fractions (like 0.1) have no exact binary representation. Tiny rounding errors appear:
s = 0.1 + 0.2;
disp(s == 0.3)
fprintf('%.20f\n', s);
disp(abs(s - 0.3) < 1e-12)
disp(eps)0
0.30000000000000004441
1
2.2204e-16
Never compare computed decimals with ==; check whether the difference is smaller than a tolerance instead. eps is the gap between 1 and the next representable double, a good sense of the precision available.
Special values
values = [1/0, -1/0, 0/0, Inf - Inf];
disp(values)
disp(isinf(values))
disp(isnan(values))
fprintf('largest double: %g\n', realmax);
fprintf('smallest normal double: %g\n', realmin); Inf -Inf NaN NaN
1 1 0 0
0 0 1 1
largest double: 1.79769e+308
smallest normal double: 2.22507e-308
NaN is special: it is not equal to anything, not even itself, so x == NaN is always false. Always use isnan(x) to test for it.
Rounding
| Function | Rule | -2.5 |
2.5 |
2.7 |
|---|---|---|---|---|
round |
Nearest, halves away from zero | -3 | 3 | 3 |
floor |
Toward −∞ | -3 | 2 | 2 |
ceil |
Toward +∞ | -2 | 3 | 3 |
fix |
Toward zero | -2 | 2 | 2 |
v = [-2.5 2.5 2.7];
disp(round(v))
disp(floor(v))
disp(ceil(v))
disp(fix(v))
disp(round(3.14159 * 100) / 100) % round to 2 decimals -3 3 3
-3 2 2
-2 3 3
-2 2 2
3.1400
The last line shows the portable trick for rounding to a number of decimals. (MATLAB also accepts round(x, 2), but Octave does not.)
Complex numbers
MATLAB handles complex numbers natively. Write them with i or j, or with complex(re, im):
z = 3 + 4i;
fprintf('real %g, imag %g, abs %g\n', real(z), imag(z), abs(z));
disp(sqrt(-9))
disp(z * conj(z))real 3, imag 4, abs 5
0 + 3i
25
Test yourself
What does format long change?
- The precision of calculations
- Only how many digits are displayed
- The type of new variables
- Nothing in scripts
How should you test whether x is NaN?
x == NaNisnan(x)x === NaNx == 'NaN'
What is fix(-3.8)?
- -4
- -3
- 3
- -3.8