-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.c
194 lines (180 loc) · 5.87 KB
/
common.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/******************************************************************************
*
* COPYRIGHT NOTICE
* Copyright (c) 2015
* All rights reserved
*
* @author : Gaoyang Dai
* @email : edward.d.erlic@gmail.com
* @file : /home/dgy/Programming/VInterval/common.c
* @date : 2015/03/26 15:09
* @description :
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
interval_value_type interval_value_max(interval_value_type a, interval_value_type b) {
if (a > b) {
return a ;
} else {
return b ;
}
} ;
interval_value_type interval_value_min(interval_value_type a, interval_value_type b) {
if (a < b) {
return a ;
} else {
return b ;
}
} ;
//handle add overflow
void add_overflow(interval_value_type *a, interval_value_type *b) {
if (((*b > 0) && (*a > (MAX_VALUE - *b))) || ((*b < 0) && (*a < (MIN_VALUE - *b)))) {
/* Handle error */
perror("add overflow!") ;
exit(EXIT_FAILURE) ;
}
}
//add operator, consider the infinite and overflow.
interval_value_type add_inf(interval_value_type a, interval_value_type b) {
if ((a == MAX_VALUE && b > MIN_VALUE && b < MAX_VALUE) || (b == MAX_VALUE && a > MIN_VALUE && a < MAX_VALUE) || (a == MAX_VALUE && b == MAX_VALUE) ) {
return MAX_VALUE ;
}
else if ((a == MIN_VALUE && b > MIN_VALUE && b < MAX_VALUE) || (b == MIN_VALUE && a > MIN_VALUE && a < MAX_VALUE) || (a == MIN_VALUE && b == MIN_VALUE)) {
return MIN_VALUE ;
}
else if ((a == MAX_VALUE && b == MIN_VALUE) || (a == MIN_VALUE && b == MAX_VALUE)) {
perror("can't calculate different infinite add!") ;
exit(EXIT_FAILURE);
}
else {
add_overflow(&a, &b) ;
return a + b ;
}
}
//handle sub overflow
void sub_overflow(interval_value_type *a, interval_value_type *b) {
if ((*b > 0 && *a < MIN_VALUE + *b) || (*b < 0 && *a > MAX_VALUE + *b)) {
/* Handle error */
perror("sub overflow!") ;
exit(EXIT_FAILURE) ;
}
}
//sub operator, consider the infinite and overflow
interval_value_type sub_inf(interval_value_type a, interval_value_type b) {
if ((a == MAX_VALUE && b > MIN_VALUE && b < MAX_VALUE) || (b == MIN_VALUE && a > MIN_VALUE && a < MAX_VALUE) || (a == MAX_VALUE && b == MIN_VALUE)) {
return MAX_VALUE ;
}
else if ((b == MAX_VALUE && a > MIN_VALUE && a < MAX_VALUE) || (a == MIN_VALUE && b > MIN_VALUE && b < MAX_VALUE) || (a == MIN_VALUE && b == MAX_VALUE)) {
return MIN_VALUE ;
}
else if ((a == MAX_VALUE && b == MAX_VALUE) || (a == MIN_VALUE && b == MIN_VALUE)) {
perror("can't calculate same infinite sub!") ;
exit(EXIT_FAILURE) ;
}
else {
sub_overflow(&a, &b) ;
return a - b ;
}
}
//handle mul overflow
void mul_overflow(interval_value_type *a, interval_value_type *b) {
signed int result = 1 ;
if (*a > 0) { /* si_a is positive */
if (*b > 0) { /* si_a and si_b are positive */
if (*a > (MAX_VALUE / *b)) {
/* Handle error */
result = -1 ;
}
} else { /* si_a positive, si_b nonpositive */
if (*b < (MIN_VALUE / *a)) {
/* Handle error */
result = -1 ;
}
} /* si_a positive, si_b nonpositive */
} else { /* si_a is nonpositive */
if (*b > 0) { /* si_a is nonpositive, si_b is positive */
if (*a < (MIN_VALUE / *b)) {
/* Handle error */
result = -1;
}
} else { /* si_a and si_b are nonpositive */
if ((*a != 0) && (*b < (MAX_VALUE / *a))) {
/* Handle error */
result = -1;
}
} /* End if si_a and si_b are nonpositive */
} /* End if si_a is nonpositive */
if (result == -1) {
perror("mul overflow!!") ;
exit(EXIT_FAILURE) ;
}
}
//mul operator, consider infinite and overflow
interval_value_type mul_inf(interval_value_type a, interval_value_type b) {
if (a == MAX_VALUE) {
if (b > 0) {
return MAX_VALUE ;
}
else if (b < 0) {
return MIN_VALUE ;
}
}
else if (a == MIN_VALUE) {
if (b > 0) {
return MIN_VALUE ;
}
else if (b < 0) {
return MAX_VALUE ;
}
}
else if (b == MAX_VALUE) {
if (a > 0) {
return MAX_VALUE ;
}
else if (a < 0) {
return MIN_VALUE ;
}
}
else if (b == MIN_VALUE) {
if (a > 0) {
return MIN_VALUE ;
}
else if (a < 0) {
return MAX_VALUE ;
}
}
else {
mul_overflow(&a, &b) ;
return a * b ;
}
}
//now can only handle int type, float type is ignored.
void div_overflow(interval_value_type *a, interval_value_type *b) {
if ((*b == 0) || ((*a == MIN_VALUE) && (*b == -1))) {
/* Handle error */
perror("div overflow!") ;
exit(EXIT_FAILURE) ;
}
}
//now the function is not used because the interval_div function has the infinite handling.
interval_value_type div_inf(interval_value_type a, interval_value_type b) {
if ((a == MAX_VALUE && b == MAX_VALUE) || (a == MAX_VALUE && b == MIN_VALUE) || (a == MIN_VALUE && b == MAX_VALUE) || (a == MIN_VALUE && b == MIN_VALUE)) {
perror("can't calculate two infinite number's div!") ;
exit(EXIT_FAILURE) ;
}
else if (b == MIN_VALUE || b == MAX_VALUE) {
return 0 ;
}
else if ((a == MAX_VALUE && b > 0) || (a == MIN_VALUE && b < 0)) {
return MAX_VALUE ;
}
else if ((a == MAX_VALUE && b < 0) || (a == MIN_VALUE && b > 0)) {
return MIN_VALUE ;
}
else {
div_overflow(&a, &b) ;
return a / b ;
}
}