TExcelTAD
Home › MATLAB / Octave › Numbers & Display Formats

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.

matlab · try it yourself
a = 3;
b = 3.0;
c = 3e0;
disp(isequal(a, b, c))
disp(class(a))
big = 6.022e23
tiny = 1.6e-19
1
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:

matlab · try it yourself
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
matlab · try it yourself
format rat
disp(0.75)
disp(pi)
format bank
disp(1234.5678)
format
3/4
355/113
1234.57

Tip: When you need a specific number of decimals in printed results, prefer fprintf('%.4f', x) over format. 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:

matlab · try it yourself
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

matlab · try it yourself
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
matlab · try it yourself
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):

matlab · try it yourself
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?

  1. The precision of calculations
  2. Only how many digits are displayed
  3. The type of new variables
  4. Nothing in scripts

How should you test whether x is NaN?

  1. x == NaN
  2. isnan(x)
  3. x === NaN
  4. x == 'NaN'

What is fix(-3.8)?

  1. -4
  2. -3
  3. 3
  4. -3.8