aboutsummaryrefslogtreecommitdiff
path: root/bc_funcs/std.bc
blob: e90231e0488bcbb24c5ffaae4034c4afffc6f15e (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
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
}