Skip to main content
Engineering LibreTexts

8.1: Number formats

  • Page ID
    87355
  • \( \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.

      Answer

      The effect of 'format long'
      a

      Result: -0.333333333333333 0 0.333333333333333 0.666666666666667

      A more complete explanation is at: Mathworks format help [www.mathworks.com]

      .

      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.

      B. Start with Fibonnaci code of section 2.6.

      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.

      Answer

      Add texts here. Do not delete this text first.

      .


      This page titled 8.1: Number formats is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

      • Was this article helpful?