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 "misc/auxiliary.h" |
---|
13 | #include "omalloc/omalloc.h" |
---|
14 | #include "reporter/reporter.h" |
---|
15 | |
---|
16 | #include "coeffs/coeffs.h" |
---|
17 | #include "coeffs/numbers.h" |
---|
18 | #include "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 the number a from s, returns the rest |
---|
31 | */ |
---|
32 | const char * nlRead (const char *s, number *a, const coeffs r) |
---|
33 | { |
---|
34 | if (*s<'0' || *s>'9') |
---|
35 | { |
---|
36 | *a = INT_TO_SR(1); /* nlInit(1) */ |
---|
37 | return s; |
---|
38 | } |
---|
39 | *a=(number)ALLOC_RNUMBER(); |
---|
40 | { |
---|
41 | (*a)->s = 3; |
---|
42 | #if defined(LDEBUG) |
---|
43 | (*a)->debug=123456; |
---|
44 | #endif |
---|
45 | mpz_ptr z=(*a)->z; |
---|
46 | mpz_ptr n=(*a)->n; |
---|
47 | mpz_init(z); |
---|
48 | s = nEatLong((char *)s, z); |
---|
49 | if (*s == '/') |
---|
50 | { |
---|
51 | mpz_init(n); |
---|
52 | (*a)->s = 0; |
---|
53 | s++; |
---|
54 | s = nEatLong((char *)s, n); |
---|
55 | if (mpz_cmp_si(n,0L)==0) |
---|
56 | { |
---|
57 | WerrorS(nDivBy0); |
---|
58 | mpz_clear(n); |
---|
59 | (*a)->s = 3; |
---|
60 | } |
---|
61 | else if (mpz_cmp_si(n,1L)==0) |
---|
62 | { |
---|
63 | mpz_clear(n); |
---|
64 | (*a)->s=3; |
---|
65 | } |
---|
66 | } |
---|
67 | if (mpz_cmp_si(z,0L)==0) |
---|
68 | { |
---|
69 | mpz_clear(z); |
---|
70 | FREE_RNUMBER(*a); |
---|
71 | *a=INT_TO_SR(0); |
---|
72 | } |
---|
73 | else if ((*a)->s==3) |
---|
74 | { |
---|
75 | number nlShort3_noinline(number x); |
---|
76 | *a=nlShort3_noinline(*a); |
---|
77 | } |
---|
78 | else |
---|
79 | { |
---|
80 | number aa=*a; |
---|
81 | nlNormalize(aa,r); // FIXME? TODO? // extern void nlNormalize(number &x, const coeffs r); |
---|
82 | *a=aa; |
---|
83 | } |
---|
84 | } |
---|
85 | return s; |
---|
86 | } |
---|
87 | |
---|
88 | /*2 |
---|
89 | * write a rational number |
---|
90 | */ |
---|
91 | void nlWrite (number a, const coeffs) |
---|
92 | { |
---|
93 | char *s,*z; |
---|
94 | if (SR_HDL(a) & SR_INT) |
---|
95 | { |
---|
96 | StringAppend("%ld",SR_TO_INT(a)); |
---|
97 | } |
---|
98 | else if (a==NULL) |
---|
99 | { |
---|
100 | StringAppendS("o"); |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | int l=mpz_sizeinbase(a->z,10); |
---|
105 | if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10)); |
---|
106 | l+=2; |
---|
107 | s=(char*)omAlloc(l); |
---|
108 | z=mpz_get_str(s,10,a->z); |
---|
109 | StringAppendS(z); |
---|
110 | if (a->s!=3) |
---|
111 | { |
---|
112 | StringAppendS("/"); |
---|
113 | z=mpz_get_str(s,10,a->n); |
---|
114 | StringAppendS(z); |
---|
115 | } |
---|
116 | omFreeSize((void *)s,l); |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | #if 0 |
---|
121 | void nlDebugWrite (number a) |
---|
122 | { |
---|
123 | char *s,*z; |
---|
124 | if (SR_HDL(a) & SR_INT) |
---|
125 | { |
---|
126 | Print("%ld",SR_TO_INT(a)); |
---|
127 | } |
---|
128 | else if (a==NULL) |
---|
129 | { |
---|
130 | PrintS("o"); |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | int l=mpz_sizeinbase(a->z,10); |
---|
135 | if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10)); |
---|
136 | l+=2; |
---|
137 | s=(char*)omAlloc(l); |
---|
138 | z=mpz_get_str(s,10,a->z); |
---|
139 | PrintS(z); |
---|
140 | if (a->s!=3) |
---|
141 | { |
---|
142 | PrintS("/"); |
---|
143 | z=mpz_get_str(s,10,a->n); |
---|
144 | PrintS(z); |
---|
145 | } |
---|
146 | omFreeSize((void *)s,l); |
---|
147 | } |
---|
148 | } |
---|
149 | #endif |
---|