source: git/libpolys/coeffs/longrat0.cc @ d11524

spielwiese
Last change on this file since d11524 was 691dba, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: use #include "config.h" everywhere
  • Property mode set to 100644
File size: 2.6 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#include "config.h"
13#include <reporter/reporter.h>
14#include <coeffs/coeffs.h>
15#include <omalloc/omalloc.h>
16#include <coeffs/longrat.h>
17#include <coeffs/numbers.h>
18
19#define SR_HDL(A) ((long)(A))
20//#define SR_INT    1 // already in longrat.h
21//#define INT_TO_SR(INT)  ((number) (((long)INT << 2) + SR_INT))
22#define SR_TO_INT(SR)   (((long)SR) >> 2)
23
24
25/*2
26* extracts a long integer from s, returns the rest
27*/
28static const char * nlEatLong(char *s, mpz_ptr i)
29{
30  const char * start=s;
31
32  while (*s >= '0' && *s <= '9') s++;
33  if (*s=='\0')
34  {
35    mpz_set_str(i,start,10);
36  }
37  else
38  {
39    char c=*s;
40    *s='\0';
41    mpz_set_str(i,start,10);
42    *s=c;
43  }
44  return s;
45}
46
47/*2
48* extracts the number a from s, returns the rest
49*/
50const char * nlRead (const char *s, number *a, const coeffs r)
51{
52  if (*s<'0' || *s>'9')
53  {
54    *a = INT_TO_SR(1); /* nlInit(1) */
55    return s;
56  }
57  *a=(number)ALLOC_RNUMBER();
58  {
59    (*a)->s = 3;
60#if defined(LDEBUG)
61    (*a)->debug=123456;
62#endif
63    mpz_ptr z=(*a)->z;
64    mpz_ptr n=(*a)->n;
65    mpz_init(z);
66    s = nlEatLong((char *)s, z);
67    if (*s == '/')
68    {
69      mpz_init(n);
70      (*a)->s = 0;
71      s++;
72      s = nlEatLong((char *)s, n);
73      if (mpz_cmp_si(n,(long)0)==0)
74      {
75        WerrorS(nDivBy0);
76        mpz_clear(n);
77        (*a)->s = 3;
78      }
79      else if (mpz_cmp_si(n,(long)1)==0)
80      {
81        mpz_clear(n);
82        (*a)->s=3;
83      }
84    }
85    if (mpz_cmp_si(z,(long)0)==0)
86    {
87      mpz_clear(z);
88      FREE_RNUMBER(*a);
89      *a=INT_TO_SR(0);
90    }
91    else
92    if ((*a)->s==3)
93    {
94      number nlShort3_noinline(number x);
95      *a=nlShort3_noinline(*a);
96    }
97    else
98    {
99      number aa=*a;
100      nlNormalize(aa,r);
101      *a=aa;
102    }
103  }
104  return s;
105}
106
107/*2
108* write a rational number
109*/
110void nlWrite (number &a, const coeffs r)
111{
112  char *s,*z;
113  if (SR_HDL(a) & SR_INT)
114  {
115    StringAppend("%ld",SR_TO_INT(a));
116  }
117  else if (a==NULL)
118  {
119    StringAppendS("o");
120  }
121  else
122  {
123    if (a->s==0)
124    {
125      nlNormalize(a,r);
126      nlWrite(a,r);
127      return;
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
Note: See TracBrowser for help on using the repository browser.