-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintMath.c
More file actions
54 lines (45 loc) · 1.13 KB
/
intMath.c
File metadata and controls
54 lines (45 loc) · 1.13 KB
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
#include <limits.h>
#include "intMath.h"
/**
Returns the smaller of the two input values
*/
inline int min(int a, int b) {
return (a < b) ? a : b;
}
/**
Returns the larger of the two input values
*/
inline int max(int a, int b) {
return (a > b) ? a : b;
}
/*
* returns the "correct" modulo of two numbers. b is assumed to be positive,
* a is an arbitrary integer.
*/
int mod(int a, int b) {
if (a % b >= 0) {
return a % b;
} else {
return b + (a % b);
}
}
int ipow(int b, int e){
//negative sucks just treat it as zero.
int r;
if(e<=0) return 1;
if(e%2==0){
r=ipow(b,e/2);
return r*r;
}
return b*ipow(b,e-1);
}
char intMultiplicationWillOverflow(int a, int x) {
return (a > INT_MAX / x) /* `a * x` would overflow */
|| (a < INT_MIN / x) /* `a * x` would underflow */
|| ((a == -1) && (x == INT_MIN)) /* `a * x` can overflow */;
}
char longMultiplicationWillOverflow(long int a, long int x) {
return (a > LONG_MAX / x) /* `a * x` would overflow */
|| (a < LONG_MIN / x) /* `a * x` would underflow */
|| ((a == -1) && (x == LONG_MIN)) /* `a * x` can overflow */;
}