Well this is something that is always going to be a problem with floating point numbers. In fact, the square root of 2 is irrational, so no decimal will do it exactly. So one thing you need to ask is, does it actually need to be exact? Anything that will handle this exactly will be much slower (for many reasons), so you really need to make sure “that you need it”.
In SymPy you just take the squareroot of an expression. If you have a symbol
x, and you just
y=sqrt(x). Then you have a new expression for the squareroot of
x. When you input 2, that will give you something that the computer can write down as the square root of 2. But remember, it can never store that decimal because… well… it’s infinite.
You can create your own
Irrational by giving a function which generates the square root of two. That’s how
pi is implemented. But that’s likely more difficult than you need.
Lastly, there’s this algebraic number system in Julia:
But note, algebraic numbers will explode in complexity as you do operations, so their usage is limited.
But why do you need "exact square root"s? Likely you want to modify your problem if that’s what you’re looking for.