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