source: git/libpolys/coeffs/longrat0.cc @ 06abb07

fieker-DuValspielwiese
Last change on this file since 06abb07 was 9b88e6, checked in by Hans Schoenemann <hannes@…>, 8 years ago
format: (long)0 -> 0L etc.
  • 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#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!
21static const n_coeffType ID = n_Q;
22
23omBin 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*/
35static 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*/
57const 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      {
82        WerrorS(nDivBy0);
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*/
116void 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    if (a->s==0)
130    {
131      nlNormalize(a,r); // FIXME? TODO? // extern void     nlNormalize(number &x, const coeffs r);
132      nlWrite(a,r);
133      return;
134    }
135    int l=mpz_sizeinbase(a->z,10);
136    if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10));
137    l+=2;
138    s=(char*)omAlloc(l);
139    z=mpz_get_str(s,10,a->z);
140    StringAppendS(z);
141    if (a->s!=3)
142    {
143      StringAppendS("/");
144      z=mpz_get_str(s,10,a->n);
145      StringAppendS(z);
146    }
147    omFreeSize((void *)s,l);
148  }
149}
150
151
Note: See TracBrowser for help on using the repository browser.