# Expressing formulas

Here's how you can enter your formulas. It's almost but not quite like ordinary math notation from the textbooks; there are differences because you have to type into input boxes instead of writing things out freehand. For instance, to express x2+1 you type in `x^2+1`.

## Basics

Adding and subtracting work as you'd expect: `x+5`, `1-x`.

To multiply, use `*`: `7*x` means 7 times x.

To divide, use `/`: `x/3` means x divided by 3.

x n is written `x^n`.

The square root of x is written `sqrt(x)`.

Putting all this together, here's a bigger example, a solution to the quadratic equation:
`sqrt(b^2 - 4*a*c) / (2*a)`.

## Reference Manual

 Feature Syntax Examples Numbers `1` `42.5` Variables `x` `longvariablename` x y `x ^ y` `3^2 = 9``2^2^3 = 2^8 = 256` Multiply, divide `x * y` `3*2 = 6` `x / y` `3/2 = 1.5` Add, subtract, negate `x + y` `3+2 = 5` `x - y` `3-2 = 1` `-x` `-3 = 0-3` Comparison `x < y` `2<3 = 1``2<2 = 0``3<2 = 0` `x <= y` `2<=3 = 1``2<=2 = 1``3<=2 = 0` `x = y` `2=3 = 0``2=2 = 1` `x <> y` `2<>3 = 1``2<>2 = 0` `x >= y` same as `y <= x` `x > y` same as `y < x` Conjunction `x and y` `1 and 1 = 1``1 and 0 = 0``0 and 0 = 0` Disjunction `x or y` `1 or 1 = 1``1 or 0 = 1``0 or 0 = 0` Absolute value `abs(x)` `abs(-2) = 2``abs(2) = 2` Arc-cosine `acos(x)` `acos(1) = 0` Arc-sine `asin(x)` `asin(1) = pi/2` Arc-tangent `atan(x)` `atan(1) = pi/4` `atan2(x, y)` `atan(-1, -1) = -3 pi / 4` Ceiling `ceil(x)` `ceil(3.5) = 4``ceil(-3.5) = -3` Cosine `cos(x)` `cos(0) = 1` e x `exp(x)` `exp(1) = 2.7182818284590451` Floor `floor(x)` `floor(3.5) = 3``floor(-3.5) = -4` Conditional `if(x, y, z)` `if(1, 42, 137) = 42``if(0, 42, 137) = 137` Natural logarithm `log(x)` `log(2.7182818284590451) = 1` Maximum `max(x, y)` `max(2, 3) = 3` Minimum `min(x, y)` `min(2, 3) = 2` Rounding `round(x)` `round(3.5) = 4``round(-3.5) = -4` Sine `sin(x)` `sin(pi/2) = 1` Square root `sqrt(x)` `sqrt(9) = 3` Tangent `tan(x)` `tan(pi/4) = 1` (approximately)

## Pitfalls

When you write `a+b*c`, should that mean to add a and b, and then multiply by c? Or is it add a to the result of multiplying b and c? In other words, which goes first, the `+` or the `*`? The answer is clear if you look at the original math notation, a+bc: the b and c go together, then we add their product to a. What if you wanted it the other way? In pencil-and-paper math, that'd be (a+b)c, and you can do the same thing at the computer as `(a+b)*c`. In general, operators listed earlier in the reference manual above, like `*`, come before later ones, like `+`.

Write `0 < x and x < 5`, rather than `0 < x < 5`. The latter is interpreted as `(0 < x) < 5`, which first evaluates `0 < x` yielding a truth value (1 or 0 for true or false), then compares that truth value to 5. Don't do that!

`a/b*c` is not `a/(b*c)`. In handwritten math notation you could write that with `a` above the division line and `b*c` vertically below it, but we can't do that here: everything is horizontal and so the program can't tell if you meant `(a/b)*c` or `a/(b*c)`. (It chooses the first, in fact.) When in doubt, use parentheses.

The program does not understand `sin x`, but requires `sin(x)` instead. This is because, if you said `sin x * y`, it'd be uncertain whether you meant `sin(x * y)` or ```(sin(x)) * y```. So all the functions need parentheses; the lessened ambiguity is worth the extra typing.

While you can refer to real numbers like pi and e and the square root of 2, this program can't represent them exactly; it only holds onto a fixed number of digits. For example, computing `tan(pi/4)` doesn't give 1 exactly, but 0.99999999999999989. You can get completely bogus answers if your formulas are too sensitive to these imprecisions; there isn't space here to treat this issue.