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.h"
17 #include "numbers.h"
18 #include "longrat.h"
19 
20 /// Our Type!
21 static const n_coeffType ID = n_Q;
22 
23 omBin rnumber_bin = omGetSpecBin(sizeof(snumber)); // TODO: move this into coeffs-struct (for Q)?!
24 
25 
26 #define SR_HDL(A) ((long)(A))
27 //#define SR_INT 1 // already in longrat.h
28 //#define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT))
29 #define SR_TO_INT(SR) (((long)SR) >> 2)
30 
31 
32 /*2
33 * extracts a long integer from s, returns the rest
34 */
35 static const char * nlEatLong(char *s, mpz_ptr i)
36 {
37  const char * start=s;
38 
39  while (*s >= '0' && *s <= '9') s++;
40  if (*s=='\0')
41  {
42  mpz_set_str(i,start,10);
43  }
44  else
45  {
46  char c=*s;
47  *s='\0';
48  mpz_set_str(i,start,10);
49  *s=c;
50  }
51  return s;
52 }
53 
54 /*2
55 * extracts the number a from s, returns the rest
56 */
57 const char * nlRead (const char *s, number *a, const coeffs r)
58 {
59  if (*s<'0' || *s>'9')
60  {
61  *a = INT_TO_SR(1); /* nlInit(1) */
62  return s;
63  }
64  *a=(number)ALLOC_RNUMBER();
65  {
66  (*a)->s = 3;
67 #if defined(LDEBUG)
68  (*a)->debug=123456;
69 #endif
70  mpz_ptr z=(*a)->z;
71  mpz_ptr n=(*a)->n;
72  mpz_init(z);
73  s = nlEatLong((char *)s, z);
74  if (*s == '/')
75  {
76  mpz_init(n);
77  (*a)->s = 0;
78  s++;
79  s = nlEatLong((char *)s, n);
80  if (mpz_cmp_si(n,0L)==0)
81  {
83  mpz_clear(n);
84  (*a)->s = 3;
85  }
86  else if (mpz_cmp_si(n,1L)==0)
87  {
88  mpz_clear(n);
89  (*a)->s=3;
90  }
91  }
92  if (mpz_cmp_si(z,0L)==0)
93  {
94  mpz_clear(z);
95  FREE_RNUMBER(*a);
96  *a=INT_TO_SR(0);
97  }
98  else if ((*a)->s==3)
99  {
100  number nlShort3_noinline(number x);
101  *a=nlShort3_noinline(*a);
102  }
103  else
104  {
105  number aa=*a;
106  nlNormalize(aa,r); // FIXME? TODO? // extern void nlNormalize(number &x, const coeffs r);
107  *a=aa;
108  }
109  }
110  return s;
111 }
112 
113 /*2
114 * write a rational number
115 */
116 void nlWrite (number a, const coeffs r)
117 {
118  char *s,*z;
119  if (SR_HDL(a) & SR_INT)
120  {
121  StringAppend("%ld",SR_TO_INT(a));
122  }
123  else if (a==NULL)
124  {
125  StringAppendS("o");
126  }
127  else
128  {
129  int l=mpz_sizeinbase(a->z,10);
130  if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10));
131  l+=2;
132  s=(char*)omAlloc(l);
133  z=mpz_get_str(s,10,a->z);
134  StringAppendS(z);
135  if (a->s!=3)
136  {
137  StringAppendS("/");
138  z=mpz_get_str(s,10,a->n);
139  StringAppendS(z);
140  }
141  omFreeSize((void *)s,l);
142  }
143 }
144 
145 #if 0
146 void nlDebugWrite (number a)
147 {
148  char *s,*z;
149  if (SR_HDL(a) & SR_INT)
150  {
151  Print("%ld",SR_TO_INT(a));
152  }
153  else if (a==NULL)
154  {
155  PrintS("o");
156  }
157  else
158  {
159  int l=mpz_sizeinbase(a->z,10);
160  if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10));
161  l+=2;
162  s=(char*)omAlloc(l);
163  z=mpz_get_str(s,10,a->z);
164  PrintS(z);
165  if (a->s!=3)
166  {
167  PrintS("/");
168  z=mpz_get_str(s,10,a->n);
169  PrintS(z);
170  }
171  omFreeSize((void *)s,l);
172  }
173 }
174 #endif
175 
const CanonicalForm int s
Definition: facAbsFact.cc:55
omBin rnumber_bin
Definition: longrat0.cc:23
#define INT_TO_SR(INT)
Definition: longrat.h:69
const poly a
Definition: syzextra.cc:212
omBin_t * omBin
Definition: omStructs.h:12
#define Print
Definition: emacs.cc:83
void nlWrite(number a, const coeffs r)
Definition: longrat0.cc:116
&#39;SR_INT&#39; is the type of those integers small enough to fit into 29 bits.
Definition: longrat.h:49
rational (GMP) numbers
Definition: coeffs.h:31
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
const char * nlRead(const char *s, number *a, const coeffs r)
Definition: longrat0.cc:57
#define SR_HDL(A)
Definition: longrat0.cc:26
#define FREE_RNUMBER(x)
Definition: coeffs.h:86
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define omAlloc(size)
Definition: omAllocDecl.h:210
static const n_coeffType ID
Our Type!
Definition: longrat0.cc:21
const ring r
Definition: syzextra.cc:208
Coefficient rings, fields and other domains suitable for Singular polynomials.
The main handler for Singular numbers which are suitable for Singular polynomials.
void StringAppendS(const char *st)
Definition: reporter.cc:107
All the auxiliary stuff.
const char *const nDivBy0
Definition: numbers.h:83
static int si_max(const int a, const int b)
Definition: auxiliary.h:121
#define StringAppend
Definition: emacs.cc:82
int i
Definition: cfEzgcd.cc:123
void PrintS(const char *s)
Definition: reporter.cc:284
number nlShort3_noinline(number x)
Definition: longrat.cc:170
#define omGetSpecBin(size)
Definition: omBin.h:11
void nlNormalize(number &x, const coeffs r)
Definition: longrat.cc:1334
n_coeffType
Definition: coeffs.h:27
#define NULL
Definition: omList.c:10
#define SR_TO_INT(SR)
Definition: longrat0.cc:29
#define SR_INT
Definition: longrat.h:68
#define ALLOC_RNUMBER()
Definition: coeffs.h:87
Variable x
Definition: cfModGcd.cc:4023
static const char * nlEatLong(char *s, mpz_ptr i)
Definition: longrat0.cc:35
int l
Definition: cfEzgcd.cc:94