-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
61 lines (40 loc) · 1.34 KB
/
util.py
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
from math import *
def format_number(n, accuracy=6):
"""Formats a number in a friendly manner
(removes trailing zeros and unneccesary point."""
fs = "%."+str(accuracy)+"f"
str_n = fs%float(n)
if '.' in str_n:
str_n = str_n.rstrip('0').rstrip('.')
if str_n == "-0":
str_n = "0"
#str_n = str_n.replace("-0", "0")
return str_n
def lerp(a, b, i):
"""Linear enterpolate from a to b."""
return a+(b-a)*i
def range2d(range_x, range_y):
"""Creates a 2D range."""
range_x = list(range_x)
return [ (x, y) for y in range_y for x in range_x ]
def xrange2d(range_x, range_y):
"""Iterates over a 2D range."""
range_x = list(range_x)
for y in range_y:
for x in range_x:
yield (x, y)
def saturate(value, low, high):
return min(max(value, low), high)
def is_power_of_2(n):
"""Returns True if a value is a power of 2."""
return log(n, 2) % 1.0 == 0.0
def next_power_of_2(n):
"""Returns the next power of 2 that is >= n"""
return int(2 ** ceil(log(n, 2)))
if __name__ == "__main__":
print (list( xrange2d(xrange(3), xrange(3)) ))
print (range2d(xrange(3), xrange(3)))
print (is_power_of_2(7))
print (is_power_of_2(8))
print (is_power_of_2(9))
print (next_power_of_2(7))