-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbc+
executable file
·87 lines (72 loc) · 1.74 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
#!/usr/bin/bc
# bc integer functions I've found useful
# while systems programming in the unix environment.
# License: LGPLv2
# Author:
# http://www.pixelbeat.org/
# Notes:
# I only use bc when python is not available.
# Personally I have this file in ~/bin/bc so
# that I can just invoke bc as normal and have these
# extra functions available.
# Changes:
# V0.1, 11 Apr 2007, Initial release
define min(x,y) {
if (x<y) return x
return y
}
define max(x,y) {
if (x>y) return x
return y
}
define abs(x) {
if (x<0) return -x
return x
}
/* take integer part */
define int(x) {
auto old_scale /* variables global by default */
old_scale=scale /* scale is global */
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)
}
/* round x to previous multiple of y */
define round_down(x,y) {
return y*floor(x/y)
}
/* round x to next multiple of y */
define round_up(x,y) {
return y*ceil(x/y)
}
/* round x to nearest multiple of y */
define round_to(x,y) {
return y*round(x/y)
}
/* Greatest Common Divisor or Highest Common Factor of x and y */
/* Note when people say Lowest Common Denominator they usually mean this */
define gcd(x,y) {
if (y==0) return x
return gcd(y,x%y) /* anything that divides into x and y also divides into the remainder of x/y */
}
/* Lowest Common Multiple of x and y */
/* Lowest Common Denominator of fractions is LCM of the denominators */
define lcm(x,y) {
return (x*y/gcd(x,y))
}