For any help regarding Electrical Engineering and Computer Science Assignment Help
Visit :- https://www.programminghomeworkhelp.com/ ,
Email :- support@programminghomeworkhelp.com or
call us at :- +1 678 648 4277
programminghomeworkhelp.com
Problem 3: Rational Number Library (rational)
Now that we know about the rounding issues of floating-point arithmetic, we’d like to
implement a library that will allow for exact computation with rational numbers. This
library should be easy and intuitive to use. You want to be able to write code as
straight-forward as you can see in this sample:
auto a = Rational{ 1, 3 }; // the number 1/3
auto b = Rational{ -6, 7 }; // the number -6/7
std::cout «« "a * b = " «« a «« " * " «« b «« «« a * b «« ;
std::cout «« "a / ( a + b / a ) = " «« a / ( a + b / a ) «« ;
which produces the following as output.
a * b = 1I3 * -6/7 = -2/7
a / ( a + b / a ) = -7/47
Of course, we’ll be able to do much more impressive things. For example, here is a snippet
of code that computes the golden ratio ϕ to over 15 decimal digits of accuracy using a very
readable implementation of its continued fraction expansion.
programminghomeworkhelp.com
Problems
const int ITER = 40;
auto phi = Rational{ 1 }; // set equal to 1 to start off with
for( int i = 0; i « ITER; ++i ) { phi = 1 / ( 1 + phi ); }
std::cout «« std::setprecision( 15 ); // look up «iomanip>
std::cout «« "phi = " «« ( 1 + phi ).to double() «« "n";
This prints out phi = 1.61803398874989, which happens to be 15 completely accurate
digits.
You can find the zipped project at provided in the file rational.zip as a basis for your
program. As in the other project problem, the folder contains an include I directory, our
standard project Make file, and a srcI directory. All of our header (.h) files will be found in
include I while all of our source (.cpp) files will be in srcI. The C++ files you should find are
rational. h, gcd. h, rational.cpp, gcd.cpp, and test.cpp; you will be modifying rational.h and
rational.cpp. Like in the previous project problem, you can modify the Make file or test.cpp
to run your own tests, but your changes there will be overwritten when you upload to the
grader.
We are provided a header file describing the interface for the Rational class.
programminghomeworkhelp.com
#ifndef _6S096 _RATIONAL _H
#define _6S096 _RATIONAL _H
#include «cstdint>
#include «iosfwd>
#include «stdexcept>
class Rational { intmax t num, den;
public:
enum sign type { POSITIVE, NEGATIVE };
Rational() : num{0}, den{1} {}
Rational( intmax t numer ) : num{numer}, den{1} {}
Rational( intmax t numer, intmax t denom ) : num{numer}, den{denom} {
normalize(); II reduces the Rational to lowest terms }
inline intmax t num() const { return num; }
inline intmax t den() const { return den; }
// you'll implement all these in rational.cpp
void normalize();
float to float() const;
double to double() const;
programminghomeworkhelp.com
sign type sign() const;
Rational inverse() const;
};
// An example multiplication operator
inline Rational operator*( const Rational &a, const Rational &b
) { return Rational{ a.num() * b.num(), a.den() * b.den() };
}
//Functions you'll be implementing:
std::ostream& operator««( std::ostream& os, const Rational
&ratio );
// ... and so on
Notice the include guards at the top, protecting us from including the file multiple times.
An explanation of some of the constructs we use is in order. You’ll notice we use the
intmax t type from the «cstdint> library. This type represents the maximum width integer
type for our particular architecture (basically, we want to use the largest integers
available). On my 64-bit computer, this means that we’lll
find size of(Rational) to be 16 since it comprises two 64-bit (8-byte) integers.
programminghomeworkhelp.com
Look up an enum. We use this to define a type sign type in the scope of our class.
Outside the class, we can refer to the type as Rational::sign type and its two values as
Rational::POSITIVE and Rational::NEGATIVE.
Contained in the header file is also a class that extends std::domain error to create a
special type of exception for our Rational class. Look up the meaning of explicit; why do
we use it in this particular constructor definition?
#include «stdexcept>
// ...near the bottom of the file
class bad rational : public std::domain error {
public:
explicit bad rational() : std::domain error( "Bad rational: zero denom." ) {}
};
Read through the code carefully and make sure you understand it. If you have any
questions about the provided code or don’t know why something is structured the
way it is, please ask about it on Piazza.
This is the list of functions you should fill out. In the header file, you have a number
of inline functions to complete, as shown here.
programminghomeworkhelp.com
inline Rational operator+( const Rational &a, const Rational &b ) {I* ... *I}
inline Rational operator-( const Rational &a, const Rational &b ) {I* ... *I}
inline Rational operator /( const Rational &a, const Rational &b ) {I* ... *I}
inline bool operator«( const Rational &lhs, const Rational &rhs ) {I* ... *I}
inline bool operator==( const Rational &lhs, const Rational &rhs ) {I* ... *I}
There are also a number of functions declared in the header file which you should write
an implementation for in the source file rational.cpp. Those declarations are:
class Rational {
// ...snipped
public:
// ...snipped
void normalize();
float to float() const;
double to double() const;
sign type sign() const;
};
std::ostream& operator««( std::ostream& os, const Rational &ratio );
programminghomeworkhelp.com
Look in the existing rational.cpp file for extensive descriptions of the required functionality.
In gcd.h, you can find the declaration for two functions which you may find useful: fast
computation of the greatest common divisor and least common multiple of two integers.
Feel free to include this header and use these functions in your code as needed.
#ifndef _6S096 _GCD _H
#define _6S096 _GCD _H
#include «cstdint>
intmax _t gcd( intmax t a, intmax _t b );
intmax _t lcm( intmax _t a, intmax _t b );
#endif // _6S096 _GCD _H
Input Format
Not applicable; your library will be compiled into a testing suite, your implemented
functions will be called by the program, and the behavior checked for correctness. For
example, here is a potential test:
programminghomeworkhelp.com
#include "rational.h"
#include «cassert>
void test addition() {
// Let's try adding 1/3 + 2/15 = 7/15.
auto sum = Rational{ 1, 3 } + Rational{ 2, 15 }
assert( sum.num() == 7 );
assert( sum.den() == 15 );
}
You are strongly encouraged to write your own tests in test.cpp so that you can try out
your implementation code before submitting it to the online grader.
Output Format
Not applicable.
programminghomeworkhelp.com
Here are the contents of rational.h:
#ifndef _6S096_RATIONAL_H
#define _6S096_RATIONAL_H
#include <cstdint>
#include <iosfwd>
#include <stdexcept>
Solutions
class Rational {
intmax_t _num, _den; public:
enum sign_type { POSITIVE, NEGATIVE };
Rational() : _num{0}, _den{1} {}
Rational( intmax_t numer ) : _num{numer}, _den{1} {}
Rational( intmax_t numer, intmax_t denom ) : _num{numer}, _den{denom} { normalize(); }
inline intmax_t num() const { return _num; }
inline intmax_t den() const { return _den; }
programminghomeworkhelp.com
void normalize();
float to_float()const;
double to_double()const;
sign_type sign() const;
Rational inverse() const;
};
std::ostream& operator<<( std::ostream& os, const Rational &ratio );
inline bool operator==( const Rational &lhs, const Rational &rhs ) {
return lhs.num() * rhs.den() == rhs.num() * lhs.den();
}
inline bool operator<( const Rational &lhs, const Rational &rhs ) {
if( lhs.sign() == Rational::POSITIVE && rhs.sign() == Rational::POSITIVE ) {
return lhs.num() * rhs.den() < rhs.num() * lhs.den();
} else if( lhs.sign() == Rational::NEGATIVE && rhs.sign() == Rational::NEGATIVE ) {
return lhs.num() * rhs.den() > rhs.num() * lhs.den(); } else {
return lhs.sign() == Rational::NEGATIVE;
}
}
programminghomeworkhelp.com
inline Rational operator*( const Rational &a, const Rational &b ) {
return Rational{ a.num() * b.num(), a.den() * b.den() };
}
inline Rational operator+( const Rational &a, const Rational &b ) {
return Rational{ a.num() * b.den() + b.num() * a.den(), a.den() * b.den() };
}
inline Rational operator-( const Rational &a, const Rational &b ) {
return Rational{ a.num() * b.den() - b.num() * a.den(), a.den() * b.den() };
}
inline Rational operator/( const Rational &a, const Rational &b ) {
return a * b.inverse();
}
class bad_rational : public std::domain_error {
public:
explicit bad_rational() : std::domain_error("Bad rational: zero denominator" ) {}
};
programminghomeworkhelp.com
#endif // _6S096_RATIONAL_H
Here is the source code file rational.cpp:
#include "rational.h"
#include "gcd.h"
#include <stdexcept>
#include <ostream>
#include <iostream>
#include <cmath>
Rational Rational::inverse() const {
return Rational{ _den, _num };
}
Rational::sign_type Rational::sign()const {
return _num >= 0 ? POSITIVE : NEGATIVE;
}
std::ostream& operator<<( std::ostream& os, const Rational &ratio ) {
if( ratio == 0 ) {
os << "0";
programminghomeworkhelp.com
} else {
if( ratio.sign() == Rational::NEGATIVE ) {
os << "-";
}
os << std::abs( ratio.num() ) << "/" << std::abs( ratio.den() );
}
return os;
}
void Rational::normalize() {
if( _den == 0 ) {
throw bad_rational();
}
if( _num == 0 ) {
_den = 1; return;
}
auto g = gcd( std::abs( _num ), std::abs( _den ) );
_num /= g; _den /= g;
if( _den < 0 ) {
_num = -_num;
_den = -_den;
}
}
programminghomeworkhelp.com
float Rational::to_float() const {
return static_cast<float>( _num ) / static_cast<float>( _den );
}
double Rational::to_double()const {
return static_cast<double>( _num ) / static_cast<double>( _den );
}
Below is the output using the test data:
rational:
1: OK [0.007 seconds] OK! add
2: OK [0.006 seconds] OK! mult
3: OK [0.009 seconds] OK! add1024
4: OK [0.014 seconds] OK! add1024
5: OK [0.158 seconds] OK! add32768
6: OK [0.007 seconds] OK! op<<
7: OK [0.289 seconds] OK! div65536 in 0.280000 s
8: OK [0.006 seconds] OK! phi, 0.000000e+00
9: OK [0.006 seconds] OK! (Bad rational: zero denominator)
10: OK [0.006 seconds] OK! xyz
11: OK [0.007 seconds] OK! pow2
12: OK [0.006 seconds] OK! x1z
programminghomeworkhelp.com

