0

I am trying to make a simple quadratic formula solver for my own personal use. It works for the most part, but there's one problem: it only works when the answers are Rational numbers (i.e., it won't display sqrt(-1) because that's "i"). When it tries to perform the calculation and the answer isn't a rational, it will display "NaN". My code looks like this:

...*regular html*
<script type = "text/javascript">
    var aValue = prompt("What is your 'a' value?");
    var bValue = prompt("What's your 'b' value?");
    var cValue = prompt("What's your 'c' value?");

var quadFinder = function x_finder(a,b,c) {
    document.write((-1 * b + Math.sqrt(b*b - 4*a*c)) / 2*a);
    document.write("<br>");
    document.write((-1 * b - Math.sqrt(b*b - 4*a*c)) / 2*a);
};
quadFinder(aValue,bValue,cValue)

I know the function is all sound because it will work as long as the answer is only a number.

One other question: what is the Math. command that will round the number? I once put in a few numbers and it came out to some crazy number with around 10 decimal numbers after it.

uto998
  • 45
  • 1
  • 5
  • 1
    This may or may not be related to your question, but fyi you need to put parentheses around `(2*a)` otherwise your answer is multiplied by `a` rather than divided by it. – Matt Feb 13 '14 at 21:33
  • Javascript only supports real numbers, not complex numbers. – Barmar Feb 13 '14 at 21:33
  • 1
    So you want to know how to check for `NaN`, since `NaN !== NaN`? Well, one way is `r !== r`, since that’ll only be true for `NaN`, but if you prefer to be more obvious, there’s always `isNaN(r)`. (Also, by “rational”, do you mean “real”?) – Ry- Feb 13 '14 at 21:34
  • You will certainly find the rounding function in the [Math documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math) – kapa Feb 13 '14 at 21:34
  • As for rounding, make sure to round on *display* using `Number.prototype.toFixed` (or `Number.prototype.toPrecision`). `r.toFixed(2) // 1.20`, for example. – Ry- Feb 13 '14 at 21:35
  • @Matt, thanks! I will fix that (even though it works already). – uto998 Feb 13 '14 at 21:35
  • @BarMar, thanks for letting me know. So this is an impossible thing to do in JS? (thanks everyone else for the Math documentation.) – uto998 Feb 13 '14 at 21:36
  • Oh, are you saying you *want* it to support complex numbers? – Ry- Feb 13 '14 at 21:37
  • 1
    See http://stackoverflow.com/questions/15399340/how-to-calculate-with-imaginary-numbers-in-javascript – Barmar Feb 13 '14 at 21:38
  • Hope you realize prompt() returns a string and not a number, just getting lucky with the multiplication turning them to numbers. – epascarello Feb 13 '14 at 21:51

1 Answers1

0

You might try looking at the discriminant and catching complex cases:

var quadFinder = function x_finder(a,b,c) {
    var disc = b * b - 4 * a * c;
    if (disc >= 0){
        document.write((-1 * b + Math.sqrt(b*b - 4*a*c)) / 2*a);
        document.write("<br>");
        document.write((-1 * b - Math.sqrt(b*b - 4*a*c)) / 2*a);
    } else {
        var real = (-1 * b) / (2 * a);
        var complex = Math.sqrt(-disc)/(2 * a);
        document.write(real + " + " + complex + "i");
        document.write("<br>");
        document.write(real + " - " + complex + "i");

};
colcarroll
  • 3,632
  • 17
  • 25
  • 1
    In the real case, it should be `/ (2*a)`. And a better variant is to compute `w=-b-sign(b)*disc` to get the solutions in the numerically stable form `w/(2*a)` and `(2*c)/w`. – Lutz Lehmann Feb 14 '14 at 00:49