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