# Rational Numbers A rational number is defined as the quotient of two integers `a` and `b`, called the numerator and denominator, respectively, where `b != 0`. The absolute value `|r|` of the rational number `r = a/b` is equal to `|a|/|b|`. The sum of two rational numbers `r1 = a1/b1` and `r2 = a2/b2` is `r1 + r2 = a1/b1 + a2/b2 = (a1 * b2 + a2 * b1) / (b1 * b2)`. The difference of two rational numbers `r1 = a1/b1` and `r2 = a2/b2` is `r1 - r2 = a1/b1 - a2/b2 = (a1 * b2 - a2 * b1) / (b1 * b2)`. The product (multiplication) of two rational numbers `r1 = a1/b1` and `r2 = a2/b2` is `r1 * r2 = (a1 * a2) / (b1 * b2)`. Dividing a rational number `r1 = a1/b1` by another `r2 = a2/b2` is `r1 / r2 = (a1 * b2) / (a2 * b1)` if `a2 * b1` is not zero. Exponentiation of a rational number `r = a/b` to a non-negative integer power `n` is `r^n = (a^n)/(b^n)`. Exponentiation of a rational number `r = a/b` to a negative integer power `n` is `r^n = (b^m)/(a^m)`, where `m = |n|`. Exponentiation of a rational number `r = a/b` to a real (floating-point) number `x` is the quotient `(a^x)/(b^x)`, which is a real number. Exponentiation of a real number `x` to a rational number `r = a/b` is `x^(a/b) = root(x^a, b)`, where `root(p, q)` is the `q`th root of `p`. Implement the following operations: - addition, subtraction, multiplication and division of two rational numbers, - absolute value, exponentiation of a given rational number to an integer power, exponentiation of a given rational number to a real (floating-point) power, exponentiation of a real number to a rational number. Your implementation of rational numbers should always be reduced to lowest terms. For example, `4/4` should reduce to `1/1`, `30/60` should reduce to `1/2`, `12/8` should reduce to `3/2`, etc. To reduce a rational number `r = a/b`, divide `a` and `b` by the greatest common divisor (gcd) of `a` and `b`. So, for example, `gcd(12, 8) = 4`, so `r = 12/8` can be reduced to `(12/4)/(8/4) = 3/2`. Assume that the programming language you are using does not have an implementation of rational numbers. ## Exception messages Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include a message. To raise a message with an exception, just write it as an argument to the exception type. For example, instead of `raise Exception`, you should write: ```python raise Exception("Meaningful message indicating the source of the error") ``` ## Running the tests To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)): - Python 2.7: `py.test rational_numbers_test.py` - Python 3.3+: `pytest rational_numbers_test.py` Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version): `python -m pytest rational_numbers_test.py` ### Common `pytest` options - `-v` : enable verbose output - `-x` : stop running tests on first failure - `--ff` : run failures from previous test before running other test cases For other options, see `python -m pytest -h` ## Submitting Exercises Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/rational-numbers` directory. You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`. For more detailed information about running tests, code style and linting, please see the [help page](http://exercism.io/languages/python). ## Source Wikipedia [https://en.wikipedia.org/wiki/Rational_number](https://en.wikipedia.org/wiki/Rational_number) ## Submitting Incomplete Solutions It's possible to submit an incomplete solution so you can see how others have completed the exercise.