test-complex-lib.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <complex>
  2. #include <string>
  3. #include <stdio.h>
  4. #include "../../ucomplex.h"
  5. void output(std::string operation, complex c1, std::complex<double> c2) {
  6. double diff_r = c1.r - c2.real();
  7. double diff_i = c1.i - c2.imag();
  8. printf("%s: \tdiff=(%g,%g), \tc1 = (%g,%g), \tc2 = (%g,%g)\n",
  9. operation.c_str(),
  10. diff_r, diff_i,
  11. c1.r, c1.i,
  12. c2.real(), c2.imag());
  13. // Epsilon for double is 1e-16, check it 1.0 + 1e-16 = 1.0
  14. if (diff_r > 1e-16||diff_i > 1e-16) printf("WARNING!! Non-zero diff!!!\n");
  15. }
  16. /********************************************************************/
  17. /********************************************************************/
  18. /********************************************************************/
  19. void output_double(std::string operation, double c1, double c2) {
  20. double diff = c1 - c2;
  21. printf("%s: \tdiff=(%g), \tc1 = (%g), \tc2 = (%g)\n",
  22. operation.c_str(),
  23. diff, c1, c2);
  24. // Epsilon for double is 1e-16, check it 1.0 + 1e-16 = 1.0
  25. if (diff > 1e-16) printf("WARNING!! Non-zero diff!!!\n");
  26. }
  27. /********************************************************************/
  28. /********************************************************************/
  29. /********************************************************************/
  30. int main() {
  31. double r = 1.412039487560983471903;
  32. complex a1 = {1.0012001301318988478, -1.0123131231241412987237},
  33. b1 = {0.42312341412412398237, 1.32312314124141232987987}, c1;
  34. std::complex<double> a2(a1.r, a1.i), b2(b1.r, b1.i), c2;
  35. printf("Re(a1) = %g, Im(a1) = %g\n", a1.r, a1.i);
  36. printf("Re(a2) = %g, Im(a2) = %g\n", a2.real(), a2.imag());
  37. printf("Re(b1) = %g, Im(b1) = %g\n", b1.r, b1.i);
  38. printf("Re(b2) = %g, Im(b2) = %g\n", b2.real(), b2.imag());
  39. printf("r = %g\n", r);
  40. c1 = Cadd(a1, b1);
  41. c2 = a2 + b2;
  42. output("Add", c1, c2);
  43. c1 = Cadd(b1, a1);
  44. c2 = b2 + a2;
  45. output("sAdd", c1, c2);
  46. c1 = Csub(a1, b1);
  47. c2 = a2 - b2;
  48. output("Sub", c1, c2);
  49. c1 = Csub(b1, a1);
  50. c2 = b2 - a2;
  51. output("sSub", c1, c2);
  52. c1 = Cmul(a1, b1);
  53. c2 = a2 * b2;
  54. output("Mul", c1, c2);
  55. c1 = Cmul(b1, a1);
  56. c2 = b2 * a2;
  57. output("sMul", c1, c2);
  58. c1 = RCmul(r, b1);
  59. c2 = r * b2;
  60. output("RCMul", c1, c2);
  61. c1 = Cdiv(a1, b1);
  62. c2 = a2 / b2;
  63. output("Div", c1, c2);
  64. c1 = Cdiv(b1, a1);
  65. c2 = b2 / a2;
  66. output("sDiv", c1, c2);
  67. c1 = Conjg(a1);
  68. c2 = conj(a2);
  69. output("Conj", c1, c2);
  70. c1 = Conjg(b1);
  71. c2 = conj(b2);
  72. output("sConj", c1, c2);
  73. c1 = Cinv(a1);
  74. c2 = 1.0/a2;
  75. output("Inv", c1, c2);
  76. c1 = Cinv(b1);
  77. c2 = 1.0/b2;
  78. output("sInv", c1, c2);
  79. output_double("Abs", Cabs(a1), std::abs(a2));
  80. output_double("sAbs", Cabs(b1), std::abs(b2));
  81. output_double("Arg", Carc(a1), std::arg(a2));
  82. output_double("sArg", Carc(b1), std::arg(b2));
  83. c1 = Cinv(a1);
  84. c2 = 1.0/a2;
  85. output("Inv", c1, c2);
  86. return 0;
  87. }