|
@@ -40,6 +40,7 @@ Rational::Rational(float x)
|
|
|
|
|
|
num = sign * (int64_t)(significand);
|
|
|
denum = smth;
|
|
|
+ cout << "float constructor work" << endl;
|
|
|
simple();
|
|
|
}
|
|
|
|
|
@@ -66,21 +67,22 @@ Rational::Rational(double x)
|
|
|
|
|
|
num = (long long int)(sign * significant);
|
|
|
denum = (long long int)pow(2, -exp);
|
|
|
+ cout << "double constructor work" << endl;
|
|
|
simple();
|
|
|
}
|
|
|
|
|
|
Rational& Rational::operator += (const Rational& r)
|
|
|
{
|
|
|
Rational newrr = r;
|
|
|
- while (INT64_MAX - num < r.num or INT64_MAX - denum < r.denum) {
|
|
|
- if(num > 10000 or denum > 10000) {
|
|
|
+ while (INT64_MAX - abs(num) < abs(r.num) or INT64_MAX - abs(denum) < abs(r.denum)) {
|
|
|
+ if(abs(num) > 10000 or abs(denum) > 10000) {
|
|
|
Fraction f(*this);
|
|
|
f.layers.pop();
|
|
|
Rational newr(f);
|
|
|
num = newr.num;
|
|
|
denum = newr.denum;
|
|
|
}
|
|
|
- if(r.num > 10000 or r.denum > 10000) {
|
|
|
+ if(abs(r.num > 10000) or abs(r.denum > 10000)) {
|
|
|
Fraction f(r);
|
|
|
f.layers.pop();
|
|
|
newrr = Rational(f);
|
|
@@ -114,16 +116,20 @@ Rational Rational :: operator - (const Rational& r) const
|
|
|
|
|
|
Rational& Rational::operator *= (const Rational& r)
|
|
|
{
|
|
|
+ cout << "suka *=" << endl;
|
|
|
Rational newrr = r;
|
|
|
- while (INT64_MAX / num < r.num or INT64_MAX / denum < r.denum) {
|
|
|
- if(num > 10000 or denum > 10000) {
|
|
|
+ while (INT64_MAX / abs(num) < abs(r.num) or INT64_MAX / abs(denum) < abs(r.denum)) {
|
|
|
+ if(abs(num) > 10000 or abs(denum) > 10000) {
|
|
|
Fraction f(*this);
|
|
|
+ cout << "pop ne ok" << endl;
|
|
|
+ f.show();
|
|
|
f.layers.pop();
|
|
|
- Rational newr(f);
|
|
|
+ cout << "pop ok" << endl;
|
|
|
+ Rational newr = Rational(f);
|
|
|
num = newr.num;
|
|
|
denum = newr.denum;
|
|
|
}
|
|
|
- if(r.num > 10000 or r.denum > 10000) {
|
|
|
+ if(abs(r.num) > 10000 or abs(r.denum) > 10000) {
|
|
|
Fraction f(r);
|
|
|
f.layers.pop();
|
|
|
newrr = Rational(f);
|
|
@@ -137,6 +143,7 @@ Rational& Rational::operator *= (const Rational& r)
|
|
|
|
|
|
Rational Rational :: operator * (const Rational& r) const
|
|
|
{
|
|
|
+ cout << "suka *" << endl;
|
|
|
Rational res(*this);
|
|
|
return res *= r;
|
|
|
}
|
|
@@ -160,6 +167,46 @@ Rational Rational :: operator -() const
|
|
|
return Rational(-num, denum);
|
|
|
}
|
|
|
|
|
|
+Rational& Rational :: operator += (const int r)
|
|
|
+{
|
|
|
+ return (*this)+=Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
+Rational Rational :: operator + (const int r) const
|
|
|
+{
|
|
|
+ return (*this)+Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
+Rational& Rational :: operator -= (const int r)
|
|
|
+{
|
|
|
+ return (*this)-=Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
+Rational Rational :: operator - (const int r) const
|
|
|
+{
|
|
|
+ return (*this)-Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
+Rational& Rational :: operator *= (const int r)
|
|
|
+{
|
|
|
+ return (*this)*=Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
+Rational Rational :: operator * (const int r) const
|
|
|
+{
|
|
|
+ return (*this)*Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
+Rational& Rational :: operator /= (const int r)
|
|
|
+{
|
|
|
+ return (*this)/=Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
+Rational Rational :: operator / (const int r) const
|
|
|
+{
|
|
|
+ return (*this)/Rational(r);
|
|
|
+}
|
|
|
+
|
|
|
ostream& operator <<(ostream& out, const Rational& r)
|
|
|
{
|
|
|
return out << '(' << r.num << ")/(" << r.denum << ')';
|
|
@@ -193,28 +240,43 @@ long long int Rational::lcm(long long int num1, long long int num2)
|
|
|
|
|
|
long long int Rational::gcd(long long int num1, long long int num2)
|
|
|
{
|
|
|
+ int s = 0;
|
|
|
while (num1 && num2)
|
|
|
- if (num1 >= num2)
|
|
|
+ {
|
|
|
+ cout << num1 << ' ' << num2 << endl;
|
|
|
+ if (abs(num1) >= abs(num2))
|
|
|
+ {
|
|
|
+ cout << "num1 >= num2" << endl;
|
|
|
num1 %= num2;
|
|
|
+ }
|
|
|
else
|
|
|
+ {
|
|
|
+ cout << "else" << endl;
|
|
|
num2 %= num1;
|
|
|
- return num1 | num2;
|
|
|
+ }
|
|
|
+ s++;
|
|
|
+ }
|
|
|
+ return abs(num1 | num2);
|
|
|
}
|
|
|
|
|
|
void Rational::simple()
|
|
|
{
|
|
|
+ cout << "smth wrong in simple" << endl;
|
|
|
long long int gct = gcd(num, denum);
|
|
|
+ cout << "gcd: " << gct << endl;
|
|
|
num /= gct;
|
|
|
denum /= gct;
|
|
|
+ cout << "simple work norm" << endl;
|
|
|
}
|
|
|
|
|
|
Rational Rational::sqrt()
|
|
|
{
|
|
|
- cout << "Real sqrt" << endl;
|
|
|
+ cout << "sqrt work" << endl;
|
|
|
Rational a = *this;
|
|
|
Rational x = *this;
|
|
|
for (long long int i = 0; i < 10000; i++) {
|
|
|
- x = (x + (a / x)) / (Rational)2;
|
|
|
+ x = (x + (a / x)) / 2;
|
|
|
}
|
|
|
+ cout << "sqrt work normal" << endl;
|
|
|
return x;
|
|
|
}
|