void rational::normalize() { // normalize rational by (a) moving sign to numerator :// b) making sure numerator and denominator have no common divisorsint sign = 1; // sign is 1 if non-negative, -1 if negativeif (top < 0) { sign = -1; top = - top;}if (bottom < 0) { sign = - sign; bottom = - bottom;}// make sure we are not dividing by zeroif (bottom == 0)throw range_error("fraction with zero numerator");// find greatest common divisor int d = gcd(top, bottom);// move sign to top and divide by gcdtop = sign * (top / d);bottom = bottom / d;
void rational::normalize() { // normalize rational by (a) moving sign to numerator :// b) making sure numerator and denominator have no common divisorsint sign = 1; // sign is 1 if non-negative, -1 if negativeif (top < 0) { sign = -1; top = - top;}if (bottom < 0) { sign = - sign; bottom = - bottom;}// make sure we are not dividing by zeroif (bottom == 0)throw range_error("fraction with zero numerator");// find greatest common divisor int d = gcd(top, bottom);// move sign to top and divide by gcdtop = sign * (top / d);bottom = bottom / d;