## Numeric to Symbolic Conversion

This topic shows how Symbolic Math Toolbox™ converts numbers into symbolic form. For an overview of symbolic and numeric arithmetic, see Choose Numeric or Symbolic Arithmetic.

To convert numeric input to symbolic form, use the `sym` command. By default, `sym` returns a rational approximation of a numeric expression.

```t = 0.1; sym(t)```
```ans = 1/10```

`sym` determines that the double-precision value `0.1` approximates the exact symbolic value `1/10`. In general, `sym` tries to correct the round-off error in floating-point inputs to return the exact symbolic form. Specifically, `sym` corrects round-off error in numeric inputs that match the forms p/q, pπ/q, (p/q)1/2, 2q, and 10q, where p and q are modest-sized integers.

For these forms, demonstrate that `sym` converts floating-point inputs to the exact symbolic form. First, numerically approximate 1/7, pi, and $1/\sqrt{2}$.

```N1 = 1/7 N2 = pi N3 = 1/sqrt(2)```
```N1 = 0.1429 N2 = 3.1416 N3 = 0.7071```

Convert the numeric approximations to exact symbolic form. `sym` corrects the round-off error.

```S1 = sym(N1) S2 = sym(N2) S3 = sym(N3)```
```S1 = 1/7 S2 = pi S3 = 2^(1/2)/2```

To return the error between the input and the estimated exact form, use the syntax `sym(num,'e')`. See Conversion to Rational Symbolic Form with Error Term.

You can force `sym` to accept the input as is by placing the input in quotes. Demonstrate this behavior on the previous input `0.142857142857143`. The `sym` function does not convert the input to `1/7`.

`sym('0.142857142857143')`
```ans = 0.142857142857143```

When you convert large numbers, use quotes to exactly represent them. Demonstrate this behavior by comparing `sym(133333333333333333333)` with `sym('133333333333333333333')`.

```sym(1333333333333333333) sym('1333333333333333333')```
```ans = 1333333333333333248 ans = 1333333333333333333```

You can specify the technique used by `sym` to convert floating-point numbers using the optional second argument, which can be `'f'`, `'r'`, `'e'`, or `'d'`. The default flag is `'r'`, for rational form.

### Conversion to Rational Symbolic Form

Convert input to exact rational form by calling `sym` with the `'r'` flag. This is the default behavior when you call `sym` without flags.

`sym(t, 'r')`
```ans = 1/10```

### Conversion by Using Floating-Point Expansion

If you call `sym` with the flag `'f'`, `sym` converts double-precision, floating-point numbers to their numeric value by using `N*2^e`, where `N` and `e` are the exponent and mantissa respectively.

Convert `t` by using a floating-point expansion.

`sym(t, 'f')`
```ans = 3602879701896397/36028797018963968```

### Conversion to Rational Symbolic Form with Error Term

If you call `sym` with the flag `'e'`, `sym` returns the rational form of `t` plus the error between the estimated, exact value for `t` and its floating-point representation. This error is expressed in terms of `eps` (the floating-point relative precision).

Convert `t` to symbolic form. Return the error between its estimated symbolic form and its floating-point value.

`sym(t, 'e')`
```ans = eps/40 + 1/10```

The error term `eps/40` is the difference between `sym('0.1')` and `sym(0.1)`.

### Conversion to Decimal Form

If you call `sym` with the flag `'d'`, `sym` returns the decimal expansion of the input. The `digits` function specifies the number of significant digits used. The default value of `digits` is 32.

`sym(t,'d')`
```ans = 0.10000000000000000555111512312578```

Change the number of significant digits by using `digits`.

```digitsOld = digits(7); sym(t,'d')```
```ans = 0.1```

For further calculations, restore the old value of `digits`.

`digits(digitsOld)`