string - Displaying complex numbers in java -


my goal today able add, subtract, multiply , divide complex numbers (a+bi) , show data somehow. knew difficult arise when tried create method divides complex numbers. should display approximately 0.39189-.1486i if math correct or ((29-11i)/74). output is: -0.041666666666666664+0.4583333333333333i , incorrect.

can me find error in complexdiv method?

here code:

public string complexdiv(complexnumbers other) {          string r = double.tostring(((real*other.real)-(complex*other.complex))                 / ((other.real * other.real)-(other.complex * other.complex)));          string c = double.tostring((-(real*other.complex)+(other.real*complex))                  / ((other.real * other.real)-(other.complex * other.complex)));          return r + "+" + c + "i";     } 

here testing class call method:

public class complextest {     public static void main(string[] args) {         complexnumbers c1 = new complexnumbers();         complexnumbers c2 = new complexnumbers();          c1.real = 3;         c1.complex = 2;         c2.real = 5;         c2.complex = 7;          system.out.println(c1.complexadd(c2));         system.out.println(c1.complexsub(c2));         system.out.println(c1.complexmult(c2));         system.out.println(c1.complexdiv(c2));         system.out.println(c1.findconjugate());     } } 

bonus question: idea how represent answers in fraction form instead of decimal?

here entire complexnumbers class give overview of approach:

package exerciseslearningjava;  public class complexnumbers {      public double real;     public double complex;     public double realconvert;     public double compconvert;       public string complexadd(complexnumbers other) {          string r = double.tostring(real + other.real);         string c = double.tostring(complex + other.complex);          return r + "+" + c + "i";     }     public string complexsub(complexnumbers other) {          string r = double.tostring(real - other.real);         string c = double.tostring(complex - other.complex);          return r + c + "i";     }     public string complexmult(complexnumbers other) {          string r = double.tostring((real * other.real) - (complex*other.complex));         string c = double.tostring((real * other.complex)+(complex*other.real));          return r + "+" + c + "i";      }     public string complexdiv(complexnumbers other) {          string r = double.tostring(((real*other.real)-(complex*other.complex))                 / ((other.real * other.real)-(other.complex * other.complex)));          string c = double.tostring((-(real*other.complex)+(other.real*complex))                  / ((other.real * other.real)-(other.complex * other.complex)));          return r + "+" + c + "i";     }     public string findconjugate() {         string r = double.tostring(real);         string c = double.tostring(complex);          return r + "-" + c + "i";     } } 

your denominator:

((other.real * other.real) - (other.complex * other.complex)) 

shouldn't be:

((other.real * other.real) + (other.complex * other.complex)) 

since multiplying complex number conjugate:

(a + bi) * (a - bi) = (a * a) - (b * b * * i) 

but since i * i -1, becomes:

(a + bi) * (a - bi) = (a * a) + (b * b) 

also, aside, bad feelings in of neck when see strings being used represent numeric values. why use strings? why not float or bigdecimal?


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -