1 /* 2 Name: imrat.h 3 Purpose: Arbitrary precision rational arithmetic routines. 4 Author: M. J. Fromberger <http://spinning-yarns.org/michael/> 5 6 Copyright (C) 2002-2007 Michael J. Fromberger, All Rights Reserved. 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 SOFTWARE. 25 */ 26 27 #ifndef IMRAT_H_ 28 #define IMRAT_H_ 29 30 #include "imath.h" 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 typedef struct mpq { 37 mpz_t num; /* Numerator */ 38 mpz_t den; /* Denominator, <> 0 */ 39 } mpq_t, *mp_rat; 40 41 #define MP_NUMER_P(Q) (&((Q)->num)) /* Pointer to numerator */ 42 #define MP_DENOM_P(Q) (&((Q)->den)) /* Pointer to denominator */ 43 44 /* Rounding constants */ 45 typedef enum { 46 MP_ROUND_DOWN, 47 MP_ROUND_HALF_UP, 48 MP_ROUND_UP, 49 MP_ROUND_HALF_DOWN 50 } mp_round_mode; 51 52 mp_result mp_rat_init(mp_rat r); 53 mp_rat mp_rat_alloc(void); 54 mp_result mp_rat_reduce(mp_rat r); 55 mp_result mp_rat_init_size(mp_rat r, mp_size n_prec, mp_size d_prec); 56 mp_result mp_rat_init_copy(mp_rat r, mp_rat old); 57 mp_result mp_rat_set_value(mp_rat r, mp_small numer, mp_small denom); 58 mp_result mp_rat_set_uvalue(mp_rat r, mp_usmall numer, mp_usmall denom); 59 void mp_rat_clear(mp_rat r); 60 void mp_rat_free(mp_rat r); 61 mp_result mp_rat_numer(mp_rat r, mp_int z); /* z = num(r) */ 62 mp_int mp_rat_numer_ref(mp_rat r); /* &num(r) */ 63 mp_result mp_rat_denom(mp_rat r, mp_int z); /* z = den(r) */ 64 mp_int mp_rat_denom_ref(mp_rat r); /* &den(r) */ 65 mp_sign mp_rat_sign(mp_rat r); 66 67 mp_result mp_rat_copy(mp_rat a, mp_rat c); /* c = a */ 68 void mp_rat_zero(mp_rat r); /* r = 0 */ 69 mp_result mp_rat_abs(mp_rat a, mp_rat c); /* c = |a| */ 70 mp_result mp_rat_neg(mp_rat a, mp_rat c); /* c = -a */ 71 mp_result mp_rat_recip(mp_rat a, mp_rat c); /* c = 1 / a */ 72 mp_result mp_rat_add(mp_rat a, mp_rat b, mp_rat c); /* c = a + b */ 73 mp_result mp_rat_sub(mp_rat a, mp_rat b, mp_rat c); /* c = a - b */ 74 mp_result mp_rat_mul(mp_rat a, mp_rat b, mp_rat c); /* c = a * b */ 75 mp_result mp_rat_div(mp_rat a, mp_rat b, mp_rat c); /* c = a / b */ 76 77 mp_result mp_rat_add_int(mp_rat a, mp_int b, mp_rat c); /* c = a + b */ 78 mp_result mp_rat_sub_int(mp_rat a, mp_int b, mp_rat c); /* c = a - b */ 79 mp_result mp_rat_mul_int(mp_rat a, mp_int b, mp_rat c); /* c = a * b */ 80 mp_result mp_rat_div_int(mp_rat a, mp_int b, mp_rat c); /* c = a / b */ 81 mp_result mp_rat_expt(mp_rat a, mp_small b, mp_rat c); /* c = a ^ b */ 82 83 int mp_rat_compare(mp_rat a, mp_rat b); /* a <=> b */ 84 int mp_rat_compare_unsigned(mp_rat a, mp_rat b); /* |a| <=> |b| */ 85 int mp_rat_compare_zero(mp_rat r); /* r <=> 0 */ 86 int mp_rat_compare_value(mp_rat r, mp_small n, mp_small d); /* r <=> n/d */ 87 int mp_rat_is_integer(mp_rat r); 88 89 /* Convert to integers, if representable (returns MP_RANGE if not). */ 90 mp_result mp_rat_to_ints(mp_rat r, mp_small *num, mp_small *den); 91 92 /* Convert to nul-terminated string with the specified radix, writing 93 at most limit characters including the nul terminator. */ 94 mp_result mp_rat_to_string(mp_rat r, mp_size radix, char *str, int limit); 95 96 /* Convert to decimal format in the specified radix and precision, 97 writing at most limit characters including a nul terminator. */ 98 mp_result mp_rat_to_decimal(mp_rat r, mp_size radix, mp_size prec, 99 mp_round_mode round, char *str, int limit); 100 101 /* Return the number of characters required to represent r in the given 102 radix. May over-estimate. */ 103 mp_result mp_rat_string_len(mp_rat r, mp_size radix); 104 105 /* Return the number of characters required to represent r in decimal 106 format with the given radix and precision. May over-estimate. */ 107 mp_result mp_rat_decimal_len(mp_rat r, mp_size radix, mp_size prec); 108 109 /* Read zero-terminated string into r */ 110 mp_result mp_rat_read_string(mp_rat r, mp_size radix, const char *str); 111 mp_result mp_rat_read_cstring(mp_rat r, mp_size radix, const char *str, 112 char **end); 113 mp_result mp_rat_read_ustring(mp_rat r, mp_size radix, const char *str, 114 char **end); 115 116 /* Read zero-terminated string in decimal format into r */ 117 mp_result mp_rat_read_decimal(mp_rat r, mp_size radix, const char *str); 118 mp_result mp_rat_read_cdecimal(mp_rat r, mp_size radix, const char *str, 119 char **end); 120 121 #ifdef __cplusplus 122 } 123 #endif 124 #endif /* IMRAT_H_ */ 125