-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.c
117 lines (109 loc) · 5.58 KB
/
interval.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
/******************************************************************************
*
* COPYRIGHT NOTICE
* Copyright (c) 2015
* All rights reserved
*
* @author : Gaoyang Dai
* @email : edward.d.erlic@gmail.com
* @file : /home/dgy/Programming/VInterval/interval.c
* @date : 2015/03/26 09:22
* @description :
*
*****************************************************************************/
#include <stdio.h>
#include <math.h>
#include "interval.h"
#include "common.h"
void interval_add(interval *a, interval *b, interval *c) {
if (a->low_value > MIN_VALUE && a->up_value > MIN_VALUE && b->low_value > MIN_VALUE && b->up_value > MIN_VALUE &&
a->low_value < MAX_VALUE && a->up_value < MAX_VALUE && b->low_value < MAX_VALUE && b->up_value < MAX_VALUE) {
c->low_value = add_inf(a->low_value, b->low_value) ;
c->up_value = add_inf(a->up_value, b->up_value) ;
}
else if ((a->up_value == MAX_VALUE || b->up_value == MAX_VALUE) &&
!((a->low_value == MIN_VALUE && b->low_value == MAX_VALUE) || (a->low_value == MAX_VALUE && b->low_value == MIN_VALUE))) {
c->low_value = add_inf(a->low_value, b->low_value) ;
c->up_value = MAX_VALUE ;
}
else if ((a->low_value == MIN_VALUE || b->low_value == MIN_VALUE) &&
!((a->up_value == MIN_VALUE && b->up_value == MAX_VALUE) || (a->up_value == MAX_VALUE && b->up_value == MIN_VALUE))) {
c->low_value = MIN_VALUE ;
c->up_value = add_inf(a->up_value, b->up_value) ;
}
else {
c->low_value = MIN_VALUE ;
c->up_value = MAX_VALUE ;
}
} ;
void interval_sub(interval *a, interval *b, interval *c) {
if (a->low_value > MIN_VALUE && a->up_value > MIN_VALUE && b->low_value > MIN_VALUE && b->up_value > MIN_VALUE &&
a->low_value < MAX_VALUE && a->up_value < MAX_VALUE && b->low_value < MAX_VALUE && b->up_value < MAX_VALUE) {
c->low_value = sub_inf(a->low_value, b->up_value) ;
c->up_value = sub_inf(a->up_value, b->low_value) ;
}
else if ((a->up_value == MAX_VALUE || b->low_value == MIN_VALUE) &&
!((a->low_value == MAX_VALUE && b->up_value == MAX_VALUE) || (a->low_value == MIN_VALUE && b->up_value == MIN_VALUE))) {
c->low_value = sub_inf(a->low_value, b->up_value) ;
c->up_value = MAX_VALUE ;
}
else if ((a->low_value == MIN_VALUE || b->up_value == MAX_VALUE) &&
!((a->up_value == MAX_VALUE && b->low_value == MAX_VALUE) || (a->up_value == MIN_VALUE && b->low_value == MIN_VALUE))) {
c->low_value = MIN_VALUE ;
c->up_value = sub_inf(a->up_value, b->low_value) ;
}
else {
c->low_value = MIN_VALUE ;
c->up_value = MAX_VALUE ;
}
} ;
void interval_mul(interval *a, interval *b, interval *c) {
if ((a->up_value < 0 && b->low_value > 0) || (a->up_value < 0 && b->up_value < 0) ||
(a->low_value > 0 && b->low_value > 0) || (a->low_value > 0 && b->up_value < 0) ||
(a->low_value > MIN_VALUE && a->up_value > MIN_VALUE && b->low_value > MIN_VALUE && b->up_value > MIN_VALUE &&
a->low_value < MAX_VALUE && a->up_value < MAX_VALUE && b->low_value < MAX_VALUE && b->up_value < MAX_VALUE)) {
interval_value_type temp_max, temp_min, temp_mid;
temp_min = interval_value_min(mul_inf(a->low_value, b->low_value), mul_inf(a->low_value, b->up_value)) ;
temp_mid = interval_value_min(mul_inf(a->up_value, b->low_value), mul_inf(a->up_value, b->up_value)) ;
temp_min = interval_value_min(temp_min, temp_mid) ;
temp_max = interval_value_max(mul_inf(a->low_value, b->low_value), mul_inf(a->low_value, b->up_value)) ;
temp_mid = interval_value_max(mul_inf(a->up_value, b->low_value), mul_inf(a->up_value, b->up_value)) ;
temp_max = interval_value_max(temp_max, temp_mid) ;
c->low_value = temp_min ;
c->up_value = temp_max ;
}
else {
c->low_value = MIN_VALUE ;
c->up_value = MAX_VALUE ;
}
};
//if the arguments are float, just wipe out the floor and ceil function from the body.
void interval_div(interval *a, interval *b, interval *c) {
if ((b->low_value > MIN_VALUE && b->up_value < 0) || (b->low_value > 0 && b->up_value < MAX_VALUE)) {
interval_value_type temp_max, temp_min, temp_mid;
temp_min = interval_value_min((interval_value_type)floor((double)a->low_value / b->low_value),
(interval_value_type)floor((double)a->low_value / b->up_value)) ;
temp_mid = interval_value_min((interval_value_type)floor((double)a->up_value / b->low_value),
(interval_value_type)floor((double)a->up_value / b->up_value)) ;
temp_min = interval_value_min(temp_min, temp_mid) ;
temp_max = interval_value_max((interval_value_type)ceil((double)a->low_value / b->low_value),
(interval_value_type)ceil((double)a->low_value / b->up_value)) ;
temp_mid = interval_value_max((interval_value_type)ceil((double)a->up_value / b->low_value),
(interval_value_type)ceil((double)a->up_value / b->up_value)) ;
temp_max = interval_value_max(temp_max, temp_mid) ;
c->low_value = temp_min ;
c->up_value = temp_max ;
}
else if (b->low_value <= 0 && b->up_value >= 0 && a->up_value < 0) {
c->low_value = MIN_VALUE ;
c->up_value = -a->low_value ;
}
else if (b->low_value <= 0 && b->up_value >=0 && a->low_value > 0) {
c->low_value = -a->up_value ;
c->up_value = MAX_VALUE ;
}
else {
c->low_value = MIN_VALUE ;
c->up_value = MAX_VALUE ;
}
} ;