forked from acornejo/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bc
142 lines (124 loc) · 2.24 KB
/
bc
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
130
131
132
133
134
135
136
137
138
139
140
141
142
/* by Alex Cornejo */
/* Make bc into a usable calculator */
e=e(1)
pi=4*a(1)
base10=10
define sin(x) { return s(x); }
define cos(x) { return c(x); }
define tan(x) { return s(x)/c(x); }
define atan(x) { return a(x); }
define asin(x) { return 2*a(x/(1+sqrt(1-x*x))); }
define acos(x) { return 2*a(sqrt(1-x*x)/(1+x)); }
define exp(x) { return e(x); }
define log(x) {
if (x <= 0) {
print "log is only defined for positive numbers.\n"
halt;
}
return l(x);
}
define fact (n) {
if (n < 0) {
print "invalid argument to factorial!\n";
halt;
}
res = 1;
for (; n > 1; n--) {
res *= n;
}
return res;
}
define binom(n, k) {
auto res;
if (n < 0 || k < 0) {
print "invalid argument to binomial!\n";
halt;
}
res = 1;
for (i=1; i<=k; i++)
res = int(res*(n+1-i)/i);
return res;
}
define sign(x) {
if (x == 0) {
return 0;
} else if (x < 0) {
return -1;
} else if (x > 0) {
return 1;
}
}
define abs(x) {
if (x < 0) {
return -x;
} else {
return x;
}
}
define min(x,y) {
if (x < y) {
return x;
} else {
return y;
}
}
define max(x,y) {
if (x > y) {
return x;
} else {
return y;
}
}
define atan2(y, x) {
auto fi;
if (y == 0) {
if (x == 0) {
print "atan2(0,0) is undefined\n";
halt;
}
if (x > 0) {
return 0;
} else {
return pi;
}
} else if (x == 0) {
return pi*sign(y)/2;
}
fi = a(abs(y/x));
if (x > 0) {
return fi * sign(y);
} else {
return (pi - fi) * sign(y);
}
}
/* take integer part */
define int(x) {
auto old_scale;
old_scale = scale;
scale = 0;
ret = x/1;
scale = old_scale;
return ret
}
/* round to nearest integer */
define round(x) {
if (x<0) x-=.5 else x+=.5;
return int(x);
}
/* smallest integer >= arg */
define ceil(x) {
auto intx;
intx=int(x);
if (intx<x) intx+=1;
return intx;
}
/* largest integer <= arg */
define floor(x) {
return -ceil(-x);
}
/* reset ibase and obase */
define reset() {
ibase=base10
obase=base10
print "ibase=10, obase=10\n"
}