1 /* $NetBSD: number.h,v 1.1 2017/04/10 02:28:23 phil Exp $ */ 2 /* number.h: Arbitrary precision numbers header file. */ 3 /* 4 * Copyright (C) 1991, 1992, 1993, 1994, 1997, 2012-2017 Free Software Foundation, Inc. 5 * Copyright (C) 2000, 2004, 2017 Philip A. Nelson. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of Philip A. Nelson nor the name of the Free Software 17 * Foundation may not be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY PHILIP A. NELSON ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL PHILIP A. NELSON OR THE FREE SOFTWARE FOUNDATION BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 #ifndef _NUMBER_H_ 33 #define _NUMBER_H_ 34 35 typedef enum {PLUS, MINUS} sign; 36 37 typedef struct bc_struct *bc_num; 38 39 typedef struct bc_struct 40 { 41 sign n_sign; 42 int n_len; /* The number of digits before the decimal point. */ 43 int n_scale; /* The number of digits after the decimal point. */ 44 int n_refs; /* The number of pointers to this number. */ 45 bc_num n_next; /* Linked list for available list. */ 46 char *n_ptr; /* The pointer to the actual storage. 47 If NULL, n_value points to the inside of 48 another number (bc_multiply...) and should 49 not be "freed." */ 50 char *n_value; /* The number. Not zero char terminated. 51 May not point to the same place as n_ptr as 52 in the case of leading zeros generated. */ 53 } bc_struct; 54 55 56 /* The base used in storing the numbers in n_value above. 57 Currently this MUST be 10. */ 58 59 #define BASE 10 60 61 /* Some useful macros and constants. */ 62 63 #define CH_VAL(c) (c - '0') 64 #define BCD_CHAR(d) (d + '0') 65 66 #ifdef MIN 67 #undef MIN 68 #undef MAX 69 #endif 70 #define MAX(a,b) ((a)>(b)?(a):(b)) 71 #define MIN(a,b) ((a)>(b)?(b):(a)) 72 #define ODD(a) ((a)&1) 73 74 #ifndef TRUE 75 #define TRUE 1 76 #define FALSE 0 77 #endif 78 79 #ifndef LONG_MAX 80 #define LONG_MAX 0x7fffffff 81 #endif 82 83 84 /* Global numbers. */ 85 extern bc_num _zero_; 86 extern bc_num _one_; 87 extern bc_num _two_; 88 89 90 /* Function Prototypes */ 91 92 void bc_init_numbers (void); 93 94 bc_num bc_new_num (int length, int scale); 95 96 void bc_free_num (bc_num *num); 97 98 bc_num bc_copy_num (bc_num num); 99 100 void bc_init_num (bc_num *num); 101 102 void bc_str2num (bc_num *num, char *str, int scale); 103 104 char *bc_num2str (bc_num num); 105 106 void bc_int2num (bc_num *num, int val); 107 108 long bc_num2long (bc_num num); 109 110 int bc_compare (bc_num n1, bc_num n2); 111 112 char bc_is_zero (bc_num num); 113 114 char bc_is_near_zero (bc_num num, int scale); 115 116 char bc_is_neg (bc_num num); 117 118 void bc_add (bc_num n1, bc_num n2, bc_num *result, int scale_min); 119 120 void bc_sub (bc_num n1, bc_num n2, bc_num *result, int scale_min); 121 122 void bc_multiply (bc_num n1, bc_num n2, bc_num *prod, int scale); 123 124 int bc_divide (bc_num n1, bc_num n2, bc_num *quot, int scale); 125 126 int bc_modulo (bc_num num1, bc_num num2, bc_num *result, int scale); 127 128 int bc_divmod (bc_num num1, bc_num num2, bc_num *quot, 129 bc_num *rem, int scale); 130 131 int bc_raisemod (bc_num base, bc_num expo, bc_num mod, 132 bc_num *result, int scale); 133 134 void bc_raise (bc_num num1, bc_num num2, bc_num *result, 135 int scale); 136 137 int bc_sqrt (bc_num *num, int scale); 138 139 void bc_out_num (bc_num num, int o_base, void (* out_char)(int), 140 int leading_zero); 141 142 void bc_out_long (long val, int size, int space, void (*out_char)(int)); 143 #endif 144