Computer Science Assignment Help

  • 1.
    For any helpregarding Electrical Engineering and Computer Science Assignment Help Visit :- https://www.programminghomeworkhelp.com/ , Email :- support@programminghomeworkhelp.com or call us at :- +1 678 648 4277 programminghomeworkhelp.com
  • 2.
    Problem 3: RationalNumber Library (rational) Now that we know about the rounding issues of floating-point arithmetic, we’d like to implement a library that will allow for exact computation with rational numbers. This library should be easy and intuitive to use. You want to be able to write code as straight-forward as you can see in this sample: auto a = Rational{ 1, 3 }; // the number 1/3 auto b = Rational{ -6, 7 }; // the number -6/7 std::cout «« "a * b = " «« a «« " * " «« b «« «« a * b «« ; std::cout «« "a / ( a + b / a ) = " «« a / ( a + b / a ) «« ; which produces the following as output. a * b = 1I3 * -6/7 = -2/7 a / ( a + b / a ) = -7/47 Of course, we’ll be able to do much more impressive things. For example, here is a snippet of code that computes the golden ratio ϕ to over 15 decimal digits of accuracy using a very readable implementation of its continued fraction expansion. programminghomeworkhelp.com Problems
  • 3.
    const int ITER= 40; auto phi = Rational{ 1 }; // set equal to 1 to start off with for( int i = 0; i « ITER; ++i ) { phi = 1 / ( 1 + phi ); } std::cout «« std::setprecision( 15 ); // look up «iomanip> std::cout «« "phi = " «« ( 1 + phi ).to double() «« "n"; This prints out phi = 1.61803398874989, which happens to be 15 completely accurate digits. You can find the zipped project at provided in the file rational.zip as a basis for your program. As in the other project problem, the folder contains an include I directory, our standard project Make file, and a srcI directory. All of our header (.h) files will be found in include I while all of our source (.cpp) files will be in srcI. The C++ files you should find are rational. h, gcd. h, rational.cpp, gcd.cpp, and test.cpp; you will be modifying rational.h and rational.cpp. Like in the previous project problem, you can modify the Make file or test.cpp to run your own tests, but your changes there will be overwritten when you upload to the grader. We are provided a header file describing the interface for the Rational class. programminghomeworkhelp.com
  • 4.
    #ifndef _6S096 _RATIONAL_H #define _6S096 _RATIONAL _H #include «cstdint> #include «iosfwd> #include «stdexcept> class Rational { intmax t num, den; public: enum sign type { POSITIVE, NEGATIVE }; Rational() : num{0}, den{1} {} Rational( intmax t numer ) : num{numer}, den{1} {} Rational( intmax t numer, intmax t denom ) : num{numer}, den{denom} { normalize(); II reduces the Rational to lowest terms } inline intmax t num() const { return num; } inline intmax t den() const { return den; } // you'll implement all these in rational.cpp void normalize(); float to float() const; double to double() const; programminghomeworkhelp.com
  • 5.
    sign type sign()const; Rational inverse() const; }; // An example multiplication operator inline Rational operator*( const Rational &a, const Rational &b ) { return Rational{ a.num() * b.num(), a.den() * b.den() }; } //Functions you'll be implementing: std::ostream& operator««( std::ostream& os, const Rational &ratio ); // ... and so on Notice the include guards at the top, protecting us from including the file multiple times. An explanation of some of the constructs we use is in order. You’ll notice we use the intmax t type from the «cstdint> library. This type represents the maximum width integer type for our particular architecture (basically, we want to use the largest integers available). On my 64-bit computer, this means that we’lll find size of(Rational) to be 16 since it comprises two 64-bit (8-byte) integers. programminghomeworkhelp.com
  • 6.
    Look up anenum. We use this to define a type sign type in the scope of our class. Outside the class, we can refer to the type as Rational::sign type and its two values as Rational::POSITIVE and Rational::NEGATIVE. Contained in the header file is also a class that extends std::domain error to create a special type of exception for our Rational class. Look up the meaning of explicit; why do we use it in this particular constructor definition? #include «stdexcept> // ...near the bottom of the file class bad rational : public std::domain error { public: explicit bad rational() : std::domain error( "Bad rational: zero denom." ) {} }; Read through the code carefully and make sure you understand it. If you have any questions about the provided code or don’t know why something is structured the way it is, please ask about it on Piazza. This is the list of functions you should fill out. In the header file, you have a number of inline functions to complete, as shown here. programminghomeworkhelp.com
  • 7.
    inline Rational operator+(const Rational &a, const Rational &b ) {I* ... *I} inline Rational operator-( const Rational &a, const Rational &b ) {I* ... *I} inline Rational operator /( const Rational &a, const Rational &b ) {I* ... *I} inline bool operator«( const Rational &lhs, const Rational &rhs ) {I* ... *I} inline bool operator==( const Rational &lhs, const Rational &rhs ) {I* ... *I} There are also a number of functions declared in the header file which you should write an implementation for in the source file rational.cpp. Those declarations are: class Rational { // ...snipped public: // ...snipped void normalize(); float to float() const; double to double() const; sign type sign() const; }; std::ostream& operator««( std::ostream& os, const Rational &ratio ); programminghomeworkhelp.com
  • 8.
    Look in theexisting rational.cpp file for extensive descriptions of the required functionality. In gcd.h, you can find the declaration for two functions which you may find useful: fast computation of the greatest common divisor and least common multiple of two integers. Feel free to include this header and use these functions in your code as needed. #ifndef _6S096 _GCD _H #define _6S096 _GCD _H #include «cstdint> intmax _t gcd( intmax t a, intmax _t b ); intmax _t lcm( intmax _t a, intmax _t b ); #endif // _6S096 _GCD _H Input Format Not applicable; your library will be compiled into a testing suite, your implemented functions will be called by the program, and the behavior checked for correctness. For example, here is a potential test: programminghomeworkhelp.com
  • 9.
    #include "rational.h" #include «cassert> voidtest addition() { // Let's try adding 1/3 + 2/15 = 7/15. auto sum = Rational{ 1, 3 } + Rational{ 2, 15 } assert( sum.num() == 7 ); assert( sum.den() == 15 ); } You are strongly encouraged to write your own tests in test.cpp so that you can try out your implementation code before submitting it to the online grader. Output Format Not applicable. programminghomeworkhelp.com
  • 10.
    Here are thecontents of rational.h: #ifndef _6S096_RATIONAL_H #define _6S096_RATIONAL_H #include <cstdint> #include <iosfwd> #include <stdexcept> Solutions class Rational { intmax_t _num, _den; public: enum sign_type { POSITIVE, NEGATIVE }; Rational() : _num{0}, _den{1} {} Rational( intmax_t numer ) : _num{numer}, _den{1} {} Rational( intmax_t numer, intmax_t denom ) : _num{numer}, _den{denom} { normalize(); } inline intmax_t num() const { return _num; } inline intmax_t den() const { return _den; } programminghomeworkhelp.com
  • 11.
    void normalize(); float to_float()const; doubleto_double()const; sign_type sign() const; Rational inverse() const; }; std::ostream& operator<<( std::ostream& os, const Rational &ratio ); inline bool operator==( const Rational &lhs, const Rational &rhs ) { return lhs.num() * rhs.den() == rhs.num() * lhs.den(); } inline bool operator<( const Rational &lhs, const Rational &rhs ) { if( lhs.sign() == Rational::POSITIVE && rhs.sign() == Rational::POSITIVE ) { return lhs.num() * rhs.den() < rhs.num() * lhs.den(); } else if( lhs.sign() == Rational::NEGATIVE && rhs.sign() == Rational::NEGATIVE ) { return lhs.num() * rhs.den() > rhs.num() * lhs.den(); } else { return lhs.sign() == Rational::NEGATIVE; } } programminghomeworkhelp.com
  • 12.
    inline Rational operator*(const Rational &a, const Rational &b ) { return Rational{ a.num() * b.num(), a.den() * b.den() }; } inline Rational operator+( const Rational &a, const Rational &b ) { return Rational{ a.num() * b.den() + b.num() * a.den(), a.den() * b.den() }; } inline Rational operator-( const Rational &a, const Rational &b ) { return Rational{ a.num() * b.den() - b.num() * a.den(), a.den() * b.den() }; } inline Rational operator/( const Rational &a, const Rational &b ) { return a * b.inverse(); } class bad_rational : public std::domain_error { public: explicit bad_rational() : std::domain_error("Bad rational: zero denominator" ) {} }; programminghomeworkhelp.com
  • 13.
    #endif // _6S096_RATIONAL_H Hereis the source code file rational.cpp: #include "rational.h" #include "gcd.h" #include <stdexcept> #include <ostream> #include <iostream> #include <cmath> Rational Rational::inverse() const { return Rational{ _den, _num }; } Rational::sign_type Rational::sign()const { return _num >= 0 ? POSITIVE : NEGATIVE; } std::ostream& operator<<( std::ostream& os, const Rational &ratio ) { if( ratio == 0 ) { os << "0"; programminghomeworkhelp.com
  • 14.
    } else { if(ratio.sign() == Rational::NEGATIVE ) { os << "-"; } os << std::abs( ratio.num() ) << "/" << std::abs( ratio.den() ); } return os; } void Rational::normalize() { if( _den == 0 ) { throw bad_rational(); } if( _num == 0 ) { _den = 1; return; } auto g = gcd( std::abs( _num ), std::abs( _den ) ); _num /= g; _den /= g; if( _den < 0 ) { _num = -_num; _den = -_den; } } programminghomeworkhelp.com
  • 15.
    float Rational::to_float() const{ return static_cast<float>( _num ) / static_cast<float>( _den ); } double Rational::to_double()const { return static_cast<double>( _num ) / static_cast<double>( _den ); } Below is the output using the test data: rational: 1: OK [0.007 seconds] OK! add 2: OK [0.006 seconds] OK! mult 3: OK [0.009 seconds] OK! add1024 4: OK [0.014 seconds] OK! add1024 5: OK [0.158 seconds] OK! add32768 6: OK [0.007 seconds] OK! op<< 7: OK [0.289 seconds] OK! div65536 in 0.280000 s 8: OK [0.006 seconds] OK! phi, 0.000000e+00 9: OK [0.006 seconds] OK! (Bad rational: zero denominator) 10: OK [0.006 seconds] OK! xyz 11: OK [0.007 seconds] OK! pow2 12: OK [0.006 seconds] OK! x1z programminghomeworkhelp.com