source: git/libpolys/coeffs/longrat0.cc @ 24bfd7

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