Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted. |
| 8 | * |
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | * PERFORMANCE OF THIS SOFTWARE. |
| 16 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 18 | * |
| 19 | * Code for the number type. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <string.h> |
| 26 | |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 27 | #include <math.h> |
| 28 | |
| 29 | #include <limits.h> |
| 30 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 31 | #include <status.h> |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 32 | #include <num.h> |
Gavin Howard | f507f23 | 2018-02-23 21:10:24 -0700 | [diff] [blame] | 33 | #include <vector.h> |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 34 | #include <bc.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 35 | |
Gavin Howard | aa53f7c | 2018-03-23 13:22:36 -0600 | [diff] [blame] | 36 | void bc_num_subArrays(BcDigit *n1, BcDigit *n2, size_t len) { |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 37 | |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 38 | size_t i, j; |
| 39 | |
| 40 | for (i = 0; i < len; ++i) { |
| 41 | |
Gavin Howard | cfc5faa | 2018-03-20 18:22:42 -0600 | [diff] [blame] | 42 | n1[i] -= n2[i]; |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 43 | |
Gavin Howard | cfc5faa | 2018-03-20 18:22:42 -0600 | [diff] [blame] | 44 | for (j = 0; n1[i + j] < 0;) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 45 | n1[i + j++] += 10; |
Gavin Howard | cfc5faa | 2018-03-20 18:22:42 -0600 | [diff] [blame] | 46 | n1[i + j] -= 1; |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 47 | } |
| 48 | } |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 49 | } |
| 50 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 51 | int bc_num_compare(BcDigit *n1, BcDigit *n2, size_t len, size_t *digits) { |
| 52 | |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 53 | size_t i, digs; |
| 54 | BcDigit c; |
| 55 | |
Gavin Howard | 2168fb8 | 2018-03-24 10:40:37 -0600 | [diff] [blame] | 56 | for (c = 0, digs = 0, i = len - 1; i < len; ++digs, --i) { |
Gavin Howard | 2a064fd | 2018-03-20 15:56:12 -0600 | [diff] [blame] | 57 | if ((c = n1[i] - n2[i])) break; |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | if (digits) *digits = digs; |
| 61 | |
| 62 | return c; |
| 63 | } |
| 64 | |
Gavin Howard | 2a064fd | 2018-03-20 15:56:12 -0600 | [diff] [blame] | 65 | int bc_num_cmp(BcNum *a, BcNum *b, size_t *digits) { |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 66 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 67 | size_t i, min, a_int, b_int, diff; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 68 | BcDigit *max_num, *min_num; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 69 | bool a_max; |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 70 | int cmp, neg; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 71 | |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 72 | if (digits) *digits = 0; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 73 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 74 | if (!a) return !b ? 0 : !b->neg * -2 + 1; |
| 75 | else if (!b) return a->neg * -2 + 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 76 | |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 77 | neg = 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 78 | |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 79 | if (a->neg) { |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 80 | if (b->neg) neg = -1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 81 | else return -1; |
| 82 | } |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 83 | else if (b->neg) return 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 84 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 85 | if (!a->len) return (!b->neg * -2 + 1) * !!b->len; |
| 86 | else if (!b->len) return a->neg * -2 + 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 87 | |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 88 | a_int = a->len - a->rdx; |
| 89 | b_int = b->len - b->rdx; |
Gavin Howard | 43ab933 | 2018-03-20 15:02:48 -0600 | [diff] [blame] | 90 | a_int -= b_int; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 91 | |
Gavin Howard | 43ab933 | 2018-03-20 15:02:48 -0600 | [diff] [blame] | 92 | if (a_int) return a_int; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 93 | |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 94 | a_max = a->rdx > b->rdx; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 95 | |
| 96 | if (a_max) { |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 97 | min = b->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 98 | diff = a->rdx - b->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 99 | max_num = a->num + diff; |
| 100 | min_num = b->num; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 101 | } |
| 102 | else { |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 103 | min = a->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 104 | diff = b->rdx - a->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 105 | max_num = b->num + diff; |
| 106 | min_num = a->num; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Gavin Howard | 2a064fd | 2018-03-20 15:56:12 -0600 | [diff] [blame] | 109 | cmp = bc_num_compare(max_num, min_num, b_int + min, digits); |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 110 | |
Gavin Howard | 2ba0711 | 2018-03-20 15:40:58 -0600 | [diff] [blame] | 111 | if (cmp) return cmp * (!a_max * -2 + 1) * neg; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 112 | |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 113 | for (max_num -= diff, i = diff - 1; i < diff; --i) { |
Gavin Howard | 86f32e9 | 2018-03-20 19:21:12 -0600 | [diff] [blame] | 114 | if (max_num[i]) return neg * (!a_max * -2 + 1); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Gavin Howard | d9734e5 | 2018-03-29 15:54:41 -0600 | [diff] [blame^] | 120 | void bc_num_truncate(BcNum *n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 121 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 122 | BcDigit *ptr; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 123 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 124 | assert(places <= n->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 125 | |
Gavin Howard | d9734e5 | 2018-03-29 15:54:41 -0600 | [diff] [blame^] | 126 | if (!places) return; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 127 | |
| 128 | ptr = n->num + places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 129 | n->len -= places; |
| 130 | n->rdx -= places; |
| 131 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 132 | memmove(n->num, ptr, n->len * sizeof(BcDigit)); |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 133 | memset(n->num + n->len, 0, sizeof(BcDigit) * (n->cap - n->len)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 136 | BcStatus bc_num_extend(BcNum *n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 137 | |
| 138 | BcStatus status; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 139 | BcDigit *ptr; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 140 | size_t len; |
| 141 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 142 | if (!places) return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 143 | |
| 144 | len = n->len + places; |
| 145 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 146 | if (n->cap < len && (status = bc_num_expand(n, len))) return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 147 | |
| 148 | ptr = n->num + places; |
| 149 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 150 | memmove(ptr, n->num, sizeof(BcDigit) * n->len); |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 151 | memset(n->num, 0, sizeof(BcDigit) * places); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 152 | |
| 153 | n->len += places; |
Gavin Howard | cde142c | 2018-02-28 17:33:44 -0700 | [diff] [blame] | 154 | n->rdx += places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 155 | |
| 156 | return BC_STATUS_SUCCESS; |
| 157 | } |
| 158 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 159 | BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) { |
| 160 | |
| 161 | BcStatus status; |
| 162 | BcNum one; |
| 163 | |
| 164 | if ((status = bc_num_init(&one, BC_NUM_DEF_SIZE))) return status; |
| 165 | |
| 166 | bc_num_one(&one); |
| 167 | status = bc_num_div(&one, a, b, scale); |
| 168 | bc_num_free(&one); |
| 169 | |
| 170 | return status; |
| 171 | } |
| 172 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 173 | BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 174 | |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 175 | BcDigit *ptr, *ptr_a, *ptr_b, *ptr_c; |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 176 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 177 | BcDigit carry; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 178 | |
| 179 | (void) scale; |
| 180 | |
Gavin Howard | f6964a1 | 2018-03-14 10:52:31 -0600 | [diff] [blame] | 181 | if (!a->len) return bc_num_copy(c, b); |
| 182 | else if (!b->len) return bc_num_copy(c, a); |
| 183 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 184 | c->neg = a->neg; |
| 185 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 186 | memset(c->num, 0, c->cap * sizeof(BcDigit)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 187 | |
| 188 | c->rdx = BC_MAX(a->rdx, b->rdx); |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 189 | min_rdx = BC_MIN(a->rdx, b->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 190 | |
| 191 | c->len = 0; |
| 192 | |
| 193 | if (a->rdx > b->rdx) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 194 | diff = a->rdx - b->rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 195 | ptr = a->num; |
| 196 | ptr_a = a->num + diff; |
| 197 | ptr_b = b->num; |
| 198 | } |
| 199 | else { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 200 | diff = b->rdx - a->rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 201 | ptr = b->num; |
| 202 | ptr_a = a->num; |
| 203 | ptr_b = b->num + diff; |
| 204 | } |
| 205 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 206 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 207 | |
| 208 | ptr_c += diff; |
| 209 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 210 | a_int = a->len - a->rdx; |
| 211 | b_int = b->len - b->rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 212 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 213 | if (a_int > b_int) { |
| 214 | min_int = b_int; |
| 215 | max = a_int; |
| 216 | ptr = ptr_a; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 217 | } |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 218 | else { |
| 219 | min_int = a_int; |
| 220 | max = b_int; |
| 221 | ptr = ptr_b; |
| 222 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 223 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 224 | for (carry = 0, i = 0; i < min_rdx + min_int; ++i, ++c->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 225 | ptr_c[i] = ptr_a[i] + ptr_b[i] + carry; |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 226 | carry = ptr_c[i] / 10; |
| 227 | ptr_c[i] %= 10; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 230 | for (; i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 231 | ptr_c[i] += ptr[i] + carry; |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 232 | carry = ptr_c[i] / 10; |
| 233 | ptr_c[i] %= 10; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 236 | if (carry) c->num[c->len++] = carry; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 237 | |
| 238 | return BC_STATUS_SUCCESS; |
| 239 | } |
| 240 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 241 | BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 242 | |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 243 | BcStatus status; |
| 244 | int cmp; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 245 | BcNum *minuend, *subtrahend; |
Gavin Howard | 2de9399 | 2018-03-20 15:58:13 -0600 | [diff] [blame] | 246 | size_t start; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 247 | bool aneg, bneg, neg; |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 248 | |
| 249 | // Because this function doesn't need to use scale (per the bc spec), |
| 250 | // I am hijacking it to tell this function whether it is doing an add |
| 251 | // or a subtract. |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 252 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 253 | if (!a->len) { |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 254 | status = bc_num_copy(c, b); |
| 255 | c->neg = !b->neg; |
| 256 | return status; |
| 257 | } |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 258 | else if (!b->len) return bc_num_copy(c, a); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 259 | |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 260 | aneg = a->neg; |
| 261 | bneg = b->neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 262 | a->neg = b->neg = false; |
| 263 | |
Gavin Howard | 2a064fd | 2018-03-20 15:56:12 -0600 | [diff] [blame] | 264 | cmp = bc_num_cmp(a, b, NULL); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 265 | |
| 266 | a->neg = aneg; |
| 267 | b->neg = bneg; |
| 268 | |
| 269 | if (!cmp) { |
| 270 | bc_num_zero(c); |
| 271 | return BC_STATUS_SUCCESS; |
| 272 | } |
| 273 | else if (cmp > 0) { |
Gavin Howard | 8694e13 | 2018-03-14 13:43:28 -0600 | [diff] [blame] | 274 | neg = sub && a->neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 275 | minuend = a; |
| 276 | subtrahend = b; |
| 277 | } |
| 278 | else { |
Gavin Howard | 8694e13 | 2018-03-14 13:43:28 -0600 | [diff] [blame] | 279 | neg = sub && !b->neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 280 | minuend = b; |
| 281 | subtrahend = a; |
| 282 | } |
| 283 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 284 | if ((status = bc_num_copy(c, minuend))) return status; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 285 | |
| 286 | c->neg = neg; |
| 287 | |
| 288 | if (c->rdx < subtrahend->rdx) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 289 | if ((status = bc_num_extend(c, subtrahend->rdx - c->rdx))) return status; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 290 | start = 0; |
| 291 | } |
Gavin Howard | d9f5c8e | 2018-03-10 14:08:13 -0700 | [diff] [blame] | 292 | else start = c->rdx - subtrahend->rdx; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 293 | |
Gavin Howard | aa53f7c | 2018-03-23 13:22:36 -0600 | [diff] [blame] | 294 | bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 295 | |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 296 | while (c->len > c->rdx && !c->num[c->len - 1]) --c->len; |
| 297 | |
| 298 | return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 301 | BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 302 | |
| 303 | BcStatus status; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 304 | BcDigit carry; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 305 | size_t i, j, len; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 306 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 307 | if (!a->len || !b->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 308 | bc_num_zero(c); |
| 309 | return BC_STATUS_SUCCESS; |
| 310 | } |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 311 | else if (BC_NUM_ONE(a)) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 312 | status = bc_num_copy(c, b); |
| 313 | if (a->neg) c->neg = !c->neg; |
| 314 | return status; |
| 315 | } |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 316 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 317 | status = bc_num_copy(c, a); |
| 318 | if (b->neg) c->neg = !c->neg; |
| 319 | return status; |
| 320 | } |
| 321 | |
| 322 | scale = BC_MAX(scale, a->rdx); |
| 323 | scale = BC_MAX(scale, b->rdx); |
| 324 | c->rdx = a->rdx + b->rdx; |
| 325 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 326 | memset(c->num, 0, sizeof(BcDigit) * c->cap); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 327 | c->len = carry = len = 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 328 | |
| 329 | for (i = 0; i < b->len; ++i) { |
| 330 | |
| 331 | for (j = 0; j < a->len; ++j) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 332 | c->num[i + j] += a->num[j] * b->num[i] + carry; |
Gavin Howard | 5f806ee | 2018-03-03 23:30:31 -0700 | [diff] [blame] | 333 | carry = c->num[i + j] / 10; |
| 334 | c->num[i + j] %= 10; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | if (carry) { |
| 338 | c->num[i + j] += carry; |
| 339 | carry = 0; |
| 340 | len = BC_MAX(len, i + j + 1); |
| 341 | } |
| 342 | else len = BC_MAX(len, i + j); |
| 343 | } |
| 344 | |
| 345 | c->len = BC_MAX(len, c->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 346 | c->neg = !a->neg != !b->neg; |
| 347 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 348 | if (scale < c->rdx) status = bc_num_truncate(c, c->rdx - scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 349 | else status = BC_STATUS_SUCCESS; |
| 350 | |
Gavin Howard | d2e917d | 2018-02-28 16:03:10 -0700 | [diff] [blame] | 351 | while (c->len > c->rdx && !c->num[c->len - 1]) --c->len; |
| 352 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 353 | return status; |
| 354 | } |
| 355 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 356 | BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 357 | |
| 358 | BcStatus status; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 359 | BcDigit *ptr, *bptr, q; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 360 | size_t len, end, i; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 361 | BcNum copy; |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 362 | bool zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 363 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 364 | if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 365 | else if (!a->len) { |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 366 | bc_num_zero(c); |
| 367 | return BC_STATUS_SUCCESS; |
| 368 | } |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 369 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | ae6e7d5 | 2018-03-14 10:13:43 -0600 | [diff] [blame] | 370 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 371 | if ((status = bc_num_copy(c, a))) return status; |
Gavin Howard | ae6e7d5 | 2018-03-14 10:13:43 -0600 | [diff] [blame] | 372 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 373 | if (b->neg) c->neg = !c->neg; |
Gavin Howard | ae6e7d5 | 2018-03-14 10:13:43 -0600 | [diff] [blame] | 374 | |
| 375 | if (c->rdx < scale) status = bc_num_extend(c, scale - c->rdx); |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 376 | else status = bc_num_truncate(c, c->rdx - scale); |
Gavin Howard | ae6e7d5 | 2018-03-14 10:13:43 -0600 | [diff] [blame] | 377 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 378 | return status; |
| 379 | } |
| 380 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 381 | if ((status = bc_num_init(©, a->len + b->rdx + scale + 1))) return status; |
| 382 | if ((status = bc_num_copy(©, a))) goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 383 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 384 | len = b->len; |
| 385 | |
| 386 | if (len > copy.len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 387 | if ((status = bc_num_expand(©, len + 2))) goto err; |
| 388 | if ((status = bc_num_extend(©, len - copy.len))) goto err; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 391 | if (b->rdx > copy.rdx && (status = bc_num_extend(©, b->rdx - copy.rdx))) |
| 392 | goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 393 | |
| 394 | copy.rdx -= b->rdx; |
| 395 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 396 | if (scale > copy.rdx && (status = bc_num_extend(©, scale - copy.rdx))) |
| 397 | goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 398 | |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 399 | if (b->rdx == b->len) { |
| 400 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 401 | bool zero = true; |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 402 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 403 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 404 | |
| 405 | if (i == len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 406 | |
| 407 | len -= i - 1; |
| 408 | } |
| 409 | |
| 410 | if (copy.cap == copy.len) { |
| 411 | status = bc_num_expand(©, copy.len + 1); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 412 | if (status) goto err; |
| 413 | } |
| 414 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 415 | // We want an extra zero in front to make things simpler. |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 416 | copy.num[copy.len++] = 0; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 417 | end = copy.len - len; |
| 418 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 419 | if ((status = bc_num_expand(c, copy.len))) goto err; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 420 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 421 | bc_num_zero(c); |
| 422 | c->rdx = copy.rdx; |
| 423 | c->len = copy.len; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 424 | bptr = b->num; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 425 | |
| 426 | for (i = end - 1; i < end; --i) { |
| 427 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 428 | ptr = copy.num + i; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 429 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 430 | for (q = 0; ptr[len] || bc_num_compare(ptr, bptr, len, NULL) >= 0; ++q) |
Gavin Howard | aa53f7c | 2018-03-23 13:22:36 -0600 | [diff] [blame] | 431 | bc_num_subArrays(ptr, bptr, len); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 432 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 433 | c->num[i] = q; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 436 | c->neg = !a->neg != !b->neg; |
| 437 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 438 | while (c->len > c->rdx && !c->num[c->len - 1]) --c->len; |
| 439 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 440 | if (c->rdx > scale) status = bc_num_truncate(c, c->rdx - scale); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 441 | |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 442 | for (i = 0, zero = true; zero && i < c->len; ++i) zero = !c->num[i]; |
| 443 | if (zero) bc_num_zero(c); |
| 444 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 445 | err: |
| 446 | |
| 447 | bc_num_free(©); |
| 448 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 449 | return status; |
| 450 | } |
| 451 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 452 | BcStatus bc_num_alg_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 453 | |
| 454 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 455 | BcNum c1, c2; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 456 | size_t len; |
| 457 | |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 458 | if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 459 | |
| 460 | if (!a->len) { |
| 461 | bc_num_zero(c); |
| 462 | return BC_STATUS_SUCCESS; |
| 463 | } |
| 464 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 465 | len = a->len + b->len + scale; |
| 466 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 467 | if ((status = bc_num_init(&c1, len))) return status; |
| 468 | if ((status = bc_num_init(&c2, len))) goto c2_err; |
| 469 | if ((status = bc_num_div(a, b, &c1, scale))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 470 | |
| 471 | c->rdx = BC_MAX(scale + b->rdx, a->rdx); |
| 472 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 473 | if ((status = bc_num_mul(&c1, b, &c2, scale))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 474 | |
| 475 | status = bc_num_sub(a, &c2, c, scale); |
| 476 | |
| 477 | err: |
| 478 | |
| 479 | bc_num_free(&c2); |
| 480 | |
| 481 | c2_err: |
| 482 | |
| 483 | bc_num_free(&c1); |
| 484 | |
| 485 | return status; |
| 486 | } |
| 487 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 488 | BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 489 | |
| 490 | BcStatus status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 491 | BcNum copy; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 492 | unsigned long pow; |
| 493 | size_t i, powrdx, resrdx; |
| 494 | bool neg, zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 495 | |
Gavin Howard | 2395858 | 2018-03-03 09:30:20 -0700 | [diff] [blame] | 496 | if (b->rdx) return BC_STATUS_MATH_NON_INTEGER; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 497 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 498 | if (!b->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 499 | bc_num_one(c); |
| 500 | return BC_STATUS_SUCCESS; |
| 501 | } |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 502 | else if (!a->len) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 503 | bc_num_zero(c); |
| 504 | return BC_STATUS_SUCCESS; |
| 505 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 506 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 507 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 508 | if (!b->neg) status = bc_num_copy(c, a); |
| 509 | else status = bc_num_inv(a, c, scale); |
Gavin Howard | 5fc40e7 | 2018-03-05 12:11:09 -0700 | [diff] [blame] | 510 | |
| 511 | return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 512 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 513 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 514 | neg = b->neg; |
| 515 | b->neg = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 516 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 517 | if ((status = bc_num_ulong(b, &pow))) return status; |
| 518 | if ((status = bc_num_init(©, a->len))) return status; |
| 519 | if ((status = bc_num_copy(©, a))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 520 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 521 | if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx)); |
| 522 | |
| 523 | b->neg = neg; |
| 524 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 525 | for (powrdx = a->rdx; !(pow & 1); pow >>= 1) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 526 | powrdx <<= 1; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 527 | if ((status = bc_num_mul(©, ©, ©, powrdx))) goto err; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 528 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 529 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 530 | if ((status = bc_num_copy(c, ©))) goto err; |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 531 | |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 532 | resrdx = powrdx; |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 533 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 534 | for (pow >>= 1; pow != 0; pow >>= 1) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 535 | |
| 536 | powrdx <<= 1; |
| 537 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 538 | if ((status = bc_num_mul(©, ©, ©, powrdx))) goto err; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 539 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 540 | if (pow & 1) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 541 | resrdx += powrdx; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 542 | if ((status = bc_num_mul(c, ©, c, resrdx))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 543 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 546 | if (neg && (status = bc_num_inv(c, c, scale))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 547 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 548 | if (c->rdx > scale && (status = bc_num_truncate(c, c->rdx - scale))) goto err; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 549 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 550 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 551 | if (zero) bc_num_zero(c); |
| 552 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 553 | err: |
| 554 | |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 555 | bc_num_free(©); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 556 | |
| 557 | return status; |
| 558 | } |
| 559 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 560 | BcStatus bc_num_sqrt_newton(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 561 | |
| 562 | BcStatus status; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 563 | BcNum num1, num2, two, f, fprime, *x0, *x1, *temp; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 564 | size_t pow, len, digits, resrdx; |
Gavin Howard | fa48573 | 2018-03-05 19:51:03 -0700 | [diff] [blame] | 565 | int cmp; |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 566 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 567 | if (!a->len) { |
Gavin Howard | 62e52ed | 2018-03-05 14:48:26 -0700 | [diff] [blame] | 568 | bc_num_zero(b); |
| 569 | return BC_STATUS_SUCCESS; |
| 570 | } |
Gavin Howard | 98e44ce | 2018-03-20 18:11:52 -0600 | [diff] [blame] | 571 | else if (a->neg) return BC_STATUS_MATH_NEG_SQRT; |
| 572 | else if (BC_NUM_ONE(a)) { |
Gavin Howard | 62e52ed | 2018-03-05 14:48:26 -0700 | [diff] [blame] | 573 | bc_num_one(b); |
| 574 | return bc_num_extend(b, scale); |
| 575 | } |
Gavin Howard | 62e52ed | 2018-03-05 14:48:26 -0700 | [diff] [blame] | 576 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 577 | memset(b->num, 0, b->cap * sizeof(BcDigit)); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 578 | len = a->len; |
Gavin Howard | 62e52ed | 2018-03-05 14:48:26 -0700 | [diff] [blame] | 579 | |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 580 | scale = BC_MAX(scale, a->rdx) + 1; |
| 581 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 582 | if ((status = bc_num_init(&num1, len))) return status; |
| 583 | if ((status = bc_num_init(&num2, num1.len))) goto num2_err; |
| 584 | if ((status = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err; |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 585 | |
| 586 | bc_num_one(&two); |
| 587 | two.num[0] = 2; |
| 588 | |
| 589 | len += scale; |
| 590 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 591 | if ((status = bc_num_init(&f, len))) goto f_err; |
| 592 | if ((status = bc_num_init(&fprime, len + scale))) goto fprime_err; |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 593 | |
| 594 | x0 = &num1; |
| 595 | x1 = &num2; |
| 596 | |
| 597 | bc_num_one(x0); |
| 598 | |
| 599 | pow = a->len - a->rdx; |
| 600 | |
Gavin Howard | fa48573 | 2018-03-05 19:51:03 -0700 | [diff] [blame] | 601 | if (pow) { |
| 602 | |
| 603 | if (pow & 1) { |
| 604 | x0->num[0] = 2; |
| 605 | pow -= 1; |
| 606 | } |
| 607 | else { |
| 608 | x0->num[0] = 6; |
| 609 | pow -= 2; |
| 610 | } |
| 611 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 612 | if ((status = bc_num_extend(x0, pow))) goto err; |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Gavin Howard | fa48573 | 2018-03-05 19:51:03 -0700 | [diff] [blame] | 615 | cmp = 1; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 616 | x0->rdx = digits = 0; |
Gavin Howard | fa48573 | 2018-03-05 19:51:03 -0700 | [diff] [blame] | 617 | resrdx = scale + 1; |
| 618 | len = (x0->len - x0->rdx) + resrdx; |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 619 | |
Gavin Howard | fa48573 | 2018-03-05 19:51:03 -0700 | [diff] [blame] | 620 | while (cmp && digits <= len) { |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 621 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 622 | if ((status = bc_num_mul(x0, x0, &f, resrdx))) goto err; |
| 623 | if ((status = bc_num_sub(&f, a, &f, resrdx))) goto err; |
| 624 | if ((status = bc_num_mul(x0, &two, &fprime, resrdx))) goto err; |
| 625 | if ((status = bc_num_div(&f, &fprime, &f, resrdx))) goto err; |
| 626 | if ((status = bc_num_sub(x0, &f, x1, resrdx))) goto err; |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 627 | |
Gavin Howard | 2a064fd | 2018-03-20 15:56:12 -0600 | [diff] [blame] | 628 | cmp = bc_num_cmp(x1, x0, &digits); |
Gavin Howard | fa48573 | 2018-03-05 19:51:03 -0700 | [diff] [blame] | 629 | |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 630 | temp = x0; |
| 631 | x0 = x1; |
| 632 | x1 = temp; |
| 633 | } |
| 634 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 635 | if ((status = bc_num_copy(b, x0))) goto err; |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 636 | |
Gavin Howard | 62e52ed | 2018-03-05 14:48:26 -0700 | [diff] [blame] | 637 | --scale; |
| 638 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 639 | if (b->rdx > scale) status = bc_num_truncate(b, b->rdx - scale); |
Gavin Howard | 62e52ed | 2018-03-05 14:48:26 -0700 | [diff] [blame] | 640 | else if (b->rdx < scale) status = bc_num_extend(b, scale - b->rdx); |
Gavin Howard | 62f8c91 | 2018-03-02 10:45:50 -0700 | [diff] [blame] | 641 | |
| 642 | err: |
| 643 | |
| 644 | bc_num_free(&fprime); |
| 645 | |
| 646 | fprime_err: |
| 647 | |
| 648 | bc_num_free(&f); |
| 649 | |
| 650 | f_err: |
| 651 | |
| 652 | bc_num_free(&two); |
| 653 | |
| 654 | two_err: |
| 655 | |
| 656 | bc_num_free(&num2); |
| 657 | |
| 658 | num2_err: |
| 659 | |
| 660 | bc_num_free(&num1); |
| 661 | |
| 662 | return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 665 | BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 666 | BcNumBinaryFunc op, size_t req) |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 667 | { |
| 668 | BcStatus status; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 669 | BcNum num2, *ptr_a, *ptr_b; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 670 | bool init; |
| 671 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 672 | assert(a && b && c && op); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 673 | |
| 674 | init = false; |
| 675 | |
| 676 | if (c == a) { |
Gavin Howard | 1cc23f3 | 2018-02-28 16:09:33 -0700 | [diff] [blame] | 677 | memcpy(&num2, c, sizeof(BcNum)); |
| 678 | ptr_a = &num2; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 679 | init = true; |
| 680 | } |
| 681 | else ptr_a = a; |
| 682 | |
| 683 | if (c == b) { |
| 684 | |
| 685 | if (c == a) { |
| 686 | ptr_b = ptr_a; |
| 687 | } |
| 688 | else { |
Gavin Howard | 1cc23f3 | 2018-02-28 16:09:33 -0700 | [diff] [blame] | 689 | memcpy(&num2, c, sizeof(BcNum)); |
| 690 | ptr_b = &num2; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 691 | init = true; |
| 692 | } |
| 693 | } |
| 694 | else ptr_b = b; |
| 695 | |
| 696 | if (init) status = bc_num_init(c, req); |
| 697 | else status = bc_num_expand(c, req); |
| 698 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 699 | if (status) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 700 | |
| 701 | status = op(ptr_a, ptr_b, c, scale); |
| 702 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 703 | err: |
| 704 | |
Gavin Howard | 1cc23f3 | 2018-02-28 16:09:33 -0700 | [diff] [blame] | 705 | if (c == a || c == b) bc_num_free(&num2); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 706 | |
| 707 | return status; |
| 708 | } |
| 709 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 710 | BcStatus bc_num_unary(BcNum *a, BcNum *b, size_t scale, |
| 711 | BcNumUnaryFunc op, size_t req) |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 712 | { |
| 713 | BcStatus status; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 714 | BcNum a2, *ptr_a; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 715 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 716 | assert(a && b && op); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 717 | |
| 718 | if (b == a) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 719 | memcpy(&a2, b, sizeof(BcNum)); |
| 720 | ptr_a = &a2; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 721 | status = bc_num_init(b, req); |
| 722 | } |
| 723 | else { |
| 724 | ptr_a = a; |
| 725 | status = bc_num_expand(b, req); |
| 726 | } |
| 727 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 728 | if (status) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 729 | |
| 730 | status = op(ptr_a, b, scale); |
| 731 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 732 | err: |
| 733 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 734 | if (b == a) bc_num_free(&a2); |
| 735 | |
| 736 | return status; |
| 737 | } |
| 738 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 739 | bool bc_num_strValid(const char *val, size_t base) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 740 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 741 | size_t len, i; |
| 742 | BcDigit c, b; |
| 743 | bool small, radix; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 744 | |
| 745 | radix = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 746 | len = strlen(val); |
| 747 | |
| 748 | if (!len) return true; |
| 749 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 750 | small = base <= 10; |
| 751 | b = small ? base + '0' : base - 9 + 'A'; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 752 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 753 | for (i = 0; i < len; ++i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 754 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 755 | c = val[i]; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 756 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 757 | if (c == '.') { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 758 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 759 | if (radix) return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 760 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 761 | radix = true; |
| 762 | continue; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 763 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 764 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 765 | if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b))) |
| 766 | return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | return true; |
| 770 | } |
| 771 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 772 | BcStatus bc_num_parseDecimal(BcNum *n, const char *val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 773 | |
| 774 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 775 | size_t len, i; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 776 | const char *ptr; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 777 | bool zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 778 | |
| 779 | for (i = 0; val[i] == '0'; ++i); |
| 780 | |
| 781 | val += i; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 782 | len = strlen(val); |
| 783 | |
| 784 | bc_num_zero(n); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 785 | zero = true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 786 | |
| 787 | if (len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 788 | for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.'; |
| 789 | if ((status = bc_num_expand(n, len))) return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 790 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 791 | |
| 792 | if (zero) { |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 793 | memset(n->num, 0, sizeof(BcDigit) * n->cap); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 794 | n->neg = false; |
| 795 | return BC_STATUS_SUCCESS; |
| 796 | } |
| 797 | |
| 798 | ptr = strchr(val, '.'); |
| 799 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 800 | // Explicitly test for NULL here to produce either a 0 or 1. |
| 801 | n->rdx = (ptr != NULL) * ((val + len) - (ptr + 1)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 802 | |
Gavin Howard | 595886d | 2018-03-28 19:57:37 -0600 | [diff] [blame] | 803 | for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 804 | n->num[n->len] = val[i] - '0'; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 805 | |
| 806 | return BC_STATUS_SUCCESS; |
| 807 | } |
| 808 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 809 | BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 810 | |
| 811 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 812 | BcNum temp, mult, result; |
| 813 | size_t i, len, digits; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 814 | BcDigit c; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 815 | bool zero; |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 816 | unsigned long v; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 817 | |
| 818 | len = strlen(val); |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 819 | bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 820 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 821 | for (zero = true, i = 0; zero && i < len; ++i) |
| 822 | zero = (val[i] == '.' || val[i] == '0'); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 823 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 824 | if (zero) return BC_STATUS_SUCCESS; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 825 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 826 | if ((status = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return status; |
| 827 | if ((status = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 828 | |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 829 | for (i = 0; i < len && (c = val[i]) != '.'; ++i) { |
| 830 | |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 831 | v = c <= '9' ? c - '0' : c - 'A' + 10; |
Gavin Howard | fcb6ebb | 2018-03-09 10:41:06 -0700 | [diff] [blame] | 832 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 833 | if ((status = bc_num_mul(n, base, &mult, 0))) goto int_err; |
| 834 | if ((status = bc_num_ulong2num(&temp, v))) goto int_err; |
| 835 | if ((status = bc_num_add(&mult, &temp, n, 0))) goto int_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 838 | if (i == len && !(c = val[i])) goto int_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 839 | |
| 840 | assert(c == '.'); |
| 841 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 842 | if ((status = bc_num_init(&result, base->len))) goto int_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 843 | |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 844 | bc_num_zero(&result); |
| 845 | bc_num_one(&mult); |
| 846 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 847 | for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 848 | |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 849 | v = c <= '9' ? c - '0' : c - 'A' + 10; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 850 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 851 | if ((status = bc_num_mul(&result, base, &result, 0))) goto err; |
| 852 | if ((status = bc_num_ulong2num(&temp, v))) goto err; |
| 853 | if ((status = bc_num_add(&result, &temp, &result, 0))) goto err; |
| 854 | if ((status = bc_num_mul(&mult, base, &mult, 0))) goto err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 857 | if ((status = bc_num_div(&result, &mult, &result, digits))) goto err; |
| 858 | if ((status = bc_num_add(n, &result, n, digits))) goto err; |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 859 | |
| 860 | if (n->len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 861 | if (n->rdx < digits && n->len) status = bc_num_extend(n, digits - n->rdx); |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 862 | } |
| 863 | else bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 864 | |
| 865 | err: |
| 866 | |
| 867 | bc_num_free(&result); |
| 868 | |
| 869 | int_err: |
| 870 | |
| 871 | bc_num_free(&mult); |
| 872 | |
| 873 | mult_err: |
| 874 | |
| 875 | bc_num_free(&temp); |
| 876 | |
| 877 | return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 880 | BcStatus bc_num_printDigits(unsigned long num, size_t width, |
| 881 | bool radix, size_t *nchars) |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 882 | { |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 883 | size_t exp, pow, div; |
| 884 | |
| 885 | if (*nchars == BC_NUM_PRINT_WIDTH - 1) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 886 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 887 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 888 | *nchars = 0; |
| 889 | } |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 890 | |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 891 | if (*nchars || radix) { |
| 892 | if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR; |
| 893 | ++(*nchars); |
| 894 | } |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 895 | |
| 896 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10); |
| 897 | |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 898 | for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) { |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 899 | |
| 900 | if (*nchars == BC_NUM_PRINT_WIDTH - 1) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 901 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 902 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 903 | *nchars = 0; |
| 904 | } |
| 905 | |
| 906 | div = num / pow; |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 907 | num -= div * pow; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 908 | |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 909 | if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 912 | return BC_STATUS_SUCCESS; |
| 913 | } |
Gavin Howard | fe679f0 | 2018-02-14 15:50:09 -0700 | [diff] [blame] | 914 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 915 | BcStatus bc_num_printHex(unsigned long num, size_t width, |
| 916 | bool radix, size_t *nchars) |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 917 | { |
Gavin Howard | 881645c | 2018-03-14 01:44:20 -0600 | [diff] [blame] | 918 | width += !!radix; |
| 919 | if (*nchars + width >= BC_NUM_PRINT_WIDTH) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 920 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 921 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 922 | *nchars = 0; |
| 923 | } |
| 924 | |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 925 | if (radix && putchar('.') == EOF) return BC_STATUS_IO_ERR; |
| 926 | if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 927 | |
| 928 | *nchars = *nchars + width; |
| 929 | |
| 930 | return BC_STATUS_SUCCESS; |
| 931 | } |
| 932 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 933 | BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars) { |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 934 | |
| 935 | BcStatus status; |
| 936 | size_t i; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 937 | |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 938 | if (n->neg) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 939 | if (putchar('-') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 940 | ++(*nchars); |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | status = BC_STATUS_SUCCESS; |
| 944 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 945 | for (i = n->len - 1; !status && i < n->len; --i) |
| 946 | status = bc_num_printHex(n->num[i], 1, i == n->rdx - 1, nchars); |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 947 | |
| 948 | return status; |
| 949 | } |
| 950 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 951 | BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t, size_t *nchars) |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 952 | { |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 953 | BcStatus status; |
| 954 | BcVec stack; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 955 | BcNum intp, fracp, digit, frac_len; |
| 956 | size_t width, i; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 957 | BcNumDigitFunc print; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 958 | unsigned long dig, *ptr; |
Gavin Howard | 53ed5bb | 2018-03-14 01:15:59 -0600 | [diff] [blame] | 959 | bool neg, radix; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 960 | |
Gavin Howard | 53ed5bb | 2018-03-14 01:15:59 -0600 | [diff] [blame] | 961 | neg = n->neg; |
| 962 | n->neg = false; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 963 | |
Gavin Howard | 53ed5bb | 2018-03-14 01:15:59 -0600 | [diff] [blame] | 964 | if (neg) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 965 | if (putchar('-') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 966 | ++nchars; |
| 967 | } |
| 968 | |
Gavin Howard | cb3ce02 | 2018-03-21 09:13:24 -0600 | [diff] [blame] | 969 | if (base_t <= BC_NUM_MAX_INPUT_BASE) { |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 970 | width = 1; |
| 971 | print = bc_num_printHex; |
| 972 | } |
| 973 | else { |
| 974 | width = (size_t) floor(log10((double) (base_t - 1)) + 1.0); |
| 975 | print = bc_num_printDigits; |
| 976 | } |
| 977 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 978 | if ((status = bc_vec_init(&stack, sizeof(long), NULL))) return status; |
| 979 | if ((status = bc_num_init(&intp, n->len))) goto int_err; |
| 980 | if ((status = bc_num_init(&fracp, n->rdx))) goto frac_err; |
| 981 | if ((status = bc_num_init(&digit, width))) goto digit_err; |
| 982 | if ((status = bc_num_copy(&intp, n))) goto frac_len_err; |
| 983 | if ((status = bc_num_truncate(&intp, intp.rdx))) goto frac_len_err; |
| 984 | if ((status = bc_num_sub(n, &intp, &fracp, 0))) goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 985 | |
Gavin Howard | 53ed5bb | 2018-03-14 01:15:59 -0600 | [diff] [blame] | 986 | while (intp.len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 987 | if ((status = bc_num_mod(&intp, base, &digit, 0))) goto frac_len_err; |
| 988 | if ((status = bc_num_ulong(&digit, &dig))) goto frac_len_err; |
| 989 | if ((status = bc_vec_push(&stack, &dig))) goto frac_len_err; |
| 990 | if ((status = bc_num_div(&intp, base, &intp, 0))) goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | for (i = 0; i < stack.len; ++i) { |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 994 | ptr = bc_vec_item_rev(&stack, i); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 995 | assert(ptr); |
| 996 | if ((status = print(*ptr, width, false, nchars))) goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | if (!n->rdx) goto frac_len_err; |
| 1000 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1001 | if ((status = bc_num_init(&frac_len, n->len - n->rdx))) goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1002 | |
| 1003 | bc_num_one(&frac_len); |
| 1004 | |
Gavin Howard | 1819ecc | 2018-03-14 01:15:41 -0600 | [diff] [blame] | 1005 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1006 | if ((status = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err; |
| 1007 | if ((status = bc_num_ulong(&fracp, &dig))) goto err; |
| 1008 | if ((status = bc_num_ulong2num(&intp, dig))) goto err; |
| 1009 | if ((status = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err; |
| 1010 | if ((status = print(dig, width, radix, nchars))) goto err; |
| 1011 | if ((status = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | err: |
| 1015 | |
Gavin Howard | 53ed5bb | 2018-03-14 01:15:59 -0600 | [diff] [blame] | 1016 | n->neg = neg; |
| 1017 | |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1018 | bc_num_free(&frac_len); |
| 1019 | |
| 1020 | frac_len_err: |
| 1021 | |
| 1022 | bc_num_free(&digit); |
| 1023 | |
| 1024 | digit_err: |
| 1025 | |
| 1026 | bc_num_free(&fracp); |
| 1027 | |
| 1028 | frac_err: |
| 1029 | |
| 1030 | bc_num_free(&intp); |
| 1031 | |
| 1032 | int_err: |
| 1033 | |
| 1034 | bc_vec_free(&stack); |
| 1035 | |
| 1036 | return status; |
| 1037 | } |
| 1038 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1039 | BcStatus bc_num_init(BcNum *n, size_t request) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1040 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 1041 | assert(n); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1042 | |
Gavin Howard | 0b465d0 | 2018-02-15 11:41:16 -0700 | [diff] [blame] | 1043 | memset(n, 0, sizeof(BcNum)); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1044 | |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1045 | request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE; |
| 1046 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1047 | if (!(n->num = malloc(request))) return BC_STATUS_MALLOC_FAIL; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1048 | |
Gavin Howard | 6f6dc94 | 2018-02-15 16:58:50 -0700 | [diff] [blame] | 1049 | n->cap = request; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1050 | |
| 1051 | return BC_STATUS_SUCCESS; |
| 1052 | } |
| 1053 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1054 | BcStatus bc_num_expand(BcNum *n, size_t request) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1055 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1056 | BcDigit *temp; |
| 1057 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 1058 | assert(n && request); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1059 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1060 | if (request <= n->cap) return BC_STATUS_SUCCESS; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1061 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1062 | if (!(temp = realloc(n->num, request))) return BC_STATUS_MALLOC_FAIL; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1063 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1064 | memset(temp + n->cap, 0, sizeof(char) * (request - n->cap)); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1065 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1066 | n->num = temp; |
| 1067 | n->cap = request; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1068 | |
| 1069 | return BC_STATUS_SUCCESS; |
| 1070 | } |
| 1071 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 1072 | void bc_num_free(void *num) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1073 | BcNum *n = (BcNum*) num; |
Gavin Howard | d9734e5 | 2018-03-29 15:54:41 -0600 | [diff] [blame^] | 1074 | if (n && n->num) free(n->num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1077 | BcStatus bc_num_copy(BcNum *d, BcNum *s) { |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1078 | |
| 1079 | BcStatus status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1080 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1081 | assert(d && s); |
Gavin Howard | f23448c | 2018-02-27 20:36:24 -0700 | [diff] [blame] | 1082 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1083 | if (d == s) return BC_STATUS_SUCCESS; |
Gavin Howard | 4515e2a | 2018-03-05 10:52:01 -0700 | [diff] [blame] | 1084 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1085 | if ((status = bc_num_expand(d, s->cap))) return status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1086 | |
| 1087 | d->len = s->len; |
| 1088 | d->neg = s->neg; |
Gavin Howard | 8389bb2 | 2018-02-15 17:40:34 -0700 | [diff] [blame] | 1089 | d->rdx = s->rdx; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1090 | |
Gavin Howard | 188b4ac | 2018-03-24 14:49:19 -0600 | [diff] [blame] | 1091 | memcpy(d->num, s->num, sizeof(BcDigit) * d->len); |
| 1092 | memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len)); |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1093 | |
| 1094 | return BC_STATUS_SUCCESS; |
| 1095 | } |
| 1096 | |
Gavin Howard | 5cea43b | 2018-03-02 11:32:11 -0700 | [diff] [blame] | 1097 | BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) { |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1098 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1099 | BcStatus status; |
| 1100 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 1101 | assert(n && val && base && base_t >= BC_NUM_MIN_BASE && |
| 1102 | base_t <= BC_NUM_MAX_INPUT_BASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1103 | |
Gavin Howard | bdb2f92 | 2018-03-15 09:25:18 -0600 | [diff] [blame] | 1104 | if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1105 | |
Gavin Howard | 5cea43b | 2018-03-02 11:32:11 -0700 | [diff] [blame] | 1106 | if (base_t == 10) status = bc_num_parseDecimal(n, val); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1107 | else status = bc_num_parseBase(n, val, base); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1108 | |
| 1109 | return status; |
| 1110 | } |
| 1111 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1112 | BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, |
| 1113 | bool newline, size_t *nchars) |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1114 | { |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1115 | BcStatus status; |
| 1116 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1117 | assert(n && base && nchars && base_t >= BC_NUM_MIN_BASE && |
Gavin Howard | 0dd566d | 2018-03-28 19:29:34 -0600 | [diff] [blame] | 1118 | base_t <= BC_MAX_BASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1119 | |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1120 | if (*nchars >= BC_NUM_PRINT_WIDTH) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 1121 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 1122 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1123 | *nchars = 0; |
| 1124 | } |
| 1125 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 1126 | if (!n->len) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 1127 | if (putchar('0') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1128 | ++(*nchars); |
Gavin Howard | ec8f8f1 | 2018-03-03 09:47:19 -0700 | [diff] [blame] | 1129 | status = BC_STATUS_SUCCESS; |
Gavin Howard | 9576c38 | 2018-03-03 08:11:53 -0700 | [diff] [blame] | 1130 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1131 | else if (base_t == 10) status = bc_num_printDecimal(n, nchars); |
| 1132 | else status = bc_num_printBase(n, base, base_t, nchars); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1133 | |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1134 | if (status) return status; |
| 1135 | |
| 1136 | if (newline) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 1137 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1138 | *nchars = 0; |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1141 | return status; |
| 1142 | } |
| 1143 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1144 | BcStatus bc_num_ulong(BcNum *n, unsigned long *result) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1145 | |
| 1146 | size_t i; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1147 | unsigned long prev, pow; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1148 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 1149 | assert(n && result); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1150 | |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1151 | if (n->neg) return BC_STATUS_MATH_NEGATIVE; |
| 1152 | |
| 1153 | *result = 0; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1154 | pow = 1; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1155 | |
Gavin Howard | 2395858 | 2018-03-03 09:30:20 -0700 | [diff] [blame] | 1156 | for (i = n->rdx; i < n->len; ++i) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1157 | |
| 1158 | prev = *result; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1159 | *result += n->num[i] * pow; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1160 | pow *= 10; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1161 | |
| 1162 | if (*result < prev) return BC_STATUS_MATH_OVERFLOW; |
| 1163 | } |
| 1164 | |
| 1165 | return BC_STATUS_SUCCESS; |
| 1166 | } |
| 1167 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1168 | BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1169 | |
| 1170 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1171 | size_t len, i; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 1172 | BcDigit *ptr; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1173 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 1174 | assert(n); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1175 | |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1176 | bc_num_zero(n); |
| 1177 | |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1178 | if (!val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1179 | memset(n->num, 0, sizeof(char) * n->cap); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1180 | return BC_STATUS_SUCCESS; |
| 1181 | } |
| 1182 | |
Gavin Howard | a83e3dc | 2018-02-15 18:57:08 -0700 | [diff] [blame] | 1183 | len = (size_t) ceil(log10(((double) ULONG_MAX) + 1.0f)); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1184 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1185 | if ((status = bc_num_expand(n, len))) return status; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1186 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1187 | for (ptr = n->num, i = 0; val; ++i, ++n->len) { |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1188 | ptr[i] = (char) (val % 10); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1189 | val /= 10; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1192 | return BC_STATUS_SUCCESS; |
| 1193 | } |
| 1194 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1195 | BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *result, size_t scale) { |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 1196 | (void) scale; |
Gavin Howard | 8694e13 | 2018-03-14 13:43:28 -0600 | [diff] [blame] | 1197 | BcNumBinaryFunc op = (!!a->neg == !!b->neg) ? bc_num_alg_a : bc_num_alg_s; |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 1198 | return bc_num_binary(a, b, result, false, op, a->len + b->len + 1); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1201 | BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *result, size_t scale) { |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 1202 | (void) scale; |
Gavin Howard | 8694e13 | 2018-03-14 13:43:28 -0600 | [diff] [blame] | 1203 | BcNumBinaryFunc op = (!!a->neg == !!b->neg) ? bc_num_alg_s : bc_num_alg_a; |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 1204 | return bc_num_binary(a, b, result, true, op, a->len + b->len + 1); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1207 | BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *result, size_t scale) { |
Gavin Howard | 5744005 | 2018-02-23 11:55:30 -0700 | [diff] [blame] | 1208 | return bc_num_binary(a, b, result, scale, bc_num_alg_m, |
Gavin Howard | dff6d85 | 2018-03-09 11:25:00 -0700 | [diff] [blame] | 1209 | a->len + b->len + scale + 1); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1212 | BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *result, size_t scale) { |
Gavin Howard | 5744005 | 2018-02-23 11:55:30 -0700 | [diff] [blame] | 1213 | return bc_num_binary(a, b, result, scale, bc_num_alg_d, |
Gavin Howard | dff6d85 | 2018-03-09 11:25:00 -0700 | [diff] [blame] | 1214 | a->len + b->len + scale + 1); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1215 | } |
| 1216 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1217 | BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *result, size_t scale) { |
Gavin Howard | 5744005 | 2018-02-23 11:55:30 -0700 | [diff] [blame] | 1218 | return bc_num_binary(a, b, result, scale, bc_num_alg_mod, |
Gavin Howard | dff6d85 | 2018-03-09 11:25:00 -0700 | [diff] [blame] | 1219 | a->len + b->len + scale + 1); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1222 | BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *result, size_t scale) { |
Gavin Howard | b15ce1c | 2018-03-03 12:57:19 -0700 | [diff] [blame] | 1223 | return bc_num_binary(a, b, result, scale, bc_num_alg_p, |
| 1224 | (a->len + 1) * (b->len + 1)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1225 | } |
| 1226 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1227 | BcStatus bc_num_sqrt(BcNum *a, BcNum *result, size_t scale) { |
Gavin Howard | 72701cc | 2018-02-14 15:50:00 -0700 | [diff] [blame] | 1228 | return bc_num_unary(a, result, scale, bc_num_sqrt_newton, |
Gavin Howard | dac9f52 | 2018-03-05 14:09:06 -0700 | [diff] [blame] | 1229 | a->rdx + (a->len - a->rdx) * 2 + 1); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 1232 | void bc_num_zero(BcNum *n) { |
Gavin Howard | 6455db1 | 2018-02-15 17:38:20 -0700 | [diff] [blame] | 1233 | |
| 1234 | if (!n) return; |
| 1235 | |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 1236 | memset(n->num, 0, n->cap * sizeof(char)); |
| 1237 | |
Gavin Howard | 6455db1 | 2018-02-15 17:38:20 -0700 | [diff] [blame] | 1238 | n->neg = false; |
| 1239 | n->len = 0; |
Gavin Howard | 8389bb2 | 2018-02-15 17:40:34 -0700 | [diff] [blame] | 1240 | n->rdx = 0; |
Gavin Howard | 6455db1 | 2018-02-15 17:38:20 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 1243 | void bc_num_one(BcNum *n) { |
Gavin Howard | f507f23 | 2018-02-23 21:10:24 -0700 | [diff] [blame] | 1244 | |
| 1245 | if (!n) return; |
| 1246 | |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1247 | bc_num_zero(n); |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 1248 | |
Gavin Howard | f507f23 | 2018-02-23 21:10:24 -0700 | [diff] [blame] | 1249 | n->len = 1; |
Gavin Howard | f507f23 | 2018-02-23 21:10:24 -0700 | [diff] [blame] | 1250 | n->num[0] = 1; |
| 1251 | } |
| 1252 | |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 1253 | void bc_num_ten(BcNum *n) { |
| 1254 | |
| 1255 | if (!n) return; |
| 1256 | |
| 1257 | bc_num_zero(n); |
| 1258 | |
| 1259 | n->len = 2; |
| 1260 | n->num[0] = 0; |
| 1261 | n->num[1] = 1; |
| 1262 | } |