|
|
1.1 ! root 1: /* ! 2: * Definition of pi. ! 3: * Used for large signs and funny ! 4: * arctangents. ! 5: */ ! 6: pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798 ! 7: ! 8: /* ! 9: * exp(x) -- exponential function ! 10: * to the scale that is in effect ! 11: * at the time of the call. ! 12: */ ! 13: define exp(x) { ! 14: auto i, num, den, term, result; ! 15: ! 16: num = den = result = 1; ! 17: for (i=1; ; i++) { ! 18: num *= x; ! 19: den *= i; ! 20: if ((term = num/den) == 0) return (result); ! 21: result += term; ! 22: } ! 23: } ! 24: ! 25: /* ! 26: * ln(x) -- natural logarithm ! 27: */ ! 28: define ln(x) { ! 29: auto i, xp1, xm1, num, den, term, result; ! 30: ! 31: if (x <= 0) return (0); ! 32: xp1 = x+1; ! 33: xm1 = x-1; ! 34: num = 2*xm1; ! 35: den = xp1; ! 36: result = num/den; ! 37: for (i=3; ; i+=2) { ! 38: num *= xm1*xm1; ! 39: den *= xp1*xp1; ! 40: if ((term = num/(i*den)) == 0) return (result); ! 41: result += term; ! 42: } ! 43: } ! 44: ! 45: /* ! 46: * sin(x) -- sine function. ! 47: */ ! 48: define sin(x) { ! 49: auto i, num, den, term, result, sign, pi2, minusx2; ! 50: ! 51: sign = den = 1; ! 52: pi2 = 2*pi; ! 53: if (x < 0) { ! 54: sign = -1; ! 55: x = -x; ! 56: } ! 57: while (x > pi2) x -= pi2; ! 58: num = result = x; ! 59: minusx2 = -x*x; ! 60: for (i=3; ; i+=2) { ! 61: num *= minusx2; ! 62: den *= (i-1)*i; ! 63: if ((term = num/den) == 0) return (sign*result); ! 64: result += term; ! 65: } ! 66: } ! 67: ! 68: /* ! 69: * cos(x) -- cosine function. ! 70: */ ! 71: define cos(x) { ! 72: auto i, num, den, term, result, pi2, minusx2; ! 73: ! 74: pi2 = 2*pi; ! 75: if (x < 0) x = -x; ! 76: while (x > pi2) x -= pi2; ! 77: num = den = result = 1; ! 78: minusx2 = -x*x; ! 79: for (i=2; ; i+=2) { ! 80: num *= minusx2; ! 81: den *= i*(i-1); ! 82: if ((term = num/den) == 0) return (result); ! 83: result += term; ! 84: } ! 85: } ! 86: ! 87: /* ! 88: * atan(x) -- arctangent function of x ! 89: */ ! 90: define atan(x) { ! 91: auto i, sign, inverse, num, den, term, x2, x2p1, result; ! 92: ! 93: sign = 1; ! 94: inverse = 0; ! 95: if (x < 0) { ! 96: sign = -1; ! 97: x = -x; ! 98: } ! 99: if (x > 1) { ! 100: inverse = 1; ! 101: x = 1/x; ! 102: } ! 103: x2 = x*x; ! 104: x2p1 = x2+1; ! 105: result = num = den = 1; ! 106: for (i=2; ; i+=2) { ! 107: num *= x2*i; ! 108: den *= x2p1*(i+1); ! 109: if ((term = num/den) == 0) { ! 110: result = x*result/x2p1; ! 111: if (inverse != 0) result = pi/2 - result; ! 112: return (sign * result); ! 113: } ! 114: result += term; ! 115: } ! 116: } ! 117: ! 118: /* ! 119: * Bessel functions of first kind of ! 120: * integer order `n' for `x'. ! 121: */ ! 122: define j(n, x) ! 123: { ! 124: auto i, num, den, result; ! 125: ! 126: if (n%1 != 0) return (0); ! 127: den = 2; ! 128: num = 1; ! 129: if (n > 0) { ! 130: for (i=0; i<n; i++) num *= x; ! 131: } else if (n < 0) { ! 132: for (i=0; i<-n; i++) den *= x; ! 133: } ! 134: return (0); ! 135: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.