aboutsummaryrefslogtreecommitdiff
path: root/rat2float.c
diff options
context:
space:
mode:
authorbremner <bremner@09fa754a-f411-0410-976a-da6bfa213b30>2006-04-21 17:39:32 +0000
committerbremner <bremner@09fa754a-f411-0410-976a-da6bfa213b30>2006-04-21 17:39:32 +0000
commita468544d39ef18c6e47fd59cf427a4e25fe22b7f (patch)
treeaacd9a4cad151cd60cef375d75088cbe3f18fe7e /rat2float.c
cvs to svn conversion script
git-svn-id: file:///export/data/bremner/svn/trunk/inetools@2344 09fa754a-f411-0410-976a-da6bfa213b30
Diffstat (limited to 'rat2float.c')
-rw-r--r--rat2float.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/rat2float.c b/rat2float.c
new file mode 100644
index 0000000..079c995
--- /dev/null
+++ b/rat2float.c
@@ -0,0 +1,130 @@
+/*
+ * Reads a polyhedron file on stdin with rationals and outputs
+ * an approximation in decimal floating point
+ *
+ * David Bremner. bremner@cs.mcgill.ca
+ *
+ */
+
+static char rcsid[]="$Id$";
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <float.h>
+
+#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;
+
+ scanf("%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
+
+
+
+#include "rat2float.ds"
+
+
+
+int main(argc,argv)
+ int argc;
+ char **argv;
+{
+ long int m,n;
+ int i,j;
+
+
+ char format[BUFSIZ];
+ char buf[BUFSIZ];
+
+ CHECK_HELP;
+
+#ifdef LRSMP
+ lrs_mp_init (MP_DIGITS,stdin,stdout);
+#endif
+ sprintf(format,"%%.%dlf ",DBL_DIG);
+ while ( fgets(buf,BUFSIZ,stdin) !=NULL )
+ {
+ fputs(buf,stdout);
+ if (strncmp(buf,"begin",5)==0) break;
+ }
+
+ if (scanf("%ld %ld %s",&m,&n,buf)==EOF){
+ fprintf(stderr,"No begin line");
+ exit(1);
+ }
+
+ printf("%ld %ld real\n",m,n);
+
+
+ for (i=0;i<m;i++)
+ {
+
+ /* read in numbers */
+
+ for(j=0;j<n;j++)
+ {
+ char *p;
+
+ integer_t num,denom;
+ double out;
+
+ readrat(num,denom);
+
+ if (zero(num)) {
+ printf("0 ");
+ } else {
+ if (one(denom)){
+ pmp("",num);
+ } else {
+ rattodouble(num,denom,&out);
+ printf(format, out);
+ }
+
+ }
+ }
+ fputs("\n",stdout);
+ }
+
+ fgets(buf,BUFSIZ,stdin); /* clean off last line */
+
+ while (fgets(buf,BUFSIZ,stdin) !=NULL )
+ {
+ fputs(buf,stdout);
+ }
+}
+
+
+
+
+
+