# Julia Language Using the REPL as a Calculator

## Example

The Julia REPL is an excellent calculator. We can start with some simple operations:

julia> 1 + 1
2

julia> 8 * 8
64

julia> 9 ^ 2
81


The ans variable contains the result of the last calculation:

julia> 4 + 9
13

julia> ans + 9
22


We can define our own variables using the assignment = operator:

julia> x = 10
10

julia> y = 20
20

julia> x + y
30


Julia has implicit multiplication for numeric literals, which makes some calculations quicker to write:

julia> 10x
100

julia> 2(x + y)
60


If we make a mistake and do something that is not allowed, the Julia REPL will throw an error, often with a helpful tip on how to fix the problem:

julia> 1 ^ -1
ERROR: DomainError:
Cannot raise an integer x to a negative power -n.
Make x a float by adding a zero decimal (e.g. 2.0^-n instead of 2^-n), or write
1/x^n, float(x)^-n, or (x//1)^-n.
in power_by_squaring at ./intfuncs.jl:82
in ^ at ./intfuncs.jl:106

julia> 1.0 ^ -1
1.0


To access or edit previous commands, use the (Up) key, which moves to the last item in history. The moves to the next item in history. The and keys can be used to move and make edits to a line.

Julia has some built-in mathematical constants, including e and pi (or π).

julia> e
e = 2.7182818284590...

julia> pi
π = 3.1415926535897...

julia> 3π
9.42477796076938


We can type characters like π quickly by using their LaTeX codes: press \, then p and i, then hit the Tab key to substitute the \pi just typed with π. This works for other Greek letters and additional unicode symbols.

We can use any of Julia's built-in math functions, which range from simple to fairly powerful:

julia> cos(π)
-1.0

julia> besselh(1, 1, 1)
0.44005058574493355 - 0.7812128213002889im


Complex numbers are supported using im as an imaginary unit:

julia> abs(3 + 4im)
5.0


Some functions will not return a complex result unless you give it a complex input, even if the input is real:

julia> sqrt(-1)
ERROR: DomainError:
sqrt will only return a complex result if called with a complex argument. Try
sqrt(complex(x)).
in sqrt at math.jl:146

julia> sqrt(-1+0im)
0.0 + 1.0im

julia> sqrt(complex(-1))
0.0 + 1.0im


Exact operations on rational numbers are possible using the // rational division operator:

julia> 1//3 + 1//3
2//3


See the Arithmetic topic for more about what sorts of arithmetic operators are supported by Julia.

## Dealing with Machine Precision

Note that machine integers are constrained in size, and will overflow if the result is too big to be stored:

julia> 2^62
4611686018427387904

julia> 2^63
-9223372036854775808


This can be prevented by using arbitrary-precision integers in the computation:

julia> big"2"^62
4611686018427387904

julia> big"2"^63
9223372036854775808


Machine floating points are also limited in precision:

julia> 0.1 + 0.2
0.30000000000000004


More (but still limited) precision is possible by again using big:

julia> big"0.1" + big"0.2"
3.000000000000000000000000000000000000000000000000000000000000000000000000000017e-01


Exact arithmetic can be done in some cases using Rationals:

julia> 1//10 + 2//10
3//10