add $(EXEEXT) to executable targets during installation for MinGW
[suif.git] / src / baseparsuif / suifmath / fract.cc
blob95f5b379a992086a77590b17a97911a6d8126d11
1 /* file "fract.cc" */
3 /* Copyright (c) 1994 Stanford University
5 All rights reserved.
7 This software is provided under the terms described in
8 the "suif_copyright.h" include file. */
10 #include <suif_copyright.h>
12 /* Fraction Implementation */
13 #define _MODULE_ "libsuifmath.a"
14 #pragma implementation "fract.h"
16 #include "suif1.h"
17 #include "fract.h"
20 /* Reduce the fraction. */
22 void fract::reduce()
24 if (n == 0) d = 1;
25 else if (d == 1) ;
26 else if (n == 1 && d > 0) ;
27 else reduce_aux();
32 void fract::reduce_aux()
34 /* convert to absolute values */
35 int mm = (n < 0) ? -n : n;
36 int nn = (d < 0) ? -d : d;
38 /* find gcd and divide thru by it */
39 while (TRUE) {
40 int rr = mm % nn;
41 if (rr == 0) break; /* nn is the answer */
42 mm = nn;
43 nn = rr;
45 if (d < 0) nn = -nn;
46 n /= nn;
47 d /= nn;
52 fract fract::operator/(const fract &a) const
54 assert(a.num() != 0);
56 /* get the reciprocal */
57 fract rcp;
58 if (a.num() < 0) {
59 rcp = fract(fneg(a.denom()), fneg(a.num()));
60 } else {
61 rcp = fract(a.denom(), a.num());
64 /* multiply by the reciprocal */
65 return *this * rcp;
70 void fract::print(FILE *f) const
72 if (d == 1) {
73 fprintf(f, " %d ", n);
74 } else {
75 fprintf(f, "%d/%d", n, d);
77 fflush(f);