Why the `sqrt(x)` does not including the minus part?

sqrt(x) necessarily returns a Float type as not every number is a perfect square. If you want to return an Integer without concerning oneself with loss of precision, you can do trunc(Int, sqrt(x)).

In this instance, it would personally seem redundant to return a Tuple showing the exact opposites of the same number as every number except 0 has both a positive and negative square root . There is a good discussion about this topic on mathematics stack exchange.

You can write your own square root function:

# keep Float type 
function square_root(x)
           return (sqrt(x), -sqrt(x))
end

# allow for loss of precision by returning as an Integer
function square_root_trunc(x)
           truncated_result = trunc(Int, sqrt(x))
           return (truncated_result, -truncated_result)
end
1 Like