[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[85e68dd] | 4 | /* $Id: longrat0.cc,v 1.5 2008-03-19 17:44:10 Singular Exp $ */ |
---|
[35aab3] | 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" |
---|
[7c961ba] | 17 | #include "numbers.h" |
---|
[35aab3] | 18 | |
---|
| 19 | #define SR_HDL(A) ((long)(A)) |
---|
[2d2326] | 20 | //#define SR_INT 1 // already in longrat.h |
---|
[35aab3] | 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 | */ |
---|
[85e68dd] | 28 | static const char * nlEatLong(char *s, MP_INT *i) |
---|
[35aab3] | 29 | { |
---|
[85e68dd] | 30 | const char * start=s; |
---|
[35aab3] | 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 | */ |
---|
[85e68dd] | 50 | const char * nlRead (const char *s, number *a) |
---|
[35aab3] | 51 | { |
---|
| 52 | if (*s<'0' || *s>'9') |
---|
| 53 | { |
---|
[6c9007] | 54 | *a = INT_TO_SR(1); /* nlInit(1) */ |
---|
[35aab3] | 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 | MP_INT *z=&((*a)->z); |
---|
| 64 | MP_INT *n=&((*a)->n); |
---|
| 65 | mpz_init(z); |
---|
[85e68dd] | 66 | s = nlEatLong((char *)s, z); |
---|
[35aab3] | 67 | if (*s == '/') |
---|
| 68 | { |
---|
| 69 | mpz_init(n); |
---|
| 70 | (*a)->s = 0; |
---|
| 71 | s++; |
---|
[85e68dd] | 72 | s = nlEatLong((char *)s, n); |
---|
[35aab3] | 73 | if (mpz_cmp_si(n,(long)0)==0) |
---|
| 74 | { |
---|
[7c961ba] | 75 | WerrorS(nDivBy0); |
---|
[35aab3] | 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 | *a=INT_TO_SR(0); |
---|
| 88 | } |
---|
| 89 | else |
---|
| 90 | if ((*a)->s==3) |
---|
| 91 | { |
---|
| 92 | int ui=(int)mpz_get_si(&(*a)->z); |
---|
| 93 | if ((((ui<<3)>>3)==ui) |
---|
| 94 | && (mpz_cmp_si(&(*a)->z,(long)ui)==0)) |
---|
| 95 | { |
---|
| 96 | mpz_clear(&(*a)->z); |
---|
| 97 | omFreeBin((ADDRESS)(*a), rnumber_bin); |
---|
| 98 | (*a)=INT_TO_SR(ui); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | nlNormalize(*a); |
---|
| 103 | } |
---|
| 104 | return s; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /*2 |
---|
| 108 | * write a rational number |
---|
| 109 | */ |
---|
| 110 | void nlWrite (number &a) |
---|
| 111 | { |
---|
| 112 | char *s,*z; |
---|
| 113 | if (SR_HDL(a) & SR_INT) |
---|
| 114 | { |
---|
| 115 | StringAppend("%d",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); |
---|
| 126 | nlWrite(a); |
---|
| 127 | return; |
---|
| 128 | } |
---|
| 129 | int l=mpz_sizeinbase(&a->z,10); |
---|
| 130 | if (a->s<2) l=si_max(l,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((ADDRESS)s,l); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|