Skip to main content
Engineering LibreTexts

1.3.1: log(), round(), floor(), and ceil() functions

  • Page ID
    87276
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

      You need to know some details about these MATLAB functions.

      log() vs. log10() vs. ln

      In algebra, we use ln(x) for the natural logarithm of x, which is base e, and we use log(x) for the logarithm base 10 of x.

      For a refresher, see this Wikipedia article: https://en.wikipedia.org/wiki/Logarithm [en.wikipedia.org]

      But, very early in computer programming's history, the log(x) function was defined to be the natural logarithm of x, which is base e, and so log10(x) was defined to be the logarithm base 10 of x. This tradition has been continued into current computer programming languages, possibly for compatibility. We can't change it, so we just have to remember to use log(x) for the natural logarithm of x, and log10(x) for the logarithm base 10 of x. Examples:

      %% log base 10
      c2 = log10(2)
      % c10 = 0.3010
      e = 2.7183
      ce = log10(e)
      % ce = 0.4343
      c10 = log10(10)
      % c10 = 1

      %% Natural log of x. In math, this is ln(x)
      d2 = log(2)
      % d1 = 0.69315
      e = 2.7183
      de = log(e)
      % de = 1
      d10 = log(10)
      %d10 = 2.3026

      round()

      Octave has a basic round() function. MATLAB has an option to round to a specified number of decimal places.

      round() in Octave

      y = round (x)

      Octave's round(x) function rounds the input to the nearest integer. The input may be a vector.

      Examples:

      >> round(2.3)
      ans =
           2
      >> round(2.5)
      ans =
           3

      >> round(pi)
      ans = 3

      >> round([2.9, 3.1, 5.4])
      ans =

         3   3   5

      Rounding for negative numbers:

      z1 = - 34.56

      >> round(z1)
      ans = -35

       

      Octave's round() function has no options. It accepts only 1 input, which can be a single number, a vector, or a matrix.

      How to round to a specific number of decimal places in Octave

      In Octave, you can't specify the number of decimal places directly, but this is a work-around:

      Let x  be the number that you want to round and n be the number of decimal places. Then use this code:

        y = round(x*10^n)/10^n
       

      This example shows how you can round pi to 3 decimal places:
       

      Example \(\PageIndex{1}\) Octave: round pi to 3 decimal places

      y = round(pi*10^3)/10^3

      Solution

      Add example text here.

      round() in MATLAB

      MATLAB's round(x) function rounds the input to the nearest integer when used with a single input variable. The input may be a vector or a matrix.

      Examples:

      a = 135.79246
       

      >> round(x)
      ans =
           136

      b = 79.421

      >> round(b) = 79
      ans =
           3

      >> round(pi)
      ans =
           3

      >> round([2.9, 3.1, 5.4])

      ans =
           3     3     5

      Rounding for negative numbers:

      >> y = -314.159
      y =
       -314.1590
      >> round(y)
      ans =
        -314

       

      MATLAB's round() function has an option. You may round to a specified number of decimal places by adding a 2nd input.

      This example shows how to round pi to 4 decimal places:

      >> round(pi,4)
      ans =
          3.1416

      Try these examples:

      % Round -1/3 to 2 decimal places.

      x = round(-1/3, 2)

      % Round 2/3 to 2 decimal places.

      x = round(2/3, 2)

      floor() function

      The floor() function always rounds a number down to the next lowest integer. It rounds negative numbers towards -infinity.

      It works the same in both MATLAB and Octave.

      Try these examples:

      x = floor(-1/3)
      x = floor(2/3)
      y = floor(4.6667)

      ceil() function

      MATLAB's ceil (ceiling) function always rounds up to the higher integer (towards +infinity). Try these examples:

      x = ceil(-1/3)
      y = ceil(2/3)
      z = ceil(4.6667)

       


      This page titled 1.3.1: log(), round(), floor(), and ceil() functions is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.