patterncMinor
Rational implementation
Viewed 0 times
implementationrationalstackoverflow
Problem
Can someone review this?
The header file can be viewed here.
```
#include
#include
#include // searches default library classpaths
#include "Rational.h" // searchs my directory
// let's the compiler know that this is a function
static bool Rational_isPositive(Rational rational);
static double absolute(double number);
/**
* Creates and initializes a new Rational object.
* Pre:
* Denominator != 0
* Returns:
* A Rational object X such that X.Top == Numerator
* and X.Bottom = Denominator.
*/
Rational Rational_Construct(int Numerator, int Denominator) {
// make all rationals into the equivalent rational with
// either a postive or negative numerator and never a
// negative denominator
Rational newRational;
if (Numerator = 0 && Denominator = R.
*/
int Rational_Ceiling(const Rational R) {
if (Rational_isPositive(R)) {
if (R.Top % R.Bottom == 0) {
return R.Top / R.Bottom;
} else {
return R.Top / R.Bottom + 1;
}
} else {
return R.Top / R.Bottom;
}
}
/**
* Round R to the nearest integer.
* Pre:
* R has been properly initialized.
* Returns:
* The closest integer N to R.
*/
int Rational_Round(const Rational R) {
double decimalFormat = (double) R.Top / (double) R.Bottom;
double R_ceiling = (double) Rational_Ceiling(R);
double R_floor = (double) Rational_Floor(R);
double distanceToR_ceiling = absolute(R_ceiling - decimalFormat);
double distanceToR_floor = absolute(R_floor - decimalFormat);
// decimalFormat is closer to the ceiling
if (distanceToR_ceiling Right, false otherwise.
*/
bool Rational_GreaterThan(const Rational Left, const Rational Right) {
if (Rational_LessThanOrEqual(Left, Right)) {
return false;
} else {
return true;
}
}
/**
* Determine whether Left is greater than or equal to Right.
* Pre:
* Left and Right have been properly initialized.
* Returns:
* True if Left >= Right, false otherwise.
*/
bool
The header file can be viewed here.
```
#include
#include
#include // searches default library classpaths
#include "Rational.h" // searchs my directory
// let's the compiler know that this is a function
static bool Rational_isPositive(Rational rational);
static double absolute(double number);
/**
* Creates and initializes a new Rational object.
* Pre:
* Denominator != 0
* Returns:
* A Rational object X such that X.Top == Numerator
* and X.Bottom = Denominator.
*/
Rational Rational_Construct(int Numerator, int Denominator) {
// make all rationals into the equivalent rational with
// either a postive or negative numerator and never a
// negative denominator
Rational newRational;
if (Numerator = 0 && Denominator = R.
*/
int Rational_Ceiling(const Rational R) {
if (Rational_isPositive(R)) {
if (R.Top % R.Bottom == 0) {
return R.Top / R.Bottom;
} else {
return R.Top / R.Bottom + 1;
}
} else {
return R.Top / R.Bottom;
}
}
/**
* Round R to the nearest integer.
* Pre:
* R has been properly initialized.
* Returns:
* The closest integer N to R.
*/
int Rational_Round(const Rational R) {
double decimalFormat = (double) R.Top / (double) R.Bottom;
double R_ceiling = (double) Rational_Ceiling(R);
double R_floor = (double) Rational_Floor(R);
double distanceToR_ceiling = absolute(R_ceiling - decimalFormat);
double distanceToR_floor = absolute(R_floor - decimalFormat);
// decimalFormat is closer to the ceiling
if (distanceToR_ceiling Right, false otherwise.
*/
bool Rational_GreaterThan(const Rational Left, const Rational Right) {
if (Rational_LessThanOrEqual(Left, Right)) {
return false;
} else {
return true;
}
}
/**
* Determine whether Left is greater than or equal to Right.
* Pre:
* Left and Right have been properly initialized.
* Returns:
* True if Left >= Right, false otherwise.
*/
bool
Solution
-
I like that you are treating your rationals as immutable by returning new rationals from all your operations.
-
Your naming conventions are a bit unusual. In most C like languages (C, C++, C#, Java) local variables and parameters are
-
You are using the mathematical terms
-
When someone passes in a
Consider forcing the error by causing the div by zero right there or changing the interface of the method to:
and return
-
Most of your comparisons like this
can be shortened to
I like that you are treating your rationals as immutable by returning new rationals from all your operations.
-
Your naming conventions are a bit unusual. In most C like languages (C, C++, C#, Java) local variables and parameters are
camelCase. Specifically in C method names tend to be camelCase or snake_case. -
You are using the mathematical terms
Numerator and Denominator for the parameters of you construction function however you use Top and Bottom for the properties of you Rational type which seems a bit unusual - why not stick to the mathematical terms?-
When someone passes in a
Denominator which is 0 you just have a printf in there. - If anything it should print at least to
stderr.
- It doesn't really alert the programer to the error and just fails in some later operations anyway (like
Round,Floor,Ceil).
Consider forcing the error by causing the div by zero right there or changing the interface of the method to:
bool Rational_Construct(int numerator, int denominator, Rational* result)and return
false if the input is invalid.-
Most of your comparisons like this
if (Left.Top * Right.Bottom == Left.Bottom * Right.Top) {
return true;
} else {
return false;
}can be shortened to
return Left.Top * Right.Bottom == Left.Bottom * Right.Top;Code Snippets
bool Rational_Construct(int numerator, int denominator, Rational* result)if (Left.Top * Right.Bottom == Left.Bottom * Right.Top) {
return true;
} else {
return false;
}return Left.Top * Right.Bottom == Left.Bottom * Right.Top;Context
StackExchange Code Review Q#32337, answer score: 7
Revisions (0)
No revisions yet.