|
a |
|
b/feasible_joint_stiffness/lrslib-062/rat2float.c |
|
|
1 |
/* |
|
|
2 |
* Reads a polyhedron file on stdin , with rationals and outputs |
|
|
3 |
* an approximation in decimal floating point |
|
|
4 |
* |
|
|
5 |
* David Bremner. bremner@cs.mcgill.ca |
|
|
6 |
* |
|
|
7 |
*/ |
|
|
8 |
/* Hacked by DA, April 20 2006 |
|
|
9 |
* |
|
|
10 |
* first argument overides stdin |
|
|
11 |
* if column 0=0 then first non zero column scaled to +/-1 (otherwise big ugly integers come out) |
|
|
12 |
*/ |
|
|
13 |
|
|
|
14 |
static char rcsid[]="$Id: rat2float.c,v 1.2 2006/04/04 12:33:38 bremner Exp $"; |
|
|
15 |
|
|
|
16 |
#include <stdlib.h> |
|
|
17 |
#include <stdio.h> |
|
|
18 |
#include <string.h> |
|
|
19 |
#include <float.h> |
|
|
20 |
|
|
|
21 |
FILE *lrs_ifp; /* input file pointer */ |
|
|
22 |
|
|
|
23 |
#define DOCSTRING "\n\ |
|
|
24 |
$Id: rat2float.ds,v 1.3 2006/04/04 12:34:35 bremner Exp $ \n\ |
|
|
25 |
\n\ |
|
|
26 |
float takes a polytope file with rational or integer coefficents, \n\ |
|
|
27 |
and outputs an approximately equivelent one with floating point \n\ |
|
|
28 |
coefficents.\n\ |
|
|
29 |
\n\ |
|
|
30 |
WARNING: Assumes that numerator and denominator will fit in long integer,\n\ |
|
|
31 |
unless compiled with multiprecision support.\n\ |
|
|
32 |
\n\ |
|
|
33 |
\n\ |
|
|
34 |
\n\ |
|
|
35 |
" |
|
|
36 |
|
|
|
37 |
int usage(){ fprintf(stderr,"\n%s\n",rcsid);fprintf(stderr,DOCSTRING); exit(1); } |
|
|
38 |
#define CHECK_HELP if (argc > 1 && argv[1][0]=='-' && argv[1][1]=='h') usage(); |
|
|
39 |
|
|
|
40 |
#ifdef LRSMP |
|
|
41 |
#include "lrsmp.h" |
|
|
42 |
#endif |
|
|
43 |
|
|
|
44 |
#ifndef LRSMP |
|
|
45 |
typedef long integer_t; |
|
|
46 |
#define zero(n) (n==0) |
|
|
47 |
#define one(n) (n==1) |
|
|
48 |
#define pmp(s,n) printf("%s %d ",s,n) |
|
|
49 |
#define readrat(n,d) my_readrat(&n,&d); |
|
|
50 |
|
|
|
51 |
void my_readrat(long *num_p, long * denom_p) { |
|
|
52 |
|
|
|
53 |
char buf[BUFSIZ]; |
|
|
54 |
char *p; |
|
|
55 |
|
|
|
56 |
fscanf(lrs_ifp,"%s",buf); |
|
|
57 |
|
|
|
58 |
if (p=index(buf,'/')){ |
|
|
59 |
*p=0; |
|
|
60 |
*denom_p=atol(&p[1]); |
|
|
61 |
} else { |
|
|
62 |
*denom_p=1; |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
*num_p=atol(buf); |
|
|
66 |
|
|
|
67 |
} |
|
|
68 |
void rattodouble(integer_t num, integer_t denom, double *out_p){ |
|
|
69 |
*out_p=(double)num/(double)denom; |
|
|
70 |
} |
|
|
71 |
#else |
|
|
72 |
typedef lrs_mp integer_t; |
|
|
73 |
#define MP_DIGITS 1000L |
|
|
74 |
#endif |
|
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
int main(argc,argv) |
|
|
81 |
int argc; |
|
|
82 |
char **argv; |
|
|
83 |
{ |
|
|
84 |
long int n; |
|
|
85 |
int j; |
|
|
86 |
integer_t num,denom,sdenom; |
|
|
87 |
double out; |
|
|
88 |
int scale; /* if column 0 is zero, scale column 1 to 1 */ |
|
|
89 |
|
|
|
90 |
|
|
|
91 |
char format[BUFSIZ]; |
|
|
92 |
char buf[BUFSIZ]; |
|
|
93 |
char inputm[BUFSIZ]; |
|
|
94 |
|
|
|
95 |
|
|
|
96 |
CHECK_HELP; |
|
|
97 |
if(argc > 1 ) |
|
|
98 |
/* command line argument overides stdin */ |
|
|
99 |
{ |
|
|
100 |
if ((lrs_ifp = fopen (argv[1], "r")) == NULL) |
|
|
101 |
{ |
|
|
102 |
printf ("\nBad input file name\n"); |
|
|
103 |
return(1); |
|
|
104 |
} |
|
|
105 |
} |
|
|
106 |
else |
|
|
107 |
lrs_ifp=stdin; |
|
|
108 |
|
|
|
109 |
|
|
|
110 |
#ifdef LRSMP |
|
|
111 |
lrs_mp_init (MP_DIGITS,lrs_ifp,stdout); |
|
|
112 |
#endif |
|
|
113 |
sprintf(format,"%%.%dlf ",DBL_DIG); |
|
|
114 |
while ( fgets(buf,BUFSIZ,lrs_ifp) !=NULL ) |
|
|
115 |
{ |
|
|
116 |
fputs(buf,stdout); |
|
|
117 |
if (strncmp(buf,"begin",5)==0) break; |
|
|
118 |
} |
|
|
119 |
|
|
|
120 |
/* in lrs output m is undefined */ |
|
|
121 |
|
|
|
122 |
if (fscanf(lrs_ifp,"%s %ld %s",inputm,&n,buf)==EOF){ |
|
|
123 |
fprintf(stderr,"No begin line"); |
|
|
124 |
exit(1); |
|
|
125 |
} |
|
|
126 |
|
|
|
127 |
printf("%s %ld real\n",inputm,n); |
|
|
128 |
|
|
|
129 |
|
|
|
130 |
/* for (i=0;i<m;i++) for filtering lrs output we do not know m */ |
|
|
131 |
|
|
|
132 |
|
|
|
133 |
while (readrat(num,denom)!=999L) |
|
|
134 |
{ |
|
|
135 |
/* If column zero is zero lrs output is integer, so we scale by number in column one */ |
|
|
136 |
if (zero(num)) |
|
|
137 |
scale=TRUE; |
|
|
138 |
else |
|
|
139 |
scale=FALSE; |
|
|
140 |
|
|
|
141 |
pmp("",num); |
|
|
142 |
|
|
|
143 |
/* read in numbers */ |
|
|
144 |
|
|
|
145 |
for(j=1;j<n;j++) |
|
|
146 |
{ |
|
|
147 |
readrat(num,denom); |
|
|
148 |
if((scale==TRUE) && ( j==1) ) |
|
|
149 |
{ |
|
|
150 |
copy(sdenom,num); |
|
|
151 |
storesign(sdenom,POS); /* or else inequality is reversed .... */ |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
if (zero(num)) { |
|
|
155 |
printf(" 0 "); |
|
|
156 |
} else { |
|
|
157 |
if (one(denom)){ |
|
|
158 |
if (scale==TRUE) |
|
|
159 |
{ |
|
|
160 |
rattodouble(num,sdenom,&out); |
|
|
161 |
printf(format, out); |
|
|
162 |
} |
|
|
163 |
else |
|
|
164 |
pmp("",num); |
|
|
165 |
} else { |
|
|
166 |
rattodouble(num,denom,&out); |
|
|
167 |
printf(format, out); |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
} |
|
|
171 |
} |
|
|
172 |
fputs("\n",stdout); |
|
|
173 |
} |
|
|
174 |
fputs("end\n",stdout); |
|
|
175 |
fgets(buf,BUFSIZ,lrs_ifp); /* clean off last line */ |
|
|
176 |
|
|
|
177 |
while (fgets(buf,BUFSIZ,lrs_ifp) !=NULL ) |
|
|
178 |
{ |
|
|
179 |
fputs(buf,stdout); |
|
|
180 |
} |
|
|
181 |
return 0; |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
|