Add a function to wrap percentage calculations

This should help avoid issues from integer overflow (which is technically undefined behaviour)
while also allowing such issues to be addressed centrally if they still exist.
This commit is contained in:
2023-01-21 17:41:33 -05:00
parent 7b4df6edf8
commit 4c6296612d
7 changed files with 18 additions and 19 deletions

View File

@@ -43,6 +43,11 @@ short minmax(short min,short max,short k){
return k;
}
int percent(int value, int percentage) {
// TODO: Any need to protect from overflow?
return (value * percentage) / 100;
}
short gcd(short a, short b){ // Grabbed from Wikipedia and translated to C code
short t;
while(b != 0){