1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT - |
---|
6 | * IO for long rational numbers (Hubert Grassmann) |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | #include <string.h> |
---|
11 | |
---|
12 | #include "libpolys/misc/auxiliary.h" |
---|
13 | #include "omalloc/omalloc.h" |
---|
14 | #include "libpolys/reporter/reporter.h" |
---|
15 | |
---|
16 | #include "libpolys/coeffs/coeffs.h" |
---|
17 | #include "libpolys/coeffs/numbers.h" |
---|
18 | #include "libpolys/coeffs/longrat.h" |
---|
19 | |
---|
20 | omBin rnumber_bin = omGetSpecBin(sizeof(snumber)); // TODO: move this into coeffs-struct (for Q)?! |
---|
21 | |
---|
22 | |
---|
23 | #define SR_HDL(A) ((long)(A)) |
---|
24 | //#define SR_INT 1 // already in longrat.h |
---|
25 | //#define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT)) |
---|
26 | #define SR_TO_INT(SR) (((long)SR) >> 2) |
---|
27 | |
---|
28 | |
---|
29 | /*2 |
---|
30 | * extracts a long integer from s, returns the rest |
---|
31 | */ |
---|
32 | const char * nlEatLong(char *s, mpz_ptr i) |
---|
33 | { |
---|
34 | const char * start=s; |
---|
35 | |
---|
36 | while (*s >= '0' && *s <= '9') s++; |
---|
37 | if (*s=='\0') |
---|
38 | { |
---|
39 | mpz_set_str(i,start,10); |
---|
40 | } |
---|
41 | else |
---|
42 | { |
---|
43 | char c=*s; |
---|
44 | *s='\0'; |
---|
45 | mpz_set_str(i,start,10); |
---|
46 | *s=c; |
---|
47 | } |
---|
48 | return s; |
---|
49 | } |
---|
50 | |
---|
51 | /*2 |
---|
52 | * extracts the number a from s, returns the rest |
---|
53 | */ |
---|
54 | const char * nlRead (const char *s, number *a, const coeffs r) |
---|
55 | { |
---|
56 | if (*s<'0' || *s>'9') |
---|
57 | { |
---|
58 | *a = INT_TO_SR(1); /* nlInit(1) */ |
---|
59 | return s; |
---|
60 | } |
---|
61 | *a=(number)ALLOC_RNUMBER(); |
---|
62 | { |
---|
63 | (*a)->s = 3; |
---|
64 | #if defined(LDEBUG) |
---|
65 | (*a)->debug=123456; |
---|
66 | #endif |
---|
67 | mpz_ptr z=(*a)->z; |
---|
68 | mpz_ptr n=(*a)->n; |
---|
69 | mpz_init(z); |
---|
70 | s = nlEatLong((char *)s, z); |
---|
71 | if (*s == '/') |
---|
72 | { |
---|
73 | mpz_init(n); |
---|
74 | (*a)->s = 0; |
---|
75 | s++; |
---|
76 | s = nlEatLong((char *)s, n); |
---|
77 | if (mpz_cmp_si(n,0L)==0) |
---|
78 | { |
---|
79 | WerrorS(nDivBy0); |
---|
80 | mpz_clear(n); |
---|
81 | (*a)->s = 3; |
---|
82 | } |
---|
83 | else if (mpz_cmp_si(n,1L)==0) |
---|
84 | { |
---|
85 | mpz_clear(n); |
---|
86 | (*a)->s=3; |
---|
87 | } |
---|
88 | } |
---|
89 | if (mpz_cmp_si(z,0L)==0) |
---|
90 | { |
---|
91 | mpz_clear(z); |
---|
92 | FREE_RNUMBER(*a); |
---|
93 | *a=INT_TO_SR(0); |
---|
94 | } |
---|
95 | else if ((*a)->s==3) |
---|
96 | { |
---|
97 | number nlShort3_noinline(number x); |
---|
98 | *a=nlShort3_noinline(*a); |
---|
99 | } |
---|
100 | else |
---|
101 | { |
---|
102 | number aa=*a; |
---|
103 | nlNormalize(aa,r); // FIXME? TODO? // extern void nlNormalize(number &x, const coeffs r); |
---|
104 | *a=aa; |
---|
105 | } |
---|
106 | } |
---|
107 | return s; |
---|
108 | } |
---|
109 | |
---|
110 | /*2 |
---|
111 | * write a rational number |
---|
112 | */ |
---|
113 | void nlWrite (number a, const coeffs) |
---|
114 | { |
---|
115 | char *s,*z; |
---|
116 | if (SR_HDL(a) & SR_INT) |
---|
117 | { |
---|
118 | StringAppend("%ld",SR_TO_INT(a)); |
---|
119 | } |
---|
120 | else if (a==NULL) |
---|
121 | { |
---|
122 | StringAppendS("o"); |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | int l=mpz_sizeinbase(a->z,10); |
---|
127 | if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10)); |
---|
128 | l+=2; |
---|
129 | s=(char*)omAlloc(l); |
---|
130 | z=mpz_get_str(s,10,a->z); |
---|
131 | StringAppendS(z); |
---|
132 | if (a->s!=3) |
---|
133 | { |
---|
134 | StringAppendS("/"); |
---|
135 | z=mpz_get_str(s,10,a->n); |
---|
136 | StringAppendS(z); |
---|
137 | } |
---|
138 | omFreeSize((void *)s,l); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | #if 0 |
---|
143 | void nlDebugWrite (number a) |
---|
144 | { |
---|
145 | char *s,*z; |
---|
146 | if (SR_HDL(a) & SR_INT) |
---|
147 | { |
---|
148 | Print("%ld",SR_TO_INT(a)); |
---|
149 | } |
---|
150 | else if (a==NULL) |
---|
151 | { |
---|
152 | PrintS("o"); |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | int l=mpz_sizeinbase(a->z,10); |
---|
157 | if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10)); |
---|
158 | l+=2; |
---|
159 | s=(char*)omAlloc(l); |
---|
160 | z=mpz_get_str(s,10,a->z); |
---|
161 | PrintS(z); |
---|
162 | if (a->s!=3) |
---|
163 | { |
---|
164 | PrintS("/"); |
---|
165 | z=mpz_get_str(s,10,a->n); |
---|
166 | PrintS(z); |
---|
167 | } |
---|
168 | omFreeSize((void *)s,l); |
---|
169 | } |
---|
170 | } |
---|
171 | #endif |
---|