1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: longrat0.cc,v 1.6 1998-05-27 17:14:08 Singular Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT - |
---|
7 | * IO for long rational numbers (Hubert Grassmann) |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdio.h> |
---|
11 | #include <string.h> |
---|
12 | #include "mod2.h" |
---|
13 | #include "tok.h" |
---|
14 | #include "mmemory.h" |
---|
15 | #include "febase.h" |
---|
16 | #include "longrat.h" |
---|
17 | |
---|
18 | #define SR_HDL(A) ((long)(A)) |
---|
19 | #define SR_INT 1 |
---|
20 | #define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT)) |
---|
21 | #define SR_TO_INT(SR) (((long)SR) >> 2) |
---|
22 | |
---|
23 | |
---|
24 | /*2 |
---|
25 | * extracts a long integer from s, returns the rest |
---|
26 | */ |
---|
27 | char * nlEatLong(char *s, MP_INT *i) |
---|
28 | { |
---|
29 | char * start=s; |
---|
30 | |
---|
31 | while (*s >= '0' && *s <= '9') s++; |
---|
32 | if (*s=='\0') |
---|
33 | { |
---|
34 | mpz_set_str(i,start,10); |
---|
35 | } |
---|
36 | else |
---|
37 | { |
---|
38 | char c=*s; |
---|
39 | *s='\0'; |
---|
40 | mpz_set_str(i,start,10); |
---|
41 | *s=c; |
---|
42 | } |
---|
43 | return s; |
---|
44 | } |
---|
45 | |
---|
46 | /*2 |
---|
47 | * extracts the number a from s, returns the rest |
---|
48 | */ |
---|
49 | char * nlRead (char *s, number *a) |
---|
50 | { |
---|
51 | if (*s<'0' || *s>'9') |
---|
52 | { |
---|
53 | *a = nlInit(1); |
---|
54 | return s; |
---|
55 | } |
---|
56 | *a=(number)Alloc(sizeof(rnumber)); |
---|
57 | { |
---|
58 | (*a)->s = 3; |
---|
59 | #ifdef LDEBUG |
---|
60 | (*a)->debug=123456; |
---|
61 | #endif |
---|
62 | MP_INT *z=&((*a)->z); |
---|
63 | MP_INT *n=&((*a)->n); |
---|
64 | mpz_init(z); |
---|
65 | s = nlEatLong(s, z); |
---|
66 | if (*s == '/') |
---|
67 | { |
---|
68 | mpz_init(n); |
---|
69 | (*a)->s = 0; |
---|
70 | s++; |
---|
71 | s = nlEatLong(s, n); |
---|
72 | if (mpz_cmp_si(n,(long)0)==0) |
---|
73 | { |
---|
74 | WerrorS("Zero Denominator"); |
---|
75 | mpz_clear(n); |
---|
76 | (*a)->s = 3; |
---|
77 | } |
---|
78 | else if (mpz_cmp_si(n,(long)1)==0) |
---|
79 | { |
---|
80 | mpz_clear(n); |
---|
81 | (*a)->s=3; |
---|
82 | } |
---|
83 | } |
---|
84 | if (mpz_cmp_si(z,(long)0)==0) |
---|
85 | { |
---|
86 | *a=INT_TO_SR(0); |
---|
87 | } |
---|
88 | else |
---|
89 | if ((*a)->s==3) |
---|
90 | { |
---|
91 | int ui=(int)mpz_get_si(&(*a)->z); |
---|
92 | if ((((ui<<3)>>3)==ui) |
---|
93 | && (mpz_cmp_si(&(*a)->z,(long)ui)==0)) |
---|
94 | { |
---|
95 | mpz_clear(&(*a)->z); |
---|
96 | Free((ADDRESS)(*a),sizeof(rnumber)); |
---|
97 | (*a)=INT_TO_SR(ui); |
---|
98 | } |
---|
99 | } |
---|
100 | else |
---|
101 | nlNormalize(*a); |
---|
102 | } |
---|
103 | return s; |
---|
104 | } |
---|
105 | |
---|
106 | /*2 |
---|
107 | * write a rational number |
---|
108 | */ |
---|
109 | void nlWrite (number &a) |
---|
110 | { |
---|
111 | char *s,*z; |
---|
112 | if (SR_HDL(a) & SR_INT) |
---|
113 | { |
---|
114 | StringAppend("%d",SR_TO_INT(a)); |
---|
115 | } |
---|
116 | #ifdef TEST |
---|
117 | else if (a==NULL) |
---|
118 | { |
---|
119 | StringAppend("o"); |
---|
120 | } |
---|
121 | #endif |
---|
122 | else |
---|
123 | { |
---|
124 | if (a->s==0) |
---|
125 | { |
---|
126 | nlNormalize(a); |
---|
127 | nlWrite(a); |
---|
128 | return; |
---|
129 | } |
---|
130 | int l=mpz_sizeinbase(&a->z,10); |
---|
131 | if (a->s<2) l=max(l,mpz_sizeinbase(&a->n,10)); |
---|
132 | l+=2; |
---|
133 | s=(char*)Alloc(l); |
---|
134 | z=mpz_get_str(s,10,&a->z); |
---|
135 | StringAppend(z); |
---|
136 | if (a->s!=3) |
---|
137 | { |
---|
138 | StringAppend("/"); |
---|
139 | z=mpz_get_str(s,10,&a->n); |
---|
140 | StringAppend(z); |
---|
141 | } |
---|
142 | Free((ADDRESS)s,l); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|