## Choose Numeric or Symbolic Arithmetic

Symbolic Math Toolbox™ operates on numbers by using either numeric or symbolic arithmetic. In numeric arithmetic, you represent numbers in floating-point format using either double precision or variable precision. In symbolic arithmetic, you represent numbers in their exact form. This topic compares double-precision, variable-precision, and symbolic arithmetic.

### Double-Precision Arithmetic

Numeric computations in MATLAB® use double-precision arithmetic by default. For example, evaluate the expressions 10001/1001, π, and $\sqrt{2}$. The results are converted to double-precision values.

```x = 10001/1001 y = pi z = sqrt(2)```
```x = 9.9910 y = 3.1416 z = 1.4142```

For more information about double-precision arithmetic, see Floating-Point Numbers. This arithmetic is recommended when you do not have Symbolic Math Toolbox or are using functions that do not accept symbolic input. Otherwise, exact symbolic arithmetic and variable-precision arithmetic are recommended. To convert a symbolic value to double precision, use the `double` function.

### Variable-Precision Arithmetic

Variable-precision arithmetic using `vpa` is the recommended approach for numeric calculations in Symbolic Math Toolbox. You can specify the number of significant digits when performing calculations with variable-precision arithmetic.

For example, use `vpa` to evaluate the fraction 10001/1001. By default, `vpa` evaluates inputs to 32 significant digits. Approximate the fraction 10001/1001 to at least 32 significant digits.

`vpa(10001/1001)`
```ans = 9.991008991008991008991008991009```

Approximate the fraction to at least 8 significant digits. Change the number of significant digits by using the `digits` function.

```digits(8); vpa(10001/1001)```
```ans = 9.991009```

In variable-precision arithmetic, you can increase the number of significant digits for greater precision. Alternatively, you can decrease the number of significant digits for faster computations and decreased memory usage.

### Symbolic Arithmetic

Symbolic Math Toolbox provides the `sym` and `syms` functions to perform exact symbolic computations. In symbolic arithmetic, you can perform computations involving numbers and variables in their exact form, such as `x/2`, `2^(1/2)`, or `pi`. The following three examples show several calculations that are performed in symbolic arithmetic.

#### Express Irrational Numbers

Use `sym` to create symbolic numbers. Express the irrational numbers π and $\sqrt{2}$ in symbolic form.

```x = sym(pi) y = sqrt(sym(2)) ```
```x = pi y = 2^(1/2)```

#### Perform Calculations with Large Integers

When you declare a number, MATLAB automatically converts the number to double precision. For example, declare the integer `80435758145817515` as the input argument of `sym`. The number loses its accuracy since it is bigger than the largest consecutive integer `flintmax` in double precision, which is `2^53`.

```Z = 80435758145817515 Zinaccurate = sym(80435758145817515)```
```Z = 8.0436e+16 Zinaccurate = 80435758145817520```
To declare a large integer as symbolic number accurately, use a character vector with single quotation marks as the input argument of `sym`.

`Zaccurate = sym('80435758145817515')`
```Zaccurate = 80435758145817515```

You can then perform calculations with large integers using symbolic arithmetic accurately. For example, evaluate the sum of the cubes of three large integers.

```Z1 = sym('80435758145817515') Z2 = sym('12602123297335631') Z3 = sym('-80538738812075974') Zsum = Z1^3 + Z2^3 + Z3^3```
```Z1 = 80435758145817515 Z2 = 12602123297335631 Z3 = -80538738812075974 Zsum = 42```

#### Solve Mathematical Equations

With symbolic arithmetic, you can solve a mathematical equation. For example, solve the quadratic equation ax2 + bx + c = 0. Use `syms` to declare the variable x and the coefficients a, b, and c in the quadratic equation.

```syms a b c x eqn = a*x^2 + b*x + c == 0;```

Find the solutions using `solve` and return them as symbolic expressions.

`sols = solve(eqn,x)`
```sols = -(b + (b^2 - 4*a*c)^(1/2))/(2*a) -(b - (b^2 - 4*a*c)^(1/2))/(2*a) ```

Use `subs` to substitute symbolic values for the coefficients. Set a = 1, b = 2, and c = 3. Return the solutions of the quadratic equation as symbolic numbers.

`solsSym = subs(sols,[a b c],[1 2 3])`
```solsSym = - (8^(1/2)*1i)/2 - 1 (8^(1/2)*1i)/2 - 1```

You can then convert the symbolic solutions to floating-point format in double precision or variable precision.

```digits(32); solsDouble = double(solsSym) solsVpa = vpa(solsSym)```
```solsDouble = -1.0000 - 1.4142i -1.0000 + 1.4142i solsVpa = - 1.0 - 1.4142135623730950488016887242097i - 1.0 + 1.4142135623730950488016887242097i```

### Comparisons of Numeric and Symbolic Arithmetic

The table below compares double-precision, variable-precision, and symbolic arithmetic.

Double PrecisionVariable PrecisionSymbolic
Example 1: Evaluate sin(π)
```a = pi sin(pi)```
```a = 3.1416 ans = 1.2246e-16```
```b = vpa(pi) sin(b)```
```b = 3.1415926535897932384626433832795 ans = -3.2101083013100396069547145883568e-40```
```c = sym(pi) sin(c)```
```c = pi ans = 0```
Example 2: Evaluate 1 - 3*(4/3 - 1)
```a = 4/3 1 - 3*(a - 1)```
```a = 1.3333 ans = 2.2204e-16```
```digits(16); b = vpa(4/3) 1 - 3*(b - 1)```
```b = 1.333333333333333 ans = 3.308722450212111e-24```
```c = sym(4)/3 1 - 3*(c - 1)```
```c = 4/3 ans = 0```
Functions Used`double``vpa`
`digits`
`sym`
Data Type`double``sym``sym`
Round-Off ErrorsYes, the answer has 16 digits of precision.Yes, the number of digits depends on the precision used.No, the results are exact.
SpeedFasterFaster, depending on the precision usedSlowest
Memory UsageLeastVariable, depending on the precision usedGreatest