Sage and gp handle the p-adics in very similar ways (not surprising, since Sage includes gp).
A $p$-adic expansion is an infinite object and computers are finite, so we have to work with finite chunks of the expansion. Just as we do with real numbers, we need to choose a certain number of digits to keep, which we think of as the “$p$-adic precision” in which we are working.
In gp, this can be done by adding +O(p^k) at the end of a number.
If we want to see the number written “in base $p$”, in Sage, the right way to do it is to first specify in what context you are
working: K=Qp(5) tells Sage that $K$ is the $p$-adic numbers $5$ with $p = 5$. Then
you can find $p$-adic expansions by asking Sage to create a number in $K$, like
this: a=K(1/42). So the commands
If you prefer to see the digits, you can vary the options when you create the
field $K$:
produces
...02442002442002442003
You can even create both K and another entity, say Kd, with the “digits”
option. Then Kd(a) gives you the digits version of $a$.
Once we can enter numbers, it’s smooth sailing: you can add, subtract,
multiply, divide, and more. Whatever operation you ask it to do, gp will
either perform the operation or tell you that it can’t.
How about putting 5 in the denominator?
Did you see what happened there? Here’s the right way:
Notice that dividing by 5 reduces the 5-adic precision. This shouldn’t be
surprising.
Now let’s try something exotic:
$\sqrt{2a}$ will get error message:
Let's try logarithm of $a$:
So apparently even square roots and logs can (sometimes) work. We needn’t
worry right now about what the error message when we tried to compute $\sqrt{2a}$ means. We’ll find out soon.