# 2.7: Floating-Point Numbers


In the previous section, the result we computed was 55.0000. Since the Fibonacci numbers are integers, you might have been surprised to see the zeros after the decimal point.

They are there because MATLAB performs calculations using floating-point numbers. With floating-point numbers, integers can be represented exactly, but most fractions cannot.

For example, if you compute the fraction 2/3, the result is only approximate—the correct answer has an infinite number of 6s:

>> 2/3
ans = 0.6666

It’s not as bad as this example makes it seem: MATLAB uses more digits than it shows by default. You can use the format command to change the output format:

>> format long
>> 2/3
ans = 0.666666666666667

In this example, the first 14 digits are correct; the last one has been rounded off.

Large and small numbers are displayed in scientific notation. For example, if we use the built-in function factorial to compute $$100!$$, we get the following result:

>> factorial(100)
ans = 9.332621544394410e+157

The e in this notation is not the transcendental number known as $$e$$; it’s just an abbreviation for “exponent.” So this means that $$100!$$ is approximately $$9.33 \times 10^{157}$$. The exact solution is a 158-digit integer, but with double-precision floating-point numbers, we only know the first 16 digits.

You can enter numbers using the same notation.

>> speed_of_light = 3.0e8
speed_of_light = 300000000

Although the floating-point format can represent very large and small numbers, there are limits. The predefined variables realmax and realmin contain the largest and smallest numbers MATLAB can handle.

>> realmax
ans = 1.797693134862316e+308

>> realmin
ans = 2.225073858507201e-308

If the result of a computation is too big, MATLAB “rounds up” to infinity.

>> factorial(170)
ans = 7.257415615307994e+306

>> factorial(171)
ans = Inf

Division by zero also returns Inf.

>> 1/0
ans = Inf

For operations that are undefined, MATLAB returns NaN, which stands for “not a number.”

>> 0/0
ans = NaN

This page titled 2.7: Floating-Point Numbers is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.