source: git/libpolys/coeffs/longrat0.cc @ 45cc512

spielwiese
Last change on this file since 45cc512 was ba5e9e, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Changed configure-scripts to generate individual public config files for each package: resources, libpolys, singular (main) fix: sources should include correct corresponding config headers.
  • Property mode set to 100644
File size: 2.8 KB
Line 
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#ifdef HAVE_CONFIG_H
13#include "libpolysconfig.h"
14#endif /* HAVE_CONFIG_H */
15#include <misc/auxiliary.h>
16
17#include <omalloc/omalloc.h>
18#include <reporter/reporter.h>
19
20#include "coeffs.h"
21#include "numbers.h"
22
23#include "longrat.h"
24
25/// Our Type!
26static const n_coeffType ID = n_Q;
27
28omBin rnumber_bin = omGetSpecBin(sizeof(snumber)); // TODO: move this into coeffs-struct (for Q)?!
29
30
31#define SR_HDL(A) ((long)(A))
32//#define SR_INT    1 // already in longrat.h
33//#define INT_TO_SR(INT)  ((number) (((long)INT << 2) + SR_INT))
34#define SR_TO_INT(SR)   (((long)SR) >> 2)
35
36
37/*2
38* extracts a long integer from s, returns the rest
39*/
40static const char * nlEatLong(char *s, mpz_ptr i)
41{
42  const char * start=s;
43
44  while (*s >= '0' && *s <= '9') s++;
45  if (*s=='\0')
46  {
47    mpz_set_str(i,start,10);
48  }
49  else
50  {
51    char c=*s;
52    *s='\0';
53    mpz_set_str(i,start,10);
54    *s=c;
55  }
56  return s;
57}
58
59/*2
60* extracts the number a from s, returns the rest
61*/
62const char * nlRead (const char *s, number *a, const coeffs r)
63{
64  if (*s<'0' || *s>'9')
65  {
66    *a = INT_TO_SR(1); /* nlInit(1) */
67    return s;
68  }
69  *a=(number)ALLOC_RNUMBER();
70  {
71    (*a)->s = 3;
72#if defined(LDEBUG)
73    (*a)->debug=123456;
74#endif
75    mpz_ptr z=(*a)->z;
76    mpz_ptr n=(*a)->n;
77    mpz_init(z);
78    s = nlEatLong((char *)s, z);
79    if (*s == '/')
80    {
81      mpz_init(n);
82      (*a)->s = 0;
83      s++;
84      s = nlEatLong((char *)s, n);
85      if (mpz_cmp_si(n,(long)0)==0)
86      {
87        WerrorS(nDivBy0);
88        mpz_clear(n);
89        (*a)->s = 3;
90      }
91      else if (mpz_cmp_si(n,(long)1)==0)
92      {
93        mpz_clear(n);
94        (*a)->s=3;
95      }
96    }
97    if (mpz_cmp_si(z,(long)0)==0)
98    {
99      mpz_clear(z);
100      FREE_RNUMBER(*a);
101      *a=INT_TO_SR(0);
102    }
103    else
104    if ((*a)->s==3)
105    {
106      number nlShort3_noinline(number x);
107      *a=nlShort3_noinline(*a);
108    }
109    else
110    {
111      number aa=*a;
112      nlNormalize(aa,r);
113      *a=aa;
114    }
115  }
116  return s;
117}
118
119/*2
120* write a rational number
121*/
122void nlWrite (number &a, const coeffs r)
123{
124  char *s,*z;
125  if (SR_HDL(a) & SR_INT)
126  {
127    StringAppend("%ld",SR_TO_INT(a));
128  }
129  else if (a==NULL)
130  {
131    StringAppendS("o");
132  }
133  else
134  {
135    if (a->s==0)
136    {
137      nlNormalize(a,r);
138      nlWrite(a,r);
139      return;
140    }
141    int l=mpz_sizeinbase(a->z,10);
142    if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10));
143    l+=2;
144    s=(char*)omAlloc(l);
145    z=mpz_get_str(s,10,a->z);
146    StringAppendS(z);
147    if (a->s!=3)
148    {
149      StringAppendS("/");
150      z=mpz_get_str(s,10,a->n);
151      StringAppendS(z);
152    }
153    omFreeSize((void *)s,l);
154  }
155}
156
157
Note: See TracBrowser for help on using the repository browser.