| 1 | #include <civlc.cvh>
|
|---|
| 2 |
|
|---|
| 3 | #define N 4
|
|---|
| 4 | $input float x[N];
|
|---|
| 5 | $input float epsilon;
|
|---|
| 6 |
|
|---|
| 7 | typedef struct Interval {
|
|---|
| 8 | float left;
|
|---|
| 9 | float right;
|
|---|
| 10 | } Interval;
|
|---|
| 11 |
|
|---|
| 12 | Interval emptyInterval;
|
|---|
| 13 |
|
|---|
| 14 | Interval makeInterval(float left, float right) {
|
|---|
| 15 | Interval result;
|
|---|
| 16 |
|
|---|
| 17 | if (left <= right) {
|
|---|
| 18 | result.left = left;
|
|---|
| 19 | result.right = right;
|
|---|
| 20 | } else {
|
|---|
| 21 | result.left = 0;
|
|---|
| 22 | result.right = -1;
|
|---|
| 23 | }
|
|---|
| 24 | return result;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | int empty(Interval a) {
|
|---|
| 28 | return a.left > a.right;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | int subset(Interval a, Interval b) {
|
|---|
| 32 | return a.left >= b.left && a.right <= b.right;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | Interval round(Interval a) {
|
|---|
| 36 | int empty_a = empty(a);
|
|---|
| 37 |
|
|---|
| 38 | if (empty_a)
|
|---|
| 39 | return a;
|
|---|
| 40 | else {
|
|---|
| 41 | float left, right;
|
|---|
| 42 | Interval result;
|
|---|
| 43 |
|
|---|
| 44 | if (a.left>=0) {
|
|---|
| 45 | left = (1-epsilon)*a.left;
|
|---|
| 46 | right = (1+epsilon)*a.right;
|
|---|
| 47 | } else {
|
|---|
| 48 | left = (1+epsilon)*a.left;
|
|---|
| 49 | if (a.right>=0)
|
|---|
| 50 | right = (1+epsilon)*a.right;
|
|---|
| 51 | else
|
|---|
| 52 | right = (1-epsilon)*a.right;
|
|---|
| 53 | }
|
|---|
| 54 | result = makeInterval(left, right);
|
|---|
| 55 | return result;
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | Interval plus(Interval a, Interval b) {
|
|---|
| 60 | int empty_a = empty(a);
|
|---|
| 61 |
|
|---|
| 62 | if (empty_a)
|
|---|
| 63 | return a;
|
|---|
| 64 | else {
|
|---|
| 65 | int empty_b = empty(b);
|
|---|
| 66 |
|
|---|
| 67 | if (empty_b)
|
|---|
| 68 | return b;
|
|---|
| 69 | else {
|
|---|
| 70 | Interval result;
|
|---|
| 71 |
|
|---|
| 72 | result = makeInterval(a.left + b.left, a.right + b.right);
|
|---|
| 73 | return result;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | Interval fplus(Interval a, Interval b) {
|
|---|
| 79 | Interval result;
|
|---|
| 80 |
|
|---|
| 81 | result = round(plus(a,b));
|
|---|
| 82 | return result;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void main() {
|
|---|
| 86 | Interval a[N];
|
|---|
| 87 | Interval r1, r2;
|
|---|
| 88 | int test;
|
|---|
| 89 |
|
|---|
| 90 | emptyInterval = makeInterval(0,-1);
|
|---|
| 91 | $assume(epsilon>0 && epsilon<0.1);
|
|---|
| 92 | for (int i=0; i<N; i++) {
|
|---|
| 93 | a[i].left = x[i];
|
|---|
| 94 | a[i].right = x[i];
|
|---|
| 95 | }
|
|---|
| 96 | r1 = fplus(fplus(fplus(a[0],a[1]),a[2]),a[3]);
|
|---|
| 97 | r2 = fplus(fplus(a[0],a[1]), fplus(a[2],a[3]));
|
|---|
| 98 | test = subset(r2, r1);
|
|---|
| 99 | $assert(test);
|
|---|
| 100 | }
|
|---|