C: rational-numbers
This commit is contained in:
51
c/rational-numbers/GNUmakefile
Normal file
51
c/rational-numbers/GNUmakefile
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
# The original 'makefile' has a flaw:
|
||||||
|
# 1) it overrides CFLAGS
|
||||||
|
# 2) it does not pass extra "FLAGS" to $(CC) that could come from environment
|
||||||
|
#
|
||||||
|
# It means :
|
||||||
|
# - we need to edit 'makefile' for different builds (DEBUG, etc...), which is
|
||||||
|
# not practical at all.
|
||||||
|
# - Also, it does not allow to run all tests without editing the test source
|
||||||
|
# code.
|
||||||
|
#
|
||||||
|
# To use this makefile (GNU make only):
|
||||||
|
# "make": build with all predefined tests (without editing test source code)
|
||||||
|
# "make debugall": build with all predefined tests and debug code
|
||||||
|
# "make mem": perform memcheck with all tests enabled
|
||||||
|
# "make unit": build standalone (unit) test
|
||||||
|
# "make debug": build standalone test with debugging code
|
||||||
|
#
|
||||||
|
# Original 'makefile' targets can be used (test, memcheck, clean, ...)
|
||||||
|
|
||||||
|
.PHONY: default all mem unit debug std debugtest
|
||||||
|
|
||||||
|
default: all
|
||||||
|
|
||||||
|
# default is to build with all predefined tests
|
||||||
|
BUILD := teststall
|
||||||
|
|
||||||
|
include makefile
|
||||||
|
|
||||||
|
all: CFLAGS+=-DTESTALL
|
||||||
|
all: clean test
|
||||||
|
|
||||||
|
debugall: CFLAGS+=-DDEBUG
|
||||||
|
debugall: all
|
||||||
|
|
||||||
|
debugtest: CFLAGS+=-DDEBUG
|
||||||
|
debugtest: test
|
||||||
|
|
||||||
|
mem: CFLAGS+=-DTESTALL
|
||||||
|
mem: clean memcheck
|
||||||
|
|
||||||
|
unit: CFLAGS+=-DUNIT_TEST
|
||||||
|
unit: clean std
|
||||||
|
|
||||||
|
debug: CFLAGS+=-DUNIT_TEST -DDEBUG
|
||||||
|
debug: clean std
|
||||||
|
|
||||||
|
debugtest: CFLAGS+=-DDEBUG
|
||||||
|
debugtest: test
|
||||||
|
|
||||||
|
std: src/*.c src/*.h
|
||||||
|
$(CC) $(CFLAGS) src/*.c -o tests.out $(LIBS)
|
67
c/rational-numbers/README.md
Normal file
67
c/rational-numbers/README.md
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# 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 `r₁ = a₁/b₁` and `r₂ = a₂/b₂` is `r₁ + r₂ = a₁/b₁ + a₂/b₂ = (a₁ * b₂ + a₂ * b₁) / (b₁ * b₂)`.
|
||||||
|
|
||||||
|
The difference of two rational numbers `r₁ = a₁/b₁` and `r₂ = a₂/b₂` is `r₁ - r₂ = a₁/b₁ - a₂/b₂ = (a₁ * b₂ - a₂ * b₁) / (b₁ * b₂)`.
|
||||||
|
|
||||||
|
The product (multiplication) of two rational numbers `r₁ = a₁/b₁` and `r₂ = a₂/b₂` is `r₁ * r₂ = (a₁ * a₂) / (b₁ * b₂)`.
|
||||||
|
|
||||||
|
Dividing a rational number `r₁ = a₁/b₁` by another `r₂ = a₂/b₂` is `r₁ / r₂ = (a₁ * b₂) / (a₂ * b₁)` if `a₂` 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.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
Make sure you have read the "Guides" section of the
|
||||||
|
[C track][c-track] on the Exercism site. This covers
|
||||||
|
the basic information on setting up the development environment expected
|
||||||
|
by the exercises.
|
||||||
|
|
||||||
|
## Passing the Tests
|
||||||
|
|
||||||
|
Get the first test compiling, linking and passing by following the [three
|
||||||
|
rules of test-driven development][3-tdd-rules].
|
||||||
|
|
||||||
|
The included makefile can be used to create and run the tests using the `test`
|
||||||
|
task.
|
||||||
|
|
||||||
|
make test
|
||||||
|
|
||||||
|
Create just the functions you need to satisfy any compiler errors and get the
|
||||||
|
test to fail. Then write just enough code to get the test to pass. Once you've
|
||||||
|
done that, move onto the next test.
|
||||||
|
|
||||||
|
As you progress through the tests, take the time to refactor your
|
||||||
|
implementation for readability and expressiveness and then go on to the next
|
||||||
|
test.
|
||||||
|
|
||||||
|
Try to use standard C99 facilities in preference to writing your own
|
||||||
|
low-level algorithms or facilities by hand.
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
[c-track]: https://exercism.io/my/tracks/c
|
||||||
|
[3-tdd-rules]: http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
|
37
c/rational-numbers/makefile
Normal file
37
c/rational-numbers/makefile
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
### If you wish to use extra libraries (math.h for instance),
|
||||||
|
### add their flags here (-lm in our case) in the "LIBS" variable.
|
||||||
|
|
||||||
|
LIBS = -lm
|
||||||
|
|
||||||
|
###
|
||||||
|
CFLAGS = -std=c99
|
||||||
|
CFLAGS += -g
|
||||||
|
CFLAGS += -Wall
|
||||||
|
CFLAGS += -Wextra
|
||||||
|
CFLAGS += -pedantic
|
||||||
|
CFLAGS += -Werror
|
||||||
|
CFLAGS += -Wmissing-declarations
|
||||||
|
CFLAGS += -DUNITY_SUPPORT_64
|
||||||
|
|
||||||
|
ASANFLAGS = -fsanitize=address
|
||||||
|
ASANFLAGS += -fno-common
|
||||||
|
ASANFLAGS += -fno-omit-frame-pointer
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test: tests.out
|
||||||
|
@./tests.out
|
||||||
|
|
||||||
|
.PHONY: memcheck
|
||||||
|
memcheck: test/*.c src/*.c src/*.h
|
||||||
|
@echo Compiling $@
|
||||||
|
@$(CC) $(ASANFLAGS) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o memcheck.out $(LIBS)
|
||||||
|
@./memcheck.out
|
||||||
|
@echo "Memory check passed"
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf *.o *.out *.out.dSYM
|
||||||
|
|
||||||
|
tests.out: test/*.c src/*.c src/*.h
|
||||||
|
@echo Compiling $@
|
||||||
|
@$(CC) $(CFLAGS) src/*.c test/vendor/unity.c test/*.c -o tests.out $(LIBS)
|
115
c/rational-numbers/src/rational_numbers.c
Normal file
115
c/rational-numbers/src/rational_numbers.c
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
#include "rational_numbers.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define N(r) ((r).numerator)
|
||||||
|
#define D(r) ((r).denominator)
|
||||||
|
|
||||||
|
/* Note. We should probably check for possible overflow in all
|
||||||
|
* functions below (not covered in exercise).
|
||||||
|
* To do this, a simple solution could be to make operations with
|
||||||
|
* long long (or some other method), and add a field in rational_t
|
||||||
|
* to express such overflow, division by zero, etc...
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Euclidean algorithm (by Donald Knuth) */
|
||||||
|
rational_t reduce(rational_t r)
|
||||||
|
{
|
||||||
|
int16_t a=abs(N(r)), b=abs(D(r)), t;
|
||||||
|
|
||||||
|
while (b != 0) {
|
||||||
|
t = b;
|
||||||
|
b = a % b;
|
||||||
|
a = t;
|
||||||
|
}
|
||||||
|
if (D(r) < 0)
|
||||||
|
a=-a;
|
||||||
|
return (rational_t) { N(r)/a, D(r)/a };
|
||||||
|
}
|
||||||
|
|
||||||
|
/* to avoid pow() for integers
|
||||||
|
* BUG: does not check for overflow
|
||||||
|
*/
|
||||||
|
static inline int power(int n, int p)
|
||||||
|
{
|
||||||
|
int res=n;
|
||||||
|
|
||||||
|
if (p==0)
|
||||||
|
return 1;
|
||||||
|
while (--p)
|
||||||
|
res*=n;
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* All formulas below come from https://en.wikipedia.org/wiki/Rational_number
|
||||||
|
*/
|
||||||
|
rational_t add(rational_t r1, rational_t r2)
|
||||||
|
{
|
||||||
|
return reduce((rational_t) {
|
||||||
|
N(r1) * D(r2) + N(r2) * D(r1),
|
||||||
|
D(r1) * D(r2)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
rational_t subtract(rational_t r1, rational_t r2)
|
||||||
|
{
|
||||||
|
return reduce((rational_t) {
|
||||||
|
N(r1) * D(r2) - N(r2) * D(r1),
|
||||||
|
D(r1) * D(r2)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
rational_t multiply(rational_t r1, rational_t r2)
|
||||||
|
{
|
||||||
|
return reduce((rational_t) {
|
||||||
|
N(r1) * N(r2),
|
||||||
|
D(r1) * D(r2)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
rational_t divide(rational_t r1, rational_t r2)
|
||||||
|
{
|
||||||
|
return reduce((rational_t) {
|
||||||
|
N(r1) * D(r2),
|
||||||
|
D(r1) * N(r2)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
rational_t absolute(rational_t r)
|
||||||
|
{
|
||||||
|
return (rational_t) {
|
||||||
|
abs(N(r)),
|
||||||
|
abs(D(r))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
rational_t exp_rational(rational_t r, uint16_t n)
|
||||||
|
{
|
||||||
|
return reduce((rational_t) {
|
||||||
|
power(N(r), n),
|
||||||
|
power(D(r), n)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
float exp_real(uint16_t x, rational_t r)
|
||||||
|
{
|
||||||
|
return powf((float)x, (float)N(r)/D(r));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* See GNUmakefile below for explanation
|
||||||
|
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
||||||
|
*/
|
||||||
|
#ifdef UNIT_TEST
|
||||||
|
int main(int ac, char **av)
|
||||||
|
{
|
||||||
|
int arg=1;
|
||||||
|
rational_t r1, r2;
|
||||||
|
|
||||||
|
for (; arg<ac-1; ++arg, ++arg) {
|
||||||
|
r1.numerator=atoi(av[arg]);;
|
||||||
|
r1.denominator=atoi(av[arg+1]);;
|
||||||
|
r2=reduce(r1);
|
||||||
|
printf("reduce(%d, %d)=(%d, %d)\n", N(r1), D(r1), N(r2), D(r2));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
33
c/rational-numbers/src/rational_numbers.h
Normal file
33
c/rational-numbers/src/rational_numbers.h
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#ifndef RATIONAL_NUMBERS
|
||||||
|
#define RATIONAL_NUMBERS
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int16_t numerator;
|
||||||
|
int16_t denominator;
|
||||||
|
} rational_t;
|
||||||
|
|
||||||
|
rational_t add(rational_t r1, rational_t r2);
|
||||||
|
rational_t subtract(rational_t r1, rational_t r2);
|
||||||
|
rational_t multiply(rational_t r1, rational_t r2);
|
||||||
|
rational_t divide(rational_t r1, rational_t r2);
|
||||||
|
rational_t absolute(rational_t r);
|
||||||
|
rational_t exp_rational(rational_t r, uint16_t n);
|
||||||
|
float exp_real(uint16_t x, rational_t r);
|
||||||
|
rational_t reduce(rational_t r);
|
||||||
|
|
||||||
|
/* See GNUmakefile below for explanation
|
||||||
|
* https://github.com/braoult/exercism/blob/master/c/templates/GNUmakefile
|
||||||
|
*/
|
||||||
|
#if defined UNIT_TEST || defined DEBUG
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef TESTALL
|
||||||
|
#undef TEST_IGNORE
|
||||||
|
#define TEST_IGNORE() {}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user