# 1.2.2: Rounding, log, and atan

$$\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}}$$

## Rounding

MATLAB's round() function rounds a number to a specified number of decimal places. 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)

% Round 4.6667 to the nearest integer.

y = round(4.6667, 0)

MATLAB's floor() function always rounds a number down to the next lowest integer. It rounds negative numbers towards -infinity. Try these examples:

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

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)

## 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.

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

## atan

atan() arctangent. As you know from trigonometry, the tangent of an angle theta = sin(theta) / cos(theta).

Since sin(theta + pi) = -sin(theta) and cos(theta + pi) = -cos(theta), then tan(theta + pi) = -sin(theta) / (-cos(theta)) = sin(theta) / cos(theta) = tan(theta).

For example,

tan(pi/3) = 1.7321
and

tan(-2*pi/3) = 1.7321

So the arctangent of 1.7321 is ambiguous.

In MATLAB and Octave, atan(1.7321) = 1.0472 = pi/3

MATLAB has a 2nd function, atan2, for which you input both the numerator and denominator of the tangent. This lets you resolve the ambiguity. For example:

sin(4*pi/3) = -0.8660

cos(4*pi/3) = -0.5000

atan2(-0.8660, -0.5000) = -2.0944 = -2*pi/3

Note that the inputs for the sin() cos() and tan() functions are in radians and the outputs of the atan() and atan2() functions are in radians.

When you want to work in degrees, use the functions sind(), cosd(), tand(), atand() and atan2d.

This page titled 1.2.2: Rounding, log, and atan is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.