source: git/kernel/longrat0.cc @ 528f5b7

spielwiese
Last change on this file since 528f5b7 was f12c611, checked in by Hans Schoenemann <hannes@…>, 13 years ago
tr 315: work-around for PLURAL git-svn-id: file:///usr/local/Singular/svn/trunk@13881 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 2.5 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 <kernel/mod2.h>
13#include <kernel/structs.h>
14#include <omalloc/omalloc.h>
15#include <kernel/febase.h>
16#include <kernel/longrat.h>
17#include <kernel/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)
51{
52  if (*s<'0' || *s>'9')
53  {
54    *a = INT_TO_SR(1); /* nlInit(1) */
55    return s;
56  }
57  *a=(number)omAllocBin(rnumber_bin);
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      omFreeBin(*a,rnumber_bin);
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      nlNormalize(*a);
99  }
100  return s;
101}
102
103/*2
104* write a rational number
105*/
106void nlWrite (number &a, const ring r)
107{
108  char *s,*z;
109  if (SR_HDL(a) & SR_INT)
110  {
111    StringAppend("%ld",SR_TO_INT(a));
112  }
113  else if (a==NULL)
114  {
115    StringAppendS("o");
116  }
117  else
118  {
119    if (a->s==0)
120    {
121      nlNormalize(a);
122      nlWrite(a,r);
123      return;
124    }
125    int l=mpz_sizeinbase(a->z,10);
126    if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10));
127    l+=2;
128    s=(char*)omAlloc(l);
129    z=mpz_get_str(s,10,a->z);
130    StringAppendS(z);
131    if (a->s!=3)
132    {
133      StringAppendS("/");
134      z=mpz_get_str(s,10,a->n);
135      StringAppendS(z);
136    }
137    omFreeSize((ADDRESS)s,l);
138  }
139}
140
Note: See TracBrowser for help on using the repository browser.