aboutsummaryrefslogtreecommitdiff
path: root/bc_funcs/lib.bc
blob: d41376397949dd5764da347f326c4216d242cb6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
define sin(x) {
	auto b, s, r, a, q, i

	if (x < 0)
		return (-sin(-x))

	b = ibase
	ibase = A
	s = scale
	scale = 1.1 * s + 2
	a = atan(1)
	scale = 0
	q = (x / a + 2) / 4
	x -= 4 * q * a

	if (q % 2)
		x = -x

	scale = s + 2
	r = a = x
	q = -x * x

	for (i = 3; a; i += 2) {
		a *= q / (i * (i - 1))
		r += a
	}

	scale = s
	ibase = b
	return (r / 1)
}

define cos(x) {
	auto b, s
	b = ibase
	ibase = A
	s = scale
	scale *= 1.2
	x = sin(2 * atan(1) + x)
	scale = s
	ibase = b

	return (x / 1)
}

define atan(x) {
	auto b, s, r, n, a, m, t, f, i, u
	b = ibase
	ibase = A
	n = 1

	if (x < 0) {
		n = -1
		x = -x
	}

	if (scale < 65) {
		if (x == 1) {
			r = .7853981633974483096156608458198757210492923498437764552437361480/n
			ibase = b
			return (r)
		}

		if (x == .2) {
			r = .1973955598498807583700497651947902934475851037878521015176889402/n
			ibase = b
			return (r)
		}
	}

	s = scale

	if (x > .2) {
		scale += 5
		a = atan(.2)
	}

	scale = s + 3

	while (x > .2) {
		m += 1
		x = (x - .2) / (1 + .2 * x)
	}

	r = u = x
	f = -x * x
	t = 1

	for (i = 3; t; i += 2) {
		u *= f
		t = u / i
		r += t
	}

	scale = s
	ibase = b
	return ((m * a + r) / n)
}

define abs(x) {
	if (x < 0)
		x *= -1
	return x
}

define fact(x) {
	auto r, s

	if (x < 0) {
		print "Error: Negative factorial\n"
		return 0
	}

	/* x % 1 requires scale set to 0 */
	s = scale
	scale = 0
	if (x % 1 != 0) {
		print "Error: Non-integer factorial\n"
		scale = s
		return 0
	}

	r = 1
	for (i = 2; i <= x; i++)
		r *= i

	scale = s
	return r
}