Rational.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. simple();
  35. }
  36. Rational::Rational(double x)
  37. {
  38. const unsigned long long int bits = *(unsigned long long int*)(&x);
  39. const unsigned long long int signBit = (bits >> 63) & 1;
  40. const unsigned long long int exponentBits = (bits >> 52) & 0x7ff;
  41. const unsigned long long int mantisBits = bits & 0xFFFFFFFFFFFFF;
  42. unsigned long long int significant = (long long int)pow(2, 52) | mantisBits;
  43. if (exponentBits == 0 and mantisBits == 0)
  44. {
  45. num = 0;
  46. denum = 1;
  47. }
  48. else if(exponentBits == 2046)
  49. {
  50. throw "Rational doesn't support NaN or Inf";
  51. }
  52. const long long int sign = signBit ? -1 : 1;
  53. const long long int exp = exponentBits - 1023 - 52;
  54. num = (long long int)(sign * significant);
  55. denum = (long long int)pow(2, -exp);
  56. simple();
  57. }
  58. Rational& Rational::operator += (const Rational& r)
  59. {
  60. Rational newrr = r;
  61. while (INT64_MAX - num < r.num or INT64_MAX - denum < r.denum) {
  62. if(num > 10000 or denum > 10000) {
  63. Fraction f(*this);
  64. f.layers.pop();
  65. Rational newr(f);
  66. num = newr.num;
  67. denum = newr.denum;
  68. }
  69. if(r.num > 10000 or r.denum > 10000) {
  70. Fraction f(r);
  71. f.layers.pop();
  72. newrr = Rational(f);
  73. }
  74. }
  75. int scm = lcm(denum, newrr.denum);
  76. num = (num * (scm/denum) + newrr.num * (scm/newrr.denum));
  77. denum = scm;
  78. return *this;
  79. }
  80. Rational Rational :: operator + (const Rational& r) const
  81. {
  82. Rational res(*this);
  83. return res += r;
  84. }
  85. Rational& Rational::operator -= (const Rational& r)
  86. {
  87. int scm = lcm(denum, r.denum);
  88. num = (num * (scm/denum) - r.num * (scm/r.denum));
  89. denum = scm;
  90. return *this;
  91. }
  92. Rational Rational :: operator - (const Rational& r) const
  93. {
  94. Rational res(*this);
  95. return res -= r;
  96. }
  97. Rational& Rational::operator *= (const Rational& r)
  98. {
  99. Rational newrr = r;
  100. while (INT64_MAX / num < r.num or INT64_MAX / denum < r.denum) {
  101. if(num > 10000 or denum > 10000) {
  102. Fraction f(*this);
  103. f.layers.pop();
  104. Rational newr(f);
  105. num = newr.num;
  106. denum = newr.denum;
  107. }
  108. if(r.num > 10000 or r.denum > 10000) {
  109. Fraction f(r);
  110. f.layers.pop();
  111. newrr = Rational(f);
  112. }
  113. }
  114. num *= newrr.num;
  115. denum *= newrr.denum;
  116. simple();
  117. return *this;
  118. }
  119. Rational Rational :: operator * (const Rational& r) const
  120. {
  121. Rational res(*this);
  122. return res *= r;
  123. }
  124. Rational& Rational::operator /= (const Rational& r)
  125. {
  126. num *= r.denum;
  127. denum *= r.num;
  128. simple();
  129. return *this;
  130. }
  131. Rational Rational :: operator / (const Rational& r) const
  132. {
  133. Rational res(*this);
  134. return res /= r;
  135. }
  136. Rational Rational :: operator -() const
  137. {
  138. return Rational(-num, denum);
  139. }
  140. ostream& operator <<(ostream& out, const Rational& r)
  141. {
  142. return out << '(' << r.num << ")/(" << r.denum << ')';
  143. }
  144. Rational::operator double() const
  145. {
  146. return ((double)num/(double)denum);
  147. }
  148. Rational::operator int() const
  149. {
  150. return int(double(*this));
  151. }
  152. long long int Rational::lcm(long long int num1, long long int num2)
  153. {
  154. int result = 1;
  155. for(int dnum = 2; num1 > 1 or num2 > 1; dnum++)
  156. {
  157. if(num1%dnum==0 or num2%dnum==0)
  158. {
  159. num1%dnum==0 ? num1/=dnum : num1*1;
  160. num2%dnum==0 ? num2/=dnum : num1*1;
  161. result *= dnum;
  162. dnum--;
  163. }
  164. }
  165. return result;
  166. }
  167. long long int Rational::gcd(long long int num1, long long int num2)
  168. {
  169. while (num1 && num2)
  170. if (num1 >= num2)
  171. num1 %= num2;
  172. else
  173. num2 %= num1;
  174. return num1 | num2;
  175. }
  176. void Rational::simple()
  177. {
  178. long long int gct = gcd(num, denum);
  179. num /= gct;
  180. denum /= gct;
  181. }
  182. Rational Rational::sqrt()
  183. {
  184. cout << "Real sqrt" << endl;
  185. Rational a = *this;
  186. Rational x = *this;
  187. for (long long int i = 0; i < 10000; i++) {
  188. x = (x + (a / x)) / (Rational)2;
  189. }
  190. return x;
  191. }