SpeedCrunch
0.11
|
00001 /* number.h: Arbitrary precision numbers header file. */ 00002 /* 00003 Copyright (C) 1991, 1992, 1993, 1994, 1997, 2000 Free Software Foundation, Inc. 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License , or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; see the file COPYING. If not, write to: 00017 00018 The Free Software Foundation, Inc. 00019 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 00022 00023 You may contact the author by: 00024 e-mail: philnelson@acm.org 00025 us-mail: Philip A. Nelson 00026 Computer Science Department, 9062 00027 Western Washington University 00028 Bellingham, WA 98226-9062 00029 00030 *************************************************************************/ 00031 00032 #ifndef _NUMBER_H_ 00033 #define _NUMBER_H_ 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 #undef _PROTOTYPE 00040 00041 #ifndef NUMBER__STDC__ 00042 #define NUMBER__STDC__ 00043 #endif 00044 00045 typedef enum {PLUS, MINUS} sign; 00046 00047 typedef struct bc_struct *bc_num; 00048 00049 typedef struct bc_struct 00050 { 00051 sign n_sign; 00052 int n_len; /* The number of digits before the decimal point. */ 00053 int n_scale; /* The number of digits after the decimal point. */ 00054 int n_refs; /* The number of pointers to this number. */ 00055 bc_num n_next; /* Linked list for available list. */ 00056 char *n_ptr; /* The pointer to the actual storage. 00057 If NULL, n_value points to the inside of 00058 another number (bc_multiply...) and should 00059 not be "freed." */ 00060 char *n_value; /* The number. Not zero char terminated. 00061 May not point to the same place as n_ptr as 00062 in the case of leading zeros generated. */ 00063 } bc_struct; 00064 00065 00066 /* The base used in storing the numbers in n_value above. 00067 Currently this MUST be 10. */ 00068 00069 #define BASE 10 00070 00071 /* Some useful macros and constants. */ 00072 00073 #define CH_VAL(c) (c - '0') 00074 #define CH_HEX(c) ((c < ':') ? ( c - '0') : (c < 'G') ? ( c - 'A' + 10) : ( c - 'a' + 10)) 00075 #define BCD_CHAR(d) (d + '0') 00076 00077 #ifdef MIN 00078 #undef MIN 00079 #undef MAX 00080 #endif 00081 #define MAX(a,b) ((a)>(b)?(a):(b)) 00082 #define MIN(a,b) ((a)>(b)?(b):(a)) 00083 #define ODD(a) ((a)&1) 00084 00085 #ifndef TRUE 00086 #define TRUE 1 00087 #define FALSE 0 00088 #endif 00089 00090 #ifndef LONG_MAX 00091 #define LONG_MAX 0x7ffffff 00092 #endif 00093 00094 00095 /* Global numbers. */ 00096 extern bc_num _zero_; 00097 extern bc_num _one_; 00098 extern bc_num _two_; 00099 00100 00101 /* Function Prototypes */ 00102 00103 /* Define the _PROTOTYPE macro if it is needed. */ 00104 00105 #ifndef _PROTOTYPE 00106 #ifdef NUMBER__STDC__ 00107 #define _PROTOTYPE(func, args) func args 00108 #else 00109 #define _PROTOTYPE(func, args) func() 00110 #endif 00111 #endif 00112 00113 _PROTOTYPE(void bc_init_numbers, (void)); 00114 00115 _PROTOTYPE(bc_num bc_new_num, (int length, int scale)); 00116 00117 _PROTOTYPE(void bc_free_num, (bc_num *num)); 00118 00119 _PROTOTYPE(bc_num bc_copy_num, (bc_num num)); 00120 00121 _PROTOTYPE(void bc_init_num, (bc_num *num)); 00122 00123 _PROTOTYPE(void bc_str2num, (bc_num *num, char *str, int scale)); 00124 00125 _PROTOTYPE(char *bc_num2str, (bc_num num)); 00126 00127 _PROTOTYPE(void bc_int2num, (bc_num *num, int val)); 00128 00129 _PROTOTYPE(long bc_num2long, (bc_num num)); 00130 00131 _PROTOTYPE(int bc_compare, (bc_num n1, bc_num n2)); 00132 00133 _PROTOTYPE(char bc_is_zero, (bc_num num)); 00134 00135 _PROTOTYPE(char bc_is_near_zero, (bc_num num, int scale)); 00136 00137 _PROTOTYPE(char bc_is_neg, (bc_num num)); 00138 00139 _PROTOTYPE(void bc_add, (bc_num n1, bc_num n2, bc_num *result, int scale_min)); 00140 00141 _PROTOTYPE(void bc_sub, (bc_num n1, bc_num n2, bc_num *result, int scale_min)); 00142 00143 _PROTOTYPE(void bc_multiply, (bc_num n1, bc_num n2, bc_num *prod, int scale)); 00144 00145 _PROTOTYPE(int bc_divide, (bc_num n1, bc_num n2, bc_num *quot, int scale)); 00146 00147 _PROTOTYPE(int bc_modulo, (bc_num num1, bc_num num2, bc_num *result, 00148 int scale)); 00149 00150 _PROTOTYPE(int bc_divmod, (bc_num num1, bc_num num2, bc_num *quot, 00151 bc_num *rem, int scale)); 00152 00153 _PROTOTYPE(int bc_raisemod, (bc_num base, bc_num expo, bc_num mod, 00154 bc_num *result, int scale)); 00155 00156 _PROTOTYPE(void bc_raise, (bc_num num1, bc_num num2, bc_num *result, 00157 int scale)); 00158 00159 _PROTOTYPE(int bc_sqrt, (bc_num *num, int scale)); 00160 00161 _PROTOTYPE(void bc_out_num, (bc_num num, int o_base, void (* out_char)(int), 00162 int leading_zero)); 00163 00164 #ifdef __cplusplus 00165 } 00166 #endif 00167 00168 #endif