[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[341696] | 4 | /* $Id$ */ |
---|
[35aab3] | 5 | /* |
---|
| 6 | * ABSTRACT - |
---|
| 7 | * IO for long rational numbers (Hubert Grassmann) |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <string.h> |
---|
[2d805a] | 12 | #include <coeffs/config.h> |
---|
[31213a4] | 13 | #include <reporter/reporter.h> |
---|
[2d805a] | 14 | #include <coeffs/coeffs.h> |
---|
[31213a4] | 15 | #include <omalloc/omalloc.h> |
---|
[2d805a] | 16 | #include <coeffs/longrat.h> |
---|
| 17 | #include <coeffs/numbers.h> |
---|
[35aab3] | 18 | |
---|
| 19 | #define SR_HDL(A) ((long)(A)) |
---|
[2d2326] | 20 | //#define SR_INT 1 // already in longrat.h |
---|
[f12c611] | 21 | //#define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT)) |
---|
[35aab3] | 22 | #define SR_TO_INT(SR) (((long)SR) >> 2) |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | /*2 |
---|
| 26 | * extracts a long integer from s, returns the rest |
---|
| 27 | */ |
---|
[a604c3] | 28 | static const char * nlEatLong(char *s, mpz_ptr 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 | */ |
---|
[36f915] | 50 | const char * nlRead (const char *s, number *a, const coeffs r) |
---|
[35aab3] | 51 | { |
---|
| 52 | if (*s<'0' || *s>'9') |
---|
| 53 | { |
---|
[6c9007] | 54 | *a = INT_TO_SR(1); /* nlInit(1) */ |
---|
[35aab3] | 55 | return s; |
---|
| 56 | } |
---|
[896561] | 57 | *a=(number)ALLOC_RNUMBER(); |
---|
[35aab3] | 58 | { |
---|
| 59 | (*a)->s = 3; |
---|
| 60 | #if defined(LDEBUG) |
---|
| 61 | (*a)->debug=123456; |
---|
| 62 | #endif |
---|
[a604c3] | 63 | mpz_ptr z=(*a)->z; |
---|
| 64 | mpz_ptr n=(*a)->n; |
---|
[35aab3] | 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 | { |
---|
[f12c611] | 87 | mpz_clear(z); |
---|
[896561] | 88 | FREE_RNUMBER(*a); |
---|
[35aab3] | 89 | *a=INT_TO_SR(0); |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | if ((*a)->s==3) |
---|
| 93 | { |
---|
[f12c611] | 94 | number nlShort3_noinline(number x); |
---|
| 95 | *a=nlShort3_noinline(*a); |
---|
[35aab3] | 96 | } |
---|
| 97 | else |
---|
[896561] | 98 | { |
---|
| 99 | number aa=*a; |
---|
[a642c64] | 100 | nlNormalize(aa,r); |
---|
[896561] | 101 | *a=aa; |
---|
| 102 | } |
---|
[35aab3] | 103 | } |
---|
| 104 | return s; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /*2 |
---|
| 108 | * write a rational number |
---|
| 109 | */ |
---|
[36f915] | 110 | void nlWrite (number &a, const coeffs r) |
---|
[35aab3] | 111 | { |
---|
| 112 | char *s,*z; |
---|
| 113 | if (SR_HDL(a) & SR_INT) |
---|
| 114 | { |
---|
[f12c611] | 115 | StringAppend("%ld",SR_TO_INT(a)); |
---|
[35aab3] | 116 | } |
---|
| 117 | else if (a==NULL) |
---|
| 118 | { |
---|
| 119 | StringAppendS("o"); |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | if (a->s==0) |
---|
| 124 | { |
---|
[36f915] | 125 | nlNormalize(a,r); |
---|
[493225] | 126 | nlWrite(a,r); |
---|
[35aab3] | 127 | return; |
---|
| 128 | } |
---|
[a604c3] | 129 | int l=mpz_sizeinbase(a->z,10); |
---|
[32c80f] | 130 | if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10)); |
---|
[35aab3] | 131 | l+=2; |
---|
| 132 | s=(char*)omAlloc(l); |
---|
[a604c3] | 133 | z=mpz_get_str(s,10,a->z); |
---|
[35aab3] | 134 | StringAppendS(z); |
---|
| 135 | if (a->s!=3) |
---|
| 136 | { |
---|
| 137 | StringAppendS("/"); |
---|
[a604c3] | 138 | z=mpz_get_str(s,10,a->n); |
---|
[35aab3] | 139 | StringAppendS(z); |
---|
| 140 | } |
---|
[7d90aa] | 141 | omFreeSize((void *)s,l); |
---|
[35aab3] | 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|