Academic Block

Floating Point Numbers in MATLAB Get Certified

Floating point numbers are used in MATLAB to represent non-integer values, including decimals and very large or small numbers. MATLAB follows the IEEE 754 standard for floating-point arithmetic.

Understanding Floating Point Numbers

Floating point numbers consist of three parts:

  • Sign: Indicates whether the number is positive or negative.
  • Exponent: Determines the scale or magnitude of the number.
  • Fraction: Represents the precision of the number.

Creating Floating Point Numbers

Floating point numbers can be created simply by using decimals in MATLAB:

% Examples of floating point numbers
a = 3.14;
b = -0.001;
c = 2.718e3;   % Scientific notation = 2718

disp(a);
disp(b);
disp(c);

Expected output:

3.14
-0.001
2718

Floating Point Precision

MATLAB supports two primary precision types:

  • Single Precision: Uses 4 bytes (32 bits).
  • Double Precision: Uses 8 bytes (64 bits) and is the default in MATLAB.
% Checking precision of numbers
a = single(3.14);   % Single precision
b = 3.14;           % Double precision (default)

disp(a);
disp(b);
disp(class(a));     % 'single'
disp(class(b));     % 'double'

Common Issues with Floating Point Numbers

Floating point arithmetic is not always exact due to precision limits. For example:

% Floating point precision example
a = 0.1 + 0.2;
disp(a == 0.3);   % Returns false (0)
disp(a);          % Slightly different from 0.3

Expected output:

0
0.30000000000000004

Use the eps function to determine the precision threshold:

% Checking precision threshold
disp(eps);        % Machine epsilon for double
disp(eps(1.0));   % Epsilon relative to 1.0

Expected output:

2.2204e-16
2.2204e-16

Floating Point Operations

Floating point numbers support standard arithmetic operations:

% Basic arithmetic with floating point numbers
a = 5.5;
b = 2.2;
add = a + b;
sub = a - b;
mul = a * b;
div = a / b;

disp(add);
disp(sub);
disp(mul);
disp(div);

Expected output:

7.7
3.3
12.1
2.5

Scientific Notation

Scientific notation is a convenient way to handle very large or small numbers:

% Examples of scientific notation
a = 3.14e2;   % 3.14 × 10² = 314
b = 1.6e-3;   % 1.6 × 10⁻³ = 0.0016

disp(a);
disp(b);

Expected output:

314
0.0016

Handling Special Numbers

MATLAB can handle special floating-point numbers such as infinity and NaN:

% Infinity and NaN examples
a = 1/0;     % Positive infinity
b = -1/0;    % Negative infinity
c = 0/0;     % NaN (Not-a-Number)

disp(a);
disp(b);
disp(c);

Expected output:

Inf
-Inf
NaN

Comparing Floating Point Numbers

Direct comparison of floating-point numbers can be unreliable. Use a tolerance instead:

% Comparing floating-point numbers
a = 0.1 + 0.2;
b = 0.3;
tol = 1e-10;          % Tolerance

if abs(a - b) < tol
    disp('a and b are approximately equal');
else
    disp('a and b are not equal');
end

Expected output:

a and b are approximately equal

Rounding Floating Point Numbers

MATLAB provides several functions to round floating-point numbers:

% Rounding examples
a = 3.14159;
round_a = round(a);   % Nearest integer
floor_a = floor(a);   % Round down
ceil_a  = ceil(a);    % Round up

disp(round_a);
disp(floor_a);
disp(ceil_a);

Expected output:

3
3
4

Floating Point Limits

Understanding the limits of floating-point representation is essential:

% Checking floating-point limits
max_val = realmax;   % Largest finite double
min_val = realmin;   % Smallest positive normalized double

disp(max_val);
disp(min_val);

Expected output:

1.7977e+308
2.2251e-308

Simulating Overflow and Underflow

Operations exceeding the limits of floating-point representation cause overflow or underflow:

% Overflow and underflow examples
overflow  = realmax * 2;   % Becomes Inf
underflow = realmin / 2;   % Becomes 0

disp(overflow);
disp(underflow);

Expected output:

Inf
0

Precision of Floating Point Arithmetic

Demonstrating how small changes can affect results:

% Precision effects
x = 1.0;
y = x + eps;      % Adding machine epsilon
z = x + eps/2;    % Adding half machine epsilon

disp(x == y);     % false (0)
disp(x == z);     % true  (1)

Expected output:

0
1

Practical Problem: Summing a Large Series

Summing a large series of floating-point numbers can introduce errors due to limited precision:

% Summing a large series
n = 1e7;
series = 1 ./ (1:n);     % Harmonic series
sum_result = sum(series);

disp(sum_result);

Expected output:

16.6953   (approximate value; exact result depends on MATLAB version)

Useful MATLAB Functions for Floating Point Numbers

Function
Explanation
eps
Returns the distance from 1.0 to the next larger floating-point number (machine epsilon).
realmax
Returns the largest finite floating-point number.
realmin
Returns the smallest positive normalized floating-point number.
isnan
Returns true if the value is NaN (Not-a-Number).
isinf
Returns true if the value is infinite.
single
Converts a number to single-precision (32-bit) floating point.
double
Converts a number to double-precision (64-bit) floating point (the default).
isfloat
Returns true if the variable is a floating-point type (single or double).
round
Rounds each element to the nearest integer.

Practice Questions

Test Yourself

1. Create a floating point number using scientific notation and check its class.

2. Add two single precision numbers and compare the result with double precision addition.

3. Write a MATLAB script to find the smallest number that can be added to 1.0 to produce a value greater than 1.0.

4. Experiment with the isnan and isinf functions by dividing numbers by zero.