File indexing completed on 2024-05-12 05:55:17

0001 /* number.h: Arbitrary precision numbers header file. */
0002 /*
0003     Copyright (C) 1991, 1992, 1993, 1994, 1997, 2000 Free Software Foundation, Inc.
0004 
0005     This program is free software; you can redistribute it and/or modify
0006     it under the terms of the GNU General Public License as published by
0007     the Free Software Foundation; either version 2 of the License , or
0008     (at your option) any later version.
0009 
0010     This program is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013     GNU General Public License for more details.
0014 
0015     You should have received a copy of the GNU General Public License
0016     along with this program; see the file COPYING.  If not, write to:
0017 
0018       The Free Software Foundation, Inc.
0019       51 Franklin Street, Fifth Floor,
0020       Boston, MA 02110-1301, USA.
0021 
0022 
0023     You may contact the author by:
0024        e-mail:  philnelson@acm.org
0025       us-mail:  Philip A. Nelson
0026                 Computer Science Department, 9062
0027                 Western Washington University
0028                 Bellingham, WA 98226-9062
0029        
0030 *************************************************************************/
0031 
0032 #ifndef _NUMBER_H_
0033 #define _NUMBER_H_
0034 
0035 #include <limits.h>
0036 
0037 #ifdef __cplusplus
0038 extern "C" {
0039 #endif 
0040 
0041 #undef _PROTOTYPE
0042 
0043 #ifndef NUMBER__STDC__
0044 #define NUMBER__STDC__
0045 #endif
0046 
0047 typedef enum {PLUS, MINUS} sign;
0048 
0049 typedef struct bc_struct *bc_num;
0050 
0051 typedef struct bc_struct
0052     {
0053       sign  n_sign;
0054       int   n_len;  /* The number of digits before the decimal point. */
0055       int   n_scale;    /* The number of digits after the decimal point. */
0056       int   n_refs;     /* The number of pointers to this number. */
0057       bc_num n_next;    /* Linked list for available list. */
0058       char *n_ptr;  /* The pointer to the actual storage.
0059                If NULL, n_value points to the inside of
0060                another number (bc_multiply...) and should
0061                not be "freed." */
0062       char *n_value;    /* The number. Not zero char terminated.
0063                May not point to the same place as n_ptr as
0064                in the case of leading zeros generated. */
0065     } bc_struct;
0066 
0067 
0068 /* The base used in storing the numbers in n_value above.
0069    Currently this MUST be 10. */
0070 
0071 #define BASE 10
0072 
0073 /*  Some useful macros and constants. */
0074 
0075 #define CH_VAL(c)     (c - '0')
0076 #define CH_HEX(c)     ((c < ':') ? ( c - '0') : (c < 'G') ? ( c - 'A' + 10) : ( c - 'a' + 10))
0077 #define BCD_CHAR(d)   (d + '0')
0078 
0079 #ifdef MIN
0080 #undef MIN
0081 #undef MAX
0082 #endif
0083 #define MAX(a,b)      ((a)>(b)?(a):(b))
0084 #define MIN(a,b)      ((a)>(b)?(b):(a))
0085 #define ODD(a)        ((a)&1)
0086 
0087 #ifndef TRUE
0088 #define TRUE 1
0089 #define FALSE 0
0090 #endif
0091 
0092 #ifndef LONG_MAX
0093 #define LONG_MAX 0x7ffffff
0094 #endif
0095 
0096 
0097 /* Global numbers. */
0098 extern bc_num _zero_;
0099 extern bc_num _one_;
0100 extern bc_num _two_;
0101 
0102 
0103 /* Function Prototypes */
0104 
0105 /* Define the _PROTOTYPE macro if it is needed. */
0106 
0107 #ifndef _PROTOTYPE
0108 #ifdef NUMBER__STDC__
0109 #define _PROTOTYPE(func, args) func args
0110 #else
0111 #define _PROTOTYPE(func, args) func()
0112 #endif
0113 #endif
0114 
0115 _PROTOTYPE(void bc_init_numbers, (void));
0116 
0117 _PROTOTYPE(bc_num bc_new_num, (int length, int scale));
0118 
0119 _PROTOTYPE(void bc_free_num, (bc_num *num));
0120 
0121 _PROTOTYPE(bc_num bc_copy_num, (bc_num num));
0122 
0123 _PROTOTYPE(void bc_init_num, (bc_num *num));
0124 
0125 _PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale));
0126 
0127 _PROTOTYPE(char *bc_num2str, (bc_num num));
0128 
0129 _PROTOTYPE(void bc_int2num, (bc_num *num, int val));
0130 
0131 _PROTOTYPE(long bc_num2long, (bc_num num));
0132 
0133 _PROTOTYPE(int bc_compare, (bc_num n1, bc_num n2));
0134 
0135 _PROTOTYPE(char bc_is_zero, (bc_num num));
0136 
0137 _PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale));
0138 
0139 _PROTOTYPE(char bc_is_neg, (bc_num num));
0140 
0141 _PROTOTYPE(void bc_add, (bc_num n1, bc_num n2, bc_num *result, int scale_min));
0142 
0143 _PROTOTYPE(void bc_sub, (bc_num n1, bc_num n2, bc_num *result, int scale_min));
0144 
0145 _PROTOTYPE(void bc_multiply, (bc_num n1, bc_num n2, bc_num *prod, int scale));
0146 
0147 _PROTOTYPE(int bc_divide, (bc_num n1, bc_num n2, bc_num *quot, int scale));
0148 
0149 _PROTOTYPE(int bc_modulo, (bc_num num1, bc_num num2, bc_num *result,
0150                int scale));
0151 
0152 _PROTOTYPE(int bc_divmod, (bc_num num1, bc_num num2, bc_num *quot,
0153                bc_num *rem, int scale));
0154 
0155 _PROTOTYPE(int bc_raisemod, (bc_num base, bc_num expo, bc_num mod,
0156                  bc_num *result, int scale));
0157 
0158 _PROTOTYPE(void bc_raise, (bc_num num1, bc_num num2, bc_num *result,
0159                int scale));
0160 
0161 _PROTOTYPE(int bc_sqrt, (bc_num *num, int scale));
0162 
0163 _PROTOTYPE(void bc_out_num, (bc_num num, int o_base, void (* out_char)(int),
0164                  int leading_zero));
0165                  
0166 #ifdef __cplusplus
0167 }
0168 #endif 
0169 
0170 #endif