My Project  debian-1:4.1.1-p2+ds-4build4
longrat0.cc
Go to the documentation of this file.
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 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  {
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
All the auxiliary stuff.
static int si_max(const int a, const int b)
Definition: auxiliary.h:138
int l
Definition: cfEzgcd.cc:93
int i
Definition: cfEzgcd.cc:125
Variable x
Definition: cfModGcd.cc:4023
Coefficient rings, fields and other domains suitable for Singular polynomials.
#define ALLOC_RNUMBER()
Definition: coeffs.h:88
#define FREE_RNUMBER(x)
Definition: coeffs.h:87
#define Print
Definition: emacs.cc:80
#define StringAppend
Definition: emacs.cc:79
const CanonicalForm int s
Definition: facAbsFact.cc:55
void WerrorS(const char *s)
Definition: feFopen.cc:24
const char * nlRead(const char *s, number *a, const coeffs r)
Definition: longrat0.cc:54
const char * nlEatLong(char *s, mpz_ptr i)
Definition: longrat0.cc:32
omBin rnumber_bin
Definition: longrat0.cc:20
void nlWrite(number a, const coeffs)
Definition: longrat0.cc:113
#define SR_HDL(A)
Definition: longrat0.cc:23
#define SR_TO_INT(SR)
Definition: longrat0.cc:26
number nlShort3_noinline(number x)
Definition: longrat.cc:165
void nlNormalize(number &x, const coeffs r)
Definition: longrat.cc:1345
#define SR_INT
Definition: longrat.h:68
#define INT_TO_SR(INT)
Definition: longrat.h:69
'SR_INT' is the type of those integers small enough to fit into 29 bits.
Definition: longrat.h:50
The main handler for Singular numbers which are suitable for Singular polynomials.
const char *const nDivBy0
Definition: numbers.h:89
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omGetSpecBin(size)
Definition: omBin.h:11
#define NULL
Definition: omList.c:10
omBin_t * omBin
Definition: omStructs.h:12
void StringAppendS(const char *st)
Definition: reporter.cc:107
void PrintS(const char *s)
Definition: reporter.cc:284