aboutsummaryrefslogtreecommitdiff
path: root/rat2float.c
blob: 2b23de8d797b4b19bd8f617a384008749a2f5a04 (plain)
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
/*
 * Reads a polyhedron file on stdin with rationals and outputs 
 * an approximation in decimal floating point
 * 
 * David Bremner. bremner@unb.ca
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <float.h>

#include "process_args.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

void
lrs_overflow (int ignored) {
  fprintf (stderr, "lrs_overflow called. exiting\n");
  exit(1);
}
#endif

#include "rat2float.h"


int main(argc,argv)
     int argc;
     char **argv;
{
  long int  m,n;
  int i,j;
  
  
  char format[BUFSIZ];
  char  buf[BUFSIZ];

  process_args(argc,argv,DOCSTRING);

#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);
    }
}