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