source: CIVL/examples/floatsum.cvl@ 97cfc53

1.23 2.0 main test-branch
Last change on this file since 97cfc53 was bf5d35f, checked in by Stephen Siegel <siegel@…>, 13 years ago

Adding new examples. Removed unused definition in build.xml.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@100 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 1.4 KB
Line 
1
2#define N 4
3$input float x[N];
4$input float epsilon;
5
6typedef struct Interval {
7 float left;
8 float right;
9} Interval;
10
11Interval emptyInterval;
12
13Interval 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
26int empty(Interval a) {
27 return a.left > a.right;
28}
29
30int subset(Interval a, Interval b) {
31 return a.left >= b.left && a.right <= b.right;
32}
33
34Interval round(Interval a) {
35 if (empty(a))
36 return a;
37 else {
38 float left, right;
39
40 if (a.left>=0) {
41 left = (1-epsilon)*a.left;
42 right = (1+epsilon)*a.right;
43 } else {
44 left = (1+epsilon)*a.left;
45 if (a.right>=0)
46 right = (1+epsilon)*a.right;
47 else
48 right = (1-epsilon)*a.right;
49 }
50 return makeInterval(left, right);
51 }
52}
53
54Interval plus(Interval a, Interval b) {
55 if (empty(a))
56 return a;
57 if (empty(b))
58 return b;
59 else
60 return makeInterval(a.left + b.left, a.right + b.right);
61}
62
63Interval fplus(Interval a, Interval b) {
64 return round(plus(a,b));
65}
66
67void main() {
68 Interval a[N];
69 Interval r1, r2;
70
71 emptyInterval = makeInterval(0,-1);
72 $assume epsilon>0 && epsilon<0.1;
73 for (int i=0; i<N; i++) {
74 a[i].left = x[i];
75 a[i].right = x[i];
76 }
77 r1 = fplus(fplus(fplus(a[0],a[1]),a[2]),a[3]);
78 r2 = fplus(fplus(a[0],a[1]), fplus(a[2],a[3]));
79 $assert subset(r2, r1);
80}
Note: See TracBrowser for help on using the repository browser.