source: git/libpolys/coeffs/longrat0.cc @ 0a3fa3

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