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