4 # http://www.pixelbeat.org/
6 # Integer functions I've found useful while
7 # systems programming in the unix environment.
9 # I only use bc when python is not available.
10 # Personally I have this file in ~/bin/bc so
11 # that I can just invoke bc as normal and have these
12 # extra functions available.
14 # V0.1, 11 Apr 2007, Initial release
16 # 13 Feb 2011, Andreas Hrubak, factor() and binom() added
17 # 17 Jan 2017, Andreas Hrubak, anot() and dbzeqz() added
21 /* factorial product */
22 if (x <= 1) return (1)
23 return factor(x-1) * x
26 /* binominal product */
27 return factor(n) / (factor(n-k)*factor(k))
46 /* take integer part */
48 auto old_scale /* variables global by default */
49 old_scale=scale /* scale is global */
56 /* round to nearest integer */
58 if (x<0) x-=.5 else x+=.5
62 /* smallest integer >= arg */
70 /* largest integer <= arg */
75 /* round x to previous multiple of y */
76 define round_down(x,y) {
80 /* round x to next multiple of y */
81 define round_up(x,y) {
85 /* round x to nearest multiple of y */
86 define round_to(x,y) {
90 /* Greatest Common Divisor or Highest Common Factor of x and y */
91 /* Note when people say Lowest Common Denominator they usually mean this */
94 return gcd(y,x%y) /* anything that divides into x and y also divides into the remainder of x/y */
97 /* Lowest Common Multiple of x and y */
98 /* Lowest Common Denominator of fractions is LCM of the denominators */
100 return (x*y) / gcd(x,y)
111 /* Logical Negation expressed in Arithmetic way */
113 return abs(sgn(abs(x))-1)
116 /* Let Division By Zero equal to Zero */
118 return (p-(anot(d)*p)) / (d+anot(d))