Advertisement
Guest User

Untitled

a guest
May 24th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #define SC_INCLUDE_FX
  2. #include <iostream>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <vector>
  6. #include <systemc>
  7.  
  8. typedef std::vector<double> array;
  9. typedef sc_dt::sc_fix_fast fixx;
  10.  
  11. double fact(double num)
  12. {
  13. double temp = 1;
  14.  
  15. for(int i = 1; i <= num; ++i)
  16. temp *= i;
  17.  
  18. return temp;
  19. }
  20. void no_of_members(const array &true_values,const array &arguments, array &res_tay_series, int &no_members)
  21. {
  22. int i, j, counter;
  23.  
  24. do
  25. {
  26. counter = 0;
  27.  
  28. for(i = 0; i < 5; ++i)
  29. res_tay_series[i] = 0;
  30.  
  31.  
  32. for(i = 0; i <= no_members; ++i)
  33. for(j = 0; j < 5; ++j)
  34. res_tay_series[j] += (pow(-1, i)*pow(arguments[j], 2*i + 1))/(fact(2*i + 1));
  35.  
  36. for(j = 0; j < 5; ++j)
  37. if(std::abs(res_tay_series[j] - true_values[j]) < 1e-5)
  38. {
  39. ++counter;
  40. }
  41.  
  42. ++no_members;
  43. } while(counter != 5);
  44. }
  45.  
  46. int no_bits_fixed_point(const array &true_values, int &no_bits_W, const int &no_bits_I)
  47. {
  48. int i;
  49. int no_bit[5];
  50.  
  51. for(i = 0; i < 5; ++i)
  52. {
  53. no_bits_W = 2;
  54. while(1)
  55. {
  56. fixx fix_point(no_bits_W, no_bits_I);
  57.  
  58. fix_point = true_values[i];
  59.  
  60. if(std::abs(fix_point - true_values[i]) < 1e-5)
  61. {
  62. no_bit[i] = no_bits_W;
  63. break;
  64. }
  65. ++no_bits_W;
  66. }
  67. }
  68.  
  69. int max = no_bit[0];
  70.  
  71. for(i = 1; i < 5; ++i)
  72. {
  73. if(no_bit[i] > max)
  74. {
  75. max = no_bit[i];
  76. }
  77. }
  78.  
  79. return max;
  80.  
  81. }
  82.  
  83. int sc_main(int argc, char* arvg[])
  84. {
  85. array true_values = {sin(M_PI/6), sin(M_PI/4), sin(M_PI/3), sin(M_PI/2), sin(M_PI)};
  86. array arguments = {M_PI/6, M_PI/4, M_PI/3, M_PI/2, M_PI};
  87. array res_tay_series = {0, 0, 0, 0, 0};
  88.  
  89. int no_members = 0;
  90.  
  91. int no_bits_W = 2; //no bits
  92. int no_bits_I = 2; //position of point
  93.  
  94. no_of_members(true_values, arguments, res_tay_series, no_members);
  95.  
  96. int no_bitt = no_bits_fixed_point(true_values, no_bits_W, no_bits_I);
  97.  
  98. std::cout<<"No members: "<< no_members<<std::endl;
  99. std::cout<<"No bits: "<< no_bitt<<std::endl;
  100. std::cout<<"No bits before point: "<< no_bits_I<<std::endl;
  101. std::cout<<"No bits after point: "<< no_bitt - no_bits_I <<std::endl;
  102.  
  103. return 0;
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement