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 | * Definitions for the num type. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #ifndef BC_NUM_H |
| 24 | #define BC_NUM_H |
| 25 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 26 | #include <stdbool.h> |
| 27 | #include <stdint.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 31 | #include <bc.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 32 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 33 | typedef signed char BcDigit; |
| 34 | |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 35 | typedef struct BcNum { |
| 36 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 37 | BcDigit *num; |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 38 | size_t rdx; |
| 39 | size_t len; |
| 40 | size_t cap; |
| 41 | bool neg; |
| 42 | |
| 43 | } BcNum; |
| 44 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 45 | #define BC_NUM_MIN_BASE (2) |
| 46 | |
| 47 | #define BC_NUM_MAX_INPUT_BASE (16) |
| 48 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 49 | #define BC_NUM_DEF_SIZE (16) |
| 50 | |
| 51 | #define BC_NUM_FROM_CHAR(c) ((c) -'0') |
| 52 | |
| 53 | #define BC_NUM_TO_CHAR(n) ((n) + '0') |
| 54 | |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 55 | #define BC_NUM_PRINT_WIDTH (69) |
Gavin Howard | 903a1e2 | 2018-02-15 16:32:01 -0700 | [diff] [blame] | 56 | |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 57 | #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 58 | |
Gavin Howard | 9bb13e2 | 2018-02-27 17:01:42 -0700 | [diff] [blame] | 59 | typedef BcStatus (*BcNumUnaryFunc)(BcNum*, BcNum*, size_t); |
| 60 | typedef BcStatus (*BcNumBinaryFunc)(BcNum*, BcNum*, BcNum*, size_t); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 61 | |
Gavin Howard | 1819ecc | 2018-03-14 01:15:41 -0600 | [diff] [blame] | 62 | typedef BcStatus (*BcNumDigitFunc)(unsigned long, size_t, bool, size_t*, FILE*); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 63 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 64 | BcStatus bc_num_init(BcNum *n, size_t request); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 65 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 66 | BcStatus bc_num_expand(BcNum *n, size_t request); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 67 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 68 | void bc_num_free(void *num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 69 | |
Gavin Howard | f23448c | 2018-02-27 20:36:24 -0700 | [diff] [blame] | 70 | BcStatus bc_num_copy(void *dest, void *src); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 71 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 72 | BcStatus bc_num_long(BcNum *n, long *result); |
| 73 | BcStatus bc_num_ulong(BcNum *n, unsigned long *result); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 74 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 75 | BcStatus bc_num_long2num(BcNum *n, long val); |
| 76 | BcStatus bc_num_ulong2num(BcNum *n, unsigned long val); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 77 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 78 | BcStatus bc_num_truncate(BcNum *n); |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 79 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 80 | BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *result, size_t scale); |
| 81 | BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *result, size_t scale); |
| 82 | BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *result, size_t scale); |
| 83 | BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *result, size_t scale); |
| 84 | BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *result, size_t scale); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 85 | |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 86 | // ** Exclude start. ** |
| 87 | BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *result, size_t scale); |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 88 | BcStatus bc_num_sqrt(BcNum *a, BcNum *result, size_t scale); |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 89 | // ** Exclude end. ** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 90 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 91 | int bc_num_compare(BcNum *a, BcNum *b); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 92 | |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 93 | void bc_num_zero(BcNum *n); |
| 94 | void bc_num_one(BcNum *n); |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 95 | void bc_num_ten(BcNum *n); |
Gavin Howard | 43a027f | 2018-02-26 13:27:10 -0700 | [diff] [blame] | 96 | |
Gavin Howard | d75aaec | 2018-03-10 20:33:39 -0700 | [diff] [blame] | 97 | // ** Exclude start. ** |
| 98 | BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t); |
| 99 | |
| 100 | BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline); |
| 101 | BcStatus bc_num_fprint(BcNum *n, BcNum *base, size_t base_t, |
| 102 | bool newline, FILE *f); |
| 103 | // ** Exclude end. ** |
| 104 | |
Gavin Howard | f456d37 | 2018-03-10 20:11:41 -0700 | [diff] [blame] | 105 | extern const char bc_num_hex_digits[]; |
| 106 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 107 | #endif // BC_NUM_H |