-5

I have been try to draw the function of y^2=4*a*x but I have run into a problem. when I use the math.sqrt function to find the square root of a value which has two answer +or- something i only get the positive value. i.e if i found the sqrt of 4 it would return +2 instead of + or -2.

Any help would be deeply appreciated.

opeyemi
  • 83
  • 1
  • 9
  • 5
    Because that's the definition of the function... If you want the negative value, you can always invert what you are given to get it... – Servy Aug 23 '13 at 14:47
  • How do you expect a function to return ±2? Are you using a quantum computer? – Kevin Aug 23 '13 at 15:10

4 Answers4

2

You can write your own method to return both values:

public static IEnumerable<double> Sqrt(double d)
{
    var result = Math.Sqrt(d);
    yield return result;
    yield return -result;
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
1

You can multiply the answer with -1 also and get both.

FelProNet
  • 689
  • 2
  • 10
  • 25
0

If you read the doc (MSDN is really complete and well made, use it before asking questions) you will see that with a parameter 0 or positive, Math.Sqrt returns only "The positive square root of d."

If you need the negative value, you have to do it yourself, for example like this :

double value = 4;
double positiveSqrt = Math.Sqrt(value);
double negativeSqrt = positiveSqrt * -1;
Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
0

If you really want a function that returns both positive and negative square roots, you could write your own pretty easily:

public static double[] Sqrts(double d) {
    var v = Math.Sqrt(d);
    return v == 0 ? new[] { v } : new[] { v, -v };
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331