diff options
| author | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
|---|---|---|
| committer | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
| commit | b3e9e62599532050fc776c5e8f076915b56c2235 (patch) | |
| tree | af252346106a61b18cc6fc6fdbd32e962d096f1c /rationals.h | |
Import official C23 code examples for Modern C (Jens Gustedt, 2024)HEADupstream-importmain
- Add official C source files, Makefile, c23-fallback.h, and LICENSE
- Update README.md with study mirror notice
Diffstat (limited to 'rationals.h')
| -rw-r--r-- | rationals.h | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/rationals.h b/rationals.h new file mode 100644 index 0000000..d69b515 --- /dev/null +++ b/rationals.h @@ -0,0 +1,41 @@ +#ifndef RATIONALS_H +# define RATIONALS_H 1 +# include <stdbool.h> +# include "euclid.h" + +typedef struct rat rat; + +struct rat { + bool sign; + size_t num; + size_t denom; +}; + +/* Functions that return a value of type rat. */ +rat rat_get(signed sign, size_t num, size_t denom) [[__unsequenced__]]; +rat rat_get_normal(rat x) [[__unsequenced__]]; +rat rat_get_extended(rat x, size_t f) [[__unsequenced__]]; +rat rat_get_prod(rat x, rat y) [[__unsequenced__]]; +rat rat_get_sum(rat x, rat y) [[__unsequenced__]]; + + +/* Functions that operate on pointers to rat. */ +void rat_destroy(rat* rp) [[__unsequenced__]]; +rat* rat_init(rat* rp, + signed sign, + size_t num, size_t denom) [[__unsequenced__]]; +rat* rat_normalize(rat* rp) [[__unsequenced__]]; +rat* rat_extend(rat* rp, size_t f) [[__unsequenced__]]; +rat* rat_sumup(rat* rp, rat y) [[__unsequenced__]]; +rat* rat_rma(rat* rp, rat x, rat y) [[__unsequenced__]]; + +/* Functions that are implemented as exercises. */ +/** @brief Print @a x into @a tmp and return tmp. **/ +char const* rat_print(size_t len, char tmp[len], rat const* x); +/** @brief Print @a x normalize and print. **/ +char const* rat_normalize_print(size_t len, char tmp[len], + rat const* x); +rat* rat_dotproduct(rat rp[static 1], size_t n, + rat const A[n], rat const B[n]); + +#endif |
