1: /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 2: 3: /* 4: $Header: b1num.h,v 1.4 85/08/22 16:41:53 timo Exp $ 5: */ 6: 7: /************************************************************************/ 8: /* Full numeric package: private definitions */ 9: /* */ 10: /* A number is modelled as one of zero, unbounded integer, */ 11: /* unbounded rational or approximate. */ 12: /* Zero has a 'length' field of zero, and nothing else. */ 13: /* A length field of +n means the number is an n digit integer, */ 14: /* (with digits to some large base). */ 15: /* A length of -1 means there follow two floating point numbers, */ 16: /* one the fraction (zero or .5 <= frac <= 1), one the exponent */ 17: /* with respect to base 2 (should be an integral value). */ 18: /* (This is so when EXT_RANGE is defined. Otherwise, there is */ 19: /* only one field, frac, which is not normalized. This saves */ 20: /* code and data space on e.g. the IBM PC, where the natural */ 21: /* range of double's is sufficient (~1E307).) */ 22: /* A length of -2 means there follow two values, pointers to two */ 23: /* unbounded integers, ie a rational number. */ 24: /* A length of -n, n>2, means it is a rational with a print width */ 25: /* of n-2. */ 26: /* */ 27: /************************************************************************/ 28: 29: /*************** Definitions exported for integers *****************/ 30: 31: typedef int digit; 32: 33: typedef struct integer { 34: HEADER; 35: digit dig[1]; 36: } *integer; 37: 38: #define FreezeSmallInt(v, vv) \ 39: (IsSmallInt(v) && (Freeze1(v, vv), Freeze2(v, vv))) 40: #define Freeze1(v, vv) ((vv).type= Num, (vv).refcnt= Maxrefcnt) 41: #define Freeze2(v, vv) \ 42: ((vv).len= (v) != 0, (vv).dig[0]= SmallIntVal(v), (v)= &(vv)) 43: 44: integer int_gadd(); 45: integer int_canon(); 46: integer int_prod(); 47: integer int_quot(); 48: integer int_gcd(); 49: integer mk_int(); 50: integer int1mul(); 51: integer int_tento(); 52: integer int_half(); 53: integer int_mod(); 54: digit int_ldiv(); 55: 56: #define int_0 ((integer) MkSmallInt(0)) 57: #define int_1 ((integer) MkSmallInt(1)) 58: #define int_2 ((integer) MkSmallInt(2)) 59: #define int_10 ((integer) MkSmallInt(10)) 60: 61: #define int_sum(v, w) int_gadd(v, w, 1) 62: #define int_diff(v, w) int_gadd(v, w, -1) 63: #define int_neg(v) int_gadd(int_0, v, -1) 64: 65: #define Integral(v) (IsSmallInt(v) || Length(v)>=0) 66: #define Modulo(a,b) (((a)%(b)+(b))%(b)) 67: #define Digit(v,n) ((v)->dig[n]) 68: #define Msd(v) (IsSmallInt(v) ? SmallIntVal(v) : Digit(v,Length(v)-1)) 69: #define Lsd(v) (IsSmallInt(v) ? SmallIntVal(v) : Digit(v,0)) 70: 71: #define Odd(x) ((x)&1) 72: #define Even(x) (!Odd(x)) 73: 74: /* Provisional definitions */ 75: 76: value copy(); 77: #define Copy(x) copy((value)(x)) 78: 79: /***************** Definitions exported for rationals *****************/ 80: 81: typedef struct { 82: HEADER; 83: integer num, den; 84: } *rational; 85: 86: 87: #define Numerator(a) ((a)->num) 88: #define Denominator(a) ((a)->den) 89: #define Rational(a) (!IsSmallInt(a) && Length(a)<-1) 90: #define Roundsize(a) (-2-Length(a)) 91: 92: rational mk_rat(); 93: rational rat_sum(); 94: rational rat_diff(); 95: rational rat_neg(); 96: rational rat_prod(); 97: rational rat_quot(); 98: rational rat_power(); 99: 100: extern rational rat_zero; 101: extern rational rat_half; 102: 103: value tento(); 104: value mk_exact(); 105: 106: /***************** Definitions exported for approximate numbers *************/ 107: 108: #ifdef vax 109: #define EXT_RANGE 110: #endif 111: 112: typedef struct real { 113: HEADER; 114: double frac; 115: #ifdef EXT_RANGE 116: double expo; 117: #endif EXT_RANGE 118: } *real; 119: 120: #define Frac(v) ((v)->frac) 121: #ifdef EXT_RANGE 122: #define Expo(v) ((v)->expo) 123: #else 124: #define Expo(v) 0.0 125: #endif 126: 127: #define Approximate(v) (!IsSmallInt(v) && Length(v)==-1) 128: #define Exact(v) (!Approximate(v)) 129: 130: extern real app_0; 131: 132: real mk_approx(); 133: 134: real app_sum(); 135: real app_diff(); 136: real app_prod(); 137: real app_quot(); 138: real app_neg(); 139: 140: real app_exp(); 141: real app_log(); 142: real app_power(); 143: 144: value app_floor(); 145: 146: 147: /* Numeric constants. */ 148: /* (Source: Knuth, The Art of Computer Programming, Vol. 1, Appendix B-1.) */ 149: 150: #define logtwo 0.6931471805599453094172321214581765680755 151: #define invlogtwo 1.4426950408889634073599246810018921374266 152: #define logten 2.3025850929940456840179914546843642076011 153: #define logBASE (logten*tenlogBASE) 154: #define twologBASE (logBASE*invlogtwo)