-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wave_math.py
57 lines (42 loc) · 1.26 KB
/
wave_math.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
"""Math helpers for wave operations."""
from typing import Union
Number = Union[int, float]
def get_max_value_from_bytes(bytes_size: int, signed=True) -> int:
"""Gets the max value that a number can be given the bytes.
Args:
bytes_size: Bytes size.
signed: Whether number is signed or not.
Returns:
Max number for given bytes.
"""
bits_size = bytes_size * 8
if signed:
bits_size = bits_size - 1 # Remove one bit for sign.
return 2 ** bits_size
def get_min_value_from_bytes(bytes_size: int, signed=True) -> int:
"""Gets the min value that a number can be given the bytes.
Args:
bytes_size: Bytes size.
signed: Whether number is signed or not.
Returns:
Min number for given bytes.
"""
if not signed:
return 0
max_size = get_max_value_from_bytes(bytes_size, signed)
return -max_size
def limit_value_to_range(
value: Number, min_value: Number, max_value: Number) -> Number:
"""Limits a number to a range.
Args:
value: Number to limit.
min_value: Min value that value can take.
max_value: Max value that value can take.
Returns:
Value within/limited by value range provided.
"""
if value >= max_value:
return max_value
if value <= min_value:
return min_value
return value