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