source: CIVL/examples/experimental/floatsum.cvl

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

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

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