Rational.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "Rational.h"
  2. #include "Fraction/Fraction.h"
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <bitset>
  6. #include <math.h>
  7. using namespace std;
  8. Rational::Rational(){
  9. num = 0;
  10. denum = 1;
  11. }
  12. Rational::Rational(int num1){
  13. num = num1;
  14. denum = 1;
  15. }
  16. Rational::Rational(int num1, int num2)
  17. {
  18. num = num1;
  19. denum = num2;
  20. simple();
  21. }
  22. Rational::Rational(float x)
  23. {
  24. const uint32_t bits = *(uint32_t*)(&x);
  25. const uint32_t signBit = bits >> 31;
  26. const uint32_t exponentBits = (bits >> 23) & 0xff;
  27. const uint32_t mantisBits = bits & 0x7fffff;
  28. uint32_t significand = (1u << 23u) | mantisBits;
  29. const int64_t sign = signBit ? -1 : 1;
  30. const int32_t exp = exponentBits - 127 - 23;
  31. static uint32_t smth = 1 << (uint32_t)(-exp);
  32. num = sign * (int64_t)(significand);
  33. denum = smth;
  34. cout << "float constructor work" << endl;
  35. simple();
  36. }
  37. Rational::Rational(double x)
  38. {
  39. const unsigned long long int bits = *(unsigned long long int*)(&x);
  40. const unsigned long long int signBit = (bits >> 63) & 1;
  41. const unsigned long long int exponentBits = (bits >> 52) & 0x7ff;
  42. const unsigned long long int mantisBits = bits & 0xFFFFFFFFFFFFF;
  43. unsigned long long int significant = (long long int)pow(2, 52) | mantisBits;
  44. if (exponentBits == 0 and mantisBits == 0)
  45. {
  46. num = 0;
  47. denum = 1;
  48. }
  49. else if(exponentBits == 2046)
  50. {
  51. throw "Rational doesn't support NaN or Inf";
  52. }
  53. const long long int sign = signBit ? -1 : 1;
  54. const long long int exp = exponentBits - 1023 - 52;
  55. num = (long long int)(sign * significant);
  56. denum = (long long int)pow(2, -exp);
  57. cout << "double constructor work" << endl;
  58. simple();
  59. }
  60. Rational& Rational::operator += (const Rational& r)
  61. {
  62. Rational newrr = r;
  63. while (INT64_MAX - abs(num) < abs(r.num) or INT64_MAX - abs(denum) < abs(r.denum)) {
  64. if(abs(num) > 10000 or abs(denum) > 10000) {
  65. Fraction f(*this);
  66. f.layers.pop();
  67. Rational newr(f);
  68. num = newr.num;
  69. denum = newr.denum;
  70. }
  71. if(abs(r.num > 10000) or abs(r.denum > 10000)) {
  72. Fraction f(r);
  73. f.layers.pop();
  74. newrr = Rational(f);
  75. }
  76. }
  77. int scm = lcm(denum, newrr.denum);
  78. num = (num * (scm/denum) + newrr.num * (scm/newrr.denum));
  79. denum = scm;
  80. return *this;
  81. }
  82. Rational Rational :: operator + (const Rational& r) const
  83. {
  84. Rational res(*this);
  85. return res += r;
  86. }
  87. Rational& Rational::operator -= (const Rational& r)
  88. {
  89. int scm = lcm(denum, r.denum);
  90. num = (num * (scm/denum) - r.num * (scm/r.denum));
  91. denum = scm;
  92. return *this;
  93. }
  94. Rational Rational :: operator - (const Rational& r) const
  95. {
  96. Rational res(*this);
  97. return res -= r;
  98. }
  99. Rational& Rational::operator *= (const Rational& r)
  100. {
  101. cout << "suka *=" << endl;
  102. Rational newrr = r;
  103. while (INT64_MAX / abs(num) < abs(r.num) or INT64_MAX / abs(denum) < abs(r.denum)) {
  104. if(abs(num) > 10000 or abs(denum) > 10000) {
  105. Fraction f(*this);
  106. cout << "pop ne ok" << endl;
  107. f.show();
  108. f.layers.pop();
  109. cout << "pop ok" << endl;
  110. Rational newr = Rational(f);
  111. num = newr.num;
  112. denum = newr.denum;
  113. }
  114. if(abs(r.num) > 10000 or abs(r.denum) > 10000) {
  115. Fraction f(r);
  116. f.layers.pop();
  117. newrr = Rational(f);
  118. }
  119. }
  120. num *= newrr.num;
  121. denum *= newrr.denum;
  122. simple();
  123. return *this;
  124. }
  125. Rational Rational :: operator * (const Rational& r) const
  126. {
  127. cout << "suka *" << endl;
  128. Rational res(*this);
  129. return res *= r;
  130. }
  131. Rational& Rational::operator /= (const Rational& r)
  132. {
  133. num *= r.denum;
  134. denum *= r.num;
  135. simple();
  136. return *this;
  137. }
  138. Rational Rational :: operator / (const Rational& r) const
  139. {
  140. Rational res(*this);
  141. return res /= r;
  142. }
  143. Rational Rational :: operator -() const
  144. {
  145. return Rational(-num, denum);
  146. }
  147. Rational& Rational :: operator += (const int r)
  148. {
  149. return (*this)+=Rational(r);
  150. }
  151. Rational Rational :: operator + (const int r) const
  152. {
  153. return (*this)+Rational(r);
  154. }
  155. Rational& Rational :: operator -= (const int r)
  156. {
  157. return (*this)-=Rational(r);
  158. }
  159. Rational Rational :: operator - (const int r) const
  160. {
  161. return (*this)-Rational(r);
  162. }
  163. Rational& Rational :: operator *= (const int r)
  164. {
  165. return (*this)*=Rational(r);
  166. }
  167. Rational Rational :: operator * (const int r) const
  168. {
  169. return (*this)*Rational(r);
  170. }
  171. Rational& Rational :: operator /= (const int r)
  172. {
  173. return (*this)/=Rational(r);
  174. }
  175. Rational Rational :: operator / (const int r) const
  176. {
  177. return (*this)/Rational(r);
  178. }
  179. ostream& operator <<(ostream& out, const Rational& r)
  180. {
  181. return out << '(' << r.num << ")/(" << r.denum << ')';
  182. }
  183. Rational::operator double() const
  184. {
  185. return ((double)num/(double)denum);
  186. }
  187. Rational::operator int() const
  188. {
  189. return int(double(*this));
  190. }
  191. long long int Rational::lcm(long long int num1, long long int num2)
  192. {
  193. int result = 1;
  194. for(int dnum = 2; num1 > 1 or num2 > 1; dnum++)
  195. {
  196. if(num1%dnum==0 or num2%dnum==0)
  197. {
  198. num1%dnum==0 ? num1/=dnum : num1*1;
  199. num2%dnum==0 ? num2/=dnum : num1*1;
  200. result *= dnum;
  201. dnum--;
  202. }
  203. }
  204. return result;
  205. }
  206. long long int Rational::gcd(long long int num1, long long int num2)
  207. {
  208. int s = 0;
  209. while (num1 && num2)
  210. {
  211. cout << num1 << ' ' << num2 << endl;
  212. if (abs(num1) >= abs(num2))
  213. {
  214. cout << "num1 >= num2" << endl;
  215. num1 %= num2;
  216. }
  217. else
  218. {
  219. cout << "else" << endl;
  220. num2 %= num1;
  221. }
  222. s++;
  223. }
  224. return abs(num1 | num2);
  225. }
  226. void Rational::simple()
  227. {
  228. cout << "smth wrong in simple" << endl;
  229. long long int gct = gcd(num, denum);
  230. cout << "gcd: " << gct << endl;
  231. num /= gct;
  232. denum /= gct;
  233. cout << "simple work norm" << endl;
  234. }
  235. Rational Rational::sqrt()
  236. {
  237. cout << "sqrt work" << endl;
  238. Rational a = *this;
  239. Rational x = *this;
  240. for (long long int i = 0; i < 10000; i++) {
  241. x = (x + (a / x)) / 2;
  242. }
  243. cout << "sqrt work normal" << endl;
  244. return x;
  245. }