# How to calculate with imaginary numbers in JavaScript?

Active3 hr before
Viewed126 times

numberscalculate
90%

Complex numbers in JavaScript,Assuming you really want complex numbers, and not just the imaginary component:, About , Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

You can just do things like:

```math.i; // i
math.sqrt(-4) // 2i
var a = math.complex('2 + 3i'); // 2 + 3i
var b = math.complex(4, 5); // 4 + 5i
math.add(a, b); // 6 + 8i
math.multiply(a, b); // -7 + 22i
math.eval('e^(pi*i) + 1'); // ~0
// etc...```
88%

Returns a JSON representation of the complex number, with signature {mathjs: 'Complex', re: number, im: number}. Used when serializing a complex number, see Serialization.,Returns a complex number that is inverse of the current complex number.,Revive a complex number from a JSON object. Accepts An object {mathjs: 'Complex', re: number, im: number}, where the property mathjs is optional. Used when deserializing a complex number, see Serialization.,Get the vector representation of the current complex number. Returns an array of size 2.

```math.complex(re: number): Complex
math.complex(re: number, im: number): Complex
math.complex(complex: Complex): Complex
math.complex({
re: Number,
im: Number
}): Complex
math.complex({
r: number,
phi: number
}): Complex
math.complex({
abs: number,
arg: number
}): Complex
math.complex(str: string): Complex```
72%

Previous: Write a JavaScript function to calculate Lanczos approximation gamma. Next: Write a JavaScript program to subtract two complex numbers.,Write a JavaScript program to add two complex numbers. A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, that satisfies the equation i2 = −1. In this expression, a is the real part and b is the imaginary part of the complex number.,Vue - JavaScript Framework,Angular - JavaScript Framework

HTML Code:

```<!DOCTYPE html>
<html>

<meta charset="utf-8">
<title>JavaScript function to add two complex numbers</title>

<body>

</body>

</html>```

JavaScript Code:

```function Complex(real, imaginary) {
this.real = 0;
this.imaginary = 0;
this.real = (typeof real === 'undefined') ? this.real : parseFloat(real);
this.imaginary = (typeof imaginary === 'undefined') ? this.imaginary : parseFloat(imaginary);
}
Complex.transform = function(num) {
var complex;
complex = (num instanceof Complex) ? num : complex;
complex = (typeof num === 'number') ? new Complex(num, 0) : num;
return complex;
};

function display_complex(re, im) {
if (im === '0') return '' + re;
if (re === 0) return '' + im + 'i';
if (im < 0) return '' + re + im + 'i';
return '' + re + '+' + im + 'i';
}

var num1, num2;
num1 = Complex.transform(first);
num2 = Complex.transform(second);
var real = num1.real + num2.real;
var imaginary = num1.imaginary + num2.imaginary;
return display_complex(real, imaginary);
}
var a = new Complex(2, -7);
var b = new Complex(4, 3);

Sample Output:

`6 - 4 i`
65%

```function Complex(real, imaginary) {
this.real = 0;
this.imaginary = 0;
this.real = (typeof real === 'undefined') ? this.real : parseFloat(real);
this.imaginary = (typeof imaginary === 'undefined') ? this.imaginary : parseFloat(imaginary);
}
Complex.transform = function(num) {
var complex;
complex = (num instanceof Complex) ? num : complex;
complex = (typeof num === 'number') ? new Complex(num, 0) : num;
return complex;
};

function display_complex(re, im) {
if (im === '0') return '' + re;
if (re === 0) return '' + im + 'i';
if (im < 0) return '' + re + im + 'i';
return '' + re + '+' + im + 'i';
}

var num1, num2;
num1 = Complex.transform(first);
num2 = Complex.transform(second);
var real = num1.real + num2.real;
var imaginary = num1.imaginary + num2.imaginary;
return display_complex(real, imaginary);
}
var a = new Complex(2, -7);
var b = new Complex(4, 3);
75%

Complex.js is a well tested JavaScript library to work with complex number arithmetic in JavaScript. It implements every elementary complex number manipulation function and the API is intentionally similar to Fraction.js. Furthermore, it's the basis of Polynomial.js and Math.js., A complex numbers library , A complex numbers library ,A complex euler number instance

```let Complex = require('complex.js');

let c = new Complex("99.3+8i");
c.mul({
re: 3,
im: 9
}).div(4.9).sub(3, 2);```
40%

0
22%

Complex numbers have a wide vareity of applications, like calculation of AC voltage, representation of rotations in 2D, Discrete Fourier Transform, complex geometry and even Riemann hypothesis involve complex numbers. That's why I have written complex.js to make work with complex numbers an easy task in JavaScript.,The library is well tested and contains a lot of functions, which try to overcome numerical imprecision and are implemented with performance in mind. To make the API more robust, every operation returns a new instance of the complex class. New JavaScript engines do a really good job on this and immutable functions are really fast nowadays.,Complex numbers sound complex, but in fact they are the most natural numbers - even if the name is a bit misleading. When extending the natural numbers to integers, to rational numbers, to real numbers and finally to complex numbers; it's the first field in which every operation is well defined.,Sorry, comments are closed for this article. Contact me if you want to leave a note.

## Simple Complex Numbers Calculation

```var Complex = require('complex.js');

var c = new Complex("99.3+8i");
c.mul({
re: 3,
im: 9
}).div(4.9).sub(3, 2);```
{"ft_lang_label":"__label__en","ft_lang_prob":0.6974125,"math_prob":0.99496883,"size":8363,"snap":"2021-43-2021-49","text_gpt3_token_len":1997,"char_repetition_ratio":0.18315588,"word_repetition_ratio":0.23255815,"special_character_ratio":0.26330265,"punctuation_ratio":0.18777293,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9986147,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-02T00:37:34Z\",\"WARC-Record-ID\":\"<urn:uuid:b9f824c7-11a9-446d-8f0a-46b5d5c21332>\",\"Content-Length\":\"100354\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:69374a29-cf30-4d65-b92f-319defd4fdc3>\",\"WARC-Concurrent-To\":\"<urn:uuid:9bfbfbbb-84d7-40e1-a3b6-3b6140e04020>\",\"WARC-IP-Address\":\"135.125.180.139\",\"WARC-Target-URI\":\"https://pretagteam.com/question/how-to-calculate-with-imaginary-numbers-in-javascript\",\"WARC-Payload-Digest\":\"sha1:VWKH4JHPP536WI7M7MVPMI62OEEY5XED\",\"WARC-Block-Digest\":\"sha1:YKSYLZLKW7P4GIBO6HFMJGRTTCY6XG2T\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964361064.58_warc_CC-MAIN-20211201234046-20211202024046-00194.warc.gz\"}"}