# 2.7.1: Number formats

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$ $$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

A straight-forward way to learn MATLAB's number formats is to try them and see their effects.

##### Exercise $$\PageIndex{1}$$ Number formats

Create a MATLAB .m or .mlx file with the following.

Assign this vector:
a = [-1/3, 0, 1/3, 2/3]

Enter each of the formats below, followed by the variable 'a', in order to see the effect of the format on a number.

format long

a

format bank

a

format short e

a

format long e

a

format short eng

a
format long eng

a

format short g

a

format long g

a

format rat % Ratio

a

format short % Reset to the default format.

The effect of 'format long'
a

Result: -0.333333333333333 0 0.333333333333333 0.666666666666667

A more complete explanation is at: https://www.mathworks.com/help/matlab/ref/format.html

.

##### Fibonnaci_2Exercise $$\PageIndex{1}$$

A. Create an m-file script for the Fibonnaci example in this section.

Call your file Fibonnaci_2_KennedyJ.m, but replace KennedyJ with your last name and 1st initial.

Add this documentation to the top of the script file:

% Set n = the index of particular Fibonocci number to be computed (precondition)

C. The division in last line of the code can result in a number that not an exact integer.

This can be demonstrated changing the the format to display about 15 decimal places with this line of code:

format long

Set n = 10 and run the script. The result will be

ans = 55.000000000000014

Fix this by changing the last line to be:

ans = round(diff / s5)

D. Test your script with n = 4, n = 5, and n =10

Note: n is a precondition for this script, so n needs to be in the command window, before running the the script.

This allows n to be set to various different value without changing the script.