|
@@ -39,6 +39,31 @@ Rational::Rational(float x)
|
|
simple();
|
|
simple();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+Rational::Rational(double x)
|
|
|
|
+{
|
|
|
|
+ const unsigned long long int bits = *(unsigned long long int*)(&x);
|
|
|
|
+ const unsigned long long int signBit = (bits >> 63) & 1;
|
|
|
|
+ const unsigned long long int exponentBits = (bits >> 52) & 0x7ff;
|
|
|
|
+ const unsigned long long int mantisBits = bits & 0xFFFFFFFFFFFFF;
|
|
|
|
+ unsigned long long int significant = (long long int)pow(2, 52) | mantisBits;
|
|
|
|
+
|
|
|
|
+ const long long int sign = signBit ? -1 : 1;
|
|
|
|
+
|
|
|
|
+ const long long int exp = exponentBits - 1023 - 52;
|
|
|
|
+
|
|
|
|
+ cout << bitset<64>(bits) << endl;
|
|
|
|
+ cout << bitset<64>(signBit) << endl;
|
|
|
|
+ cout << bitset<64>(exponentBits) << endl;
|
|
|
|
+ cout << bitset<64>(mantisBits) << endl;
|
|
|
|
+ cout << bitset<64>(pow(2,52)) << endl;
|
|
|
|
+ cout << bitset<64>(significant) << endl;
|
|
|
|
+ cout << bitset<64>(exp) << endl;
|
|
|
|
+
|
|
|
|
+ cout << setprecision(15) << fixed << sign * significant << '/' << (unsigned long long int)pow(2, -exp) << endl;
|
|
|
|
+ cout << (sign * significant) / pow(2, -exp) << endl;
|
|
|
|
+ cout << sign * pow(2, -1022) * significant << endl;
|
|
|
|
+}
|
|
|
|
+
|
|
Rational& Rational::operator += (const Rational& r)
|
|
Rational& Rational::operator += (const Rational& r)
|
|
{
|
|
{
|
|
int scm = lcm(denum, r.denum);
|
|
int scm = lcm(denum, r.denum);
|