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

**URL:** <https://discourse.julialang.org/t/why-the-sqrt-x-does-not-including-the-minus-part/91676>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 15, 2022, 5:13am UTC](https://discourse.julialang.org/t/why-the-sqrt-x-does-not-including-the-minus-part/91676 "2022-12-15T05:13:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 15, 2022, 5:13am UTC](https://discourse.julialang.org/t/why-the-sqrt-x-does-not-including-the-minus-part/91676/1 "2022-12-15T05:13:46Z")

</div>

HI all,

I try this:

```julia
sqrt(1)

```

why it only returns `1.0` not `+1` and `-1` ?

How to get all the roots? Which packages to use?

Thanks.

---

<div class="post-metadata">

**Author:** ![M-PERSIC](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/m-persic/32/51968_2.png) [@M-PERSIC](https://discourse.julialang.org/u/M-PERSIC)\
**Post date:** [December 15, 2022, 5:40am UTC](https://discourse.julialang.org/t/why-the-sqrt-x-does-not-including-the-minus-part/91676/2 "2022-12-15T05:40:49Z")

</div>

`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](https://math.stackexchange.com/questions/809424/can-the-square-root-of-a-real-number-be-negative).

You can write your own square root function:

```julia
# 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

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 15, 2022, 6:16am UTC](https://discourse.julialang.org/t/why-the-sqrt-x-does-not-including-the-minus-part/91676/3 "2022-12-15T06:16:50Z")

</div>

For integer input you also have `isqrt`, which should avoid rounding errors.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 15, 2022, 6:35am UTC](https://discourse.julialang.org/t/why-the-sqrt-x-does-not-including-the-minus-part/91676/4 "2022-12-15T06:35:00Z")

</div>

> [@M-PERSIC](#):
>
> ```julia
> # 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
> 
> ```

It is really amazing and works great this is the full code for anyone who want:

```julia
using SymPy

x = symbols("x")

# To calculate the integral between interval [-2,2]
fc = integrate(abs(x), (x, -2, 2))

# 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

# Find the values of c that satisfy the Mean Value Theorem for Integrals
# since f(x) = |x|, then f(c) = |c|
c = fc/(2-(-2))
println("f(c) = |c| = ", c)

println("c = ", square_root(c))

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [December 15, 2022, 9:09am UTC](https://discourse.julialang.org/t/why-the-sqrt-x-does-not-including-the-minus-part/91676/5 "2022-12-15T09:09:55Z")

</div>

As for the ±, that is just the definition of the square root. That is, while `(-1)^2 = 1`, `\sqrt{1} = +1`.
