source: git/kernel/longrat0.cc @ 3a67ea7

spielwiese
Last change on this file since 3a67ea7 was 2d2326, checked in by Hans Schönemann <hannes@…>, 18 years ago
*hannes: syntax git-svn-id: file:///usr/local/Singular/svn/trunk@8926 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 2.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: longrat0.cc,v 1.3 2006-02-01 13:51:05 Singular Exp $ */
5/*
6* ABSTRACT -
7* IO for long rational numbers (Hubert Grassmann)
8*/
9
10#include <stdio.h>
11#include <string.h>
12#include "mod2.h"
13#include "structs.h"
14#include "omalloc.h"
15#include "febase.h"
16#include "longrat.h"
17
18#define SR_HDL(A) ((long)(A))
19//#define SR_INT    1 // already in longrat.h
20#define INT_TO_SR(INT)  ((number) (((long)INT << 2) + SR_INT))
21#define SR_TO_INT(SR)   (((long)SR) >> 2)
22
23
24/*2
25* extracts a long integer from s, returns the rest
26*/
27char * nlEatLong(char *s, MP_INT *i)
28{
29  char * start=s;
30
31  while (*s >= '0' && *s <= '9') s++;
32  if (*s=='\0')
33  {
34    mpz_set_str(i,start,10);
35  }
36  else
37  {
38    char c=*s;
39    *s='\0';
40    mpz_set_str(i,start,10);
41    *s=c;
42  }
43  return s;
44}
45
46/*2
47* extracts the number a from s, returns the rest
48*/
49char * nlRead (char *s, number *a)
50{
51  if (*s<'0' || *s>'9')
52  {
53    *a = INT_TO_SR(1); /* nlInit(1) */
54    return s;
55  }
56  *a=(number)omAllocBin(rnumber_bin);
57  {
58    (*a)->s = 3;
59#if defined(LDEBUG)
60    (*a)->debug=123456;
61#endif
62    MP_INT *z=&((*a)->z);
63    MP_INT *n=&((*a)->n);
64    mpz_init(z);
65    s = nlEatLong(s, z);
66    if (*s == '/')
67    {
68      mpz_init(n);
69      (*a)->s = 0;
70      s++;
71      s = nlEatLong(s, n);
72      if (mpz_cmp_si(n,(long)0)==0)
73      {
74        WerrorS("Zero Denominator");
75        mpz_clear(n);
76        (*a)->s = 3;
77      }
78      else if (mpz_cmp_si(n,(long)1)==0)
79      {
80        mpz_clear(n);
81        (*a)->s=3;
82      }
83    }
84    if (mpz_cmp_si(z,(long)0)==0)
85    {
86      *a=INT_TO_SR(0);
87    }
88    else
89    if ((*a)->s==3)
90    {
91      int ui=(int)mpz_get_si(&(*a)->z);
92      if ((((ui<<3)>>3)==ui)
93      && (mpz_cmp_si(&(*a)->z,(long)ui)==0))
94      {
95        mpz_clear(&(*a)->z);
96        omFreeBin((ADDRESS)(*a), rnumber_bin);
97        (*a)=INT_TO_SR(ui);
98      }
99    }
100    else
101      nlNormalize(*a);
102  }
103  return s;
104}
105
106/*2
107* write a rational number
108*/
109void nlWrite (number &a)
110{
111  char *s,*z;
112  if (SR_HDL(a) & SR_INT)
113  {
114    StringAppend("%d",SR_TO_INT(a));
115  }
116  else if (a==NULL)
117  {
118    StringAppendS("o");
119  }
120  else
121  {
122    if (a->s==0)
123    {
124      nlNormalize(a);
125      nlWrite(a);
126      return;
127    }
128    int l=mpz_sizeinbase(&a->z,10);
129    if (a->s<2) l=si_max(l,mpz_sizeinbase(&a->n,10));
130    l+=2;
131    s=(char*)omAlloc(l);
132    z=mpz_get_str(s,10,&a->z);
133    StringAppendS(z);
134    if (a->s!=3)
135    {
136      StringAppendS("/");
137      z=mpz_get_str(s,10,&a->n);
138      StringAppendS(z);
139    }
140    omFreeSize((ADDRESS)s,l);
141  }
142}
143
Note: See TracBrowser for help on using the repository browser.