2

I am new to programming in general and finally started to work on a small project -- that is to build a Quadratic Function Calculator and I've come across the problem of the language not supporting imaginary numbers and more specifically, negative square roots.

How do I use imaginary numbers in Javascript without using math.js?

function quadraticCalc () {
var a = prompt ("What is the a variable?") ; 
var b = prompt ("What is the b variable?") ; 
var c = prompt ("What is the c variable?") ;
var aC = 4 * a * c ;
var bExp = Math.pow(b, 2);
var multA = 2 * a
var insideRoot = bExp - aC
var root = Math.sqrt(insideRoot);}
  • 1
    Web project using js in a View? JS based project in Node.js? What's your application structure? – Daniel Hoffmann-Mitscherling Aug 02 '15 at 05:25
  • 1
    You haven't actually asked a question. – zzzzBov Aug 02 '15 at 05:27
  • Here's how to add math.js to your project http://mathjs.org/docs/getting_started.html – Matthew Lock Aug 02 '15 at 05:34
  • Here's a math.js tutorial on complex numbers http://mathjs.org/docs/datatypes/complex_numbers.html – Matthew Lock Aug 02 '15 at 05:35
  • I changed the post to clarify my question.As for the links posted in the comments, I have been to them before posting here, but I could not get math.js to work properly. Just knowing how to take the square root of negative numbers in Javascript should suffice for now. – Ernest Webb Aug 02 '15 at 06:12
  • Since Javascript does not have native complex numbers, if you want to use imaginary numbers without a library you will need to implement them from scratch. I would recommend trying everything you can to get math.js or a similar library to work before resorting to this. – Jim Garrison Aug 02 '15 at 06:17
  • Does anybody know of a programming language that _does_ have native support for complex numbers? I don't mean an environment, I mean a language itself. Just curious. – Stephen P Aug 02 '15 at 06:40
  • @JimGarrison Do you know of a tutorial or information on how to add a javascript library like math.js into my script? I've been to the site and done everything it says to do, but it doesn't seem to be working and frankly, its driving me crazy! – Ernest Webb Aug 03 '15 at 04:26
  • @StephenP Python, Octave/Matlab, Mathematica, Julia, Fortran, C++, C (as of C99), and [many others](https://en.wikipedia.org/wiki/Complex_data_type#Language_support) – Jim Garrison Aug 03 '15 at 10:14
  • @ErnestWebb Are you trying to get math.js to work on a web page in a web browser, or in a different environment? (see the first question here, by Daniel). If so, add the following line to your ``: `https://cdnjs.cloudflare.com/ajax/libs/mathjs/2.0.1/math.min.js`. – Jim Garrison Aug 03 '15 at 10:16
  • Sorry, I meant `` – Jim Garrison Aug 03 '15 at 11:04

1 Answers1

3

How do I use imaginary numbers in Javascript without using math.js?

You can't, at least not directly. Javascript numbers can only represent real values, positive and negative infinity, and NaN (Not a Number). They cannot store complex numbers.

If you need to work with complex numbers, you will need to represent them as pairs of real and imaginary coordinates, or in some other equivalent format, and manipulate them appropriately.