-
Notifications
You must be signed in to change notification settings - Fork 3
/
interval.c
151 lines (123 loc) · 2.55 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
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
#include "interval.h"
#include "stdio.h"
#include <math.h>
// for matlab, compiler may not have M_PI defined
#ifndef M_PI
# define M_PI 3.141592653589793238
#endif
#define TWO_PI (2*M_PI)
Interval new_interval(double min, double max)
{
Interval rv;
rv.min = min;
rv.max = max;
return rv;
}
Interval new_interval_v(double val)
{
Interval rv;
rv.min = rv.max = val;
return rv;
}
Interval add_interval(Interval i, Interval j)
{
Interval rv;
double a = i.min;
double b = i.max;
double c = j.min;
double d = j.max;
rv.min = a + c;
rv.max = b + d;
return rv;
}
Interval sub_interval(Interval i, Interval j)
{
Interval rv;
double a = i.min;
double b = i.max;
double c = j.min;
double d = j.max;
rv.min = a - d;
rv.max = b - c;
return rv;
}
Interval mul_interval(Interval i, Interval j)
{
Interval rv;
double a = i.min;
double b = i.max;
double c = j.min;
double d = j.max;
rv.min = fmin(fmin(a*c,a*d), fmin(b*c,b*d));
rv.max = fmax(fmax(a*c,a*d), fmax(b*c,b*d));
return rv;
}
Interval div_interval(Interval i, Interval j)
{
double c = j.min;
double d = j.max;
return mul_interval(i, new_interval(1 / d, 1 / c));
}
Interval pow_interval(Interval i, int n) // a^n
{
double a = i.min;
double b = i.max;
double c = 0;
double d = 0;
if (n % 2 == 1)
{
c = pow(a, n);
d = pow(b, n);
}
else
{
if (a >= 0)
{
c = pow(a, n);
d = pow(b, n);
}
else if (b < 0)
{
c = pow(b, n);
d = pow(a, n);
}
else
{
c = 0;
d = fmax(pow(a, n), pow(b, n));
}
}
return new_interval(c, d);
}
Interval sin_interval(Interval i)
{
double a = i.min;
double b = i.max;
double c = 0;
double d = 0;
if (floor((a-1.5*M_PI) / TWO_PI) != floor((b-1.5*M_PI) / TWO_PI))
c = -1;
else
c = fmin(sin(a), sin(b));
if (floor((a-0.5*M_PI) / TWO_PI) != floor((b-0.5*M_PI) / TWO_PI))
d = 1;
else
d = fmax(sin(a), sin(b));
return new_interval(c, d);
}
Interval cos_interval(Interval i)
{
double a = i.min;
double b = i.max;
double c = 0;
double d = 0;
if (floor((a+M_PI) / TWO_PI) != floor((b+M_PI) / TWO_PI))
c = -1;
else
c = fmin(cos(a), cos(b));
if (floor(a / TWO_PI) != floor(b / TWO_PI))
d = 1;
else
d = fmax(cos(a), cos(b));
return new_interval(c, d);
}