[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT - |
---|
| 6 | * IO for long rational numbers (Hubert Grassmann) |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | #include <stdio.h> |
---|
| 10 | #include <string.h> |
---|
[d663f2] | 11 | |
---|
[691dba] | 12 | #include "config.h" |
---|
[d663f2] | 13 | #include <misc/auxiliary.h> |
---|
| 14 | |
---|
[31213a4] | 15 | #include <omalloc/omalloc.h> |
---|
[d663f2] | 16 | #include <reporter/reporter.h> |
---|
| 17 | |
---|
| 18 | #include "coeffs.h" |
---|
| 19 | #include "numbers.h" |
---|
| 20 | |
---|
| 21 | #include "longrat.h" |
---|
| 22 | |
---|
| 23 | /// Our Type! |
---|
| 24 | static const n_coeffType ID = n_Q; |
---|
| 25 | |
---|
| 26 | //#ifndef SI_THREADS |
---|
| 27 | omBin rnumber_bin = omGetSpecBin(sizeof(snumber)); // TODO: move this into coeffs-struct (for Q)?! |
---|
| 28 | //#endif |
---|
| 29 | |
---|
[35aab3] | 30 | |
---|
| 31 | #define SR_HDL(A) ((long)(A)) |
---|
[2d2326] | 32 | //#define SR_INT 1 // already in longrat.h |
---|
[f12c611] | 33 | //#define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT)) |
---|
[35aab3] | 34 | #define SR_TO_INT(SR) (((long)SR) >> 2) |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | /*2 |
---|
| 38 | * extracts a long integer from s, returns the rest |
---|
| 39 | */ |
---|
[a604c3] | 40 | static const char * nlEatLong(char *s, mpz_ptr i) |
---|
[35aab3] | 41 | { |
---|
[85e68dd] | 42 | const char * start=s; |
---|
[35aab3] | 43 | |
---|
| 44 | while (*s >= '0' && *s <= '9') s++; |
---|
| 45 | if (*s=='\0') |
---|
| 46 | { |
---|
| 47 | mpz_set_str(i,start,10); |
---|
| 48 | } |
---|
| 49 | else |
---|
| 50 | { |
---|
| 51 | char c=*s; |
---|
| 52 | *s='\0'; |
---|
| 53 | mpz_set_str(i,start,10); |
---|
| 54 | *s=c; |
---|
| 55 | } |
---|
| 56 | return s; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /*2 |
---|
| 60 | * extracts the number a from s, returns the rest |
---|
| 61 | */ |
---|
[36f915] | 62 | const char * nlRead (const char *s, number *a, const coeffs r) |
---|
[35aab3] | 63 | { |
---|
| 64 | if (*s<'0' || *s>'9') |
---|
| 65 | { |
---|
[6c9007] | 66 | *a = INT_TO_SR(1); /* nlInit(1) */ |
---|
[35aab3] | 67 | return s; |
---|
| 68 | } |
---|
[896561] | 69 | *a=(number)ALLOC_RNUMBER(); |
---|
[35aab3] | 70 | { |
---|
| 71 | (*a)->s = 3; |
---|
| 72 | #if defined(LDEBUG) |
---|
| 73 | (*a)->debug=123456; |
---|
| 74 | #endif |
---|
[a604c3] | 75 | mpz_ptr z=(*a)->z; |
---|
| 76 | mpz_ptr n=(*a)->n; |
---|
[35aab3] | 77 | mpz_init(z); |
---|
[85e68dd] | 78 | s = nlEatLong((char *)s, z); |
---|
[35aab3] | 79 | if (*s == '/') |
---|
| 80 | { |
---|
| 81 | mpz_init(n); |
---|
| 82 | (*a)->s = 0; |
---|
| 83 | s++; |
---|
[85e68dd] | 84 | s = nlEatLong((char *)s, n); |
---|
[35aab3] | 85 | if (mpz_cmp_si(n,(long)0)==0) |
---|
| 86 | { |
---|
[7c961ba] | 87 | WerrorS(nDivBy0); |
---|
[35aab3] | 88 | mpz_clear(n); |
---|
| 89 | (*a)->s = 3; |
---|
| 90 | } |
---|
| 91 | else if (mpz_cmp_si(n,(long)1)==0) |
---|
| 92 | { |
---|
| 93 | mpz_clear(n); |
---|
| 94 | (*a)->s=3; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | if (mpz_cmp_si(z,(long)0)==0) |
---|
| 98 | { |
---|
[f12c611] | 99 | mpz_clear(z); |
---|
[896561] | 100 | FREE_RNUMBER(*a); |
---|
[35aab3] | 101 | *a=INT_TO_SR(0); |
---|
| 102 | } |
---|
| 103 | else |
---|
| 104 | if ((*a)->s==3) |
---|
| 105 | { |
---|
[f12c611] | 106 | number nlShort3_noinline(number x); |
---|
| 107 | *a=nlShort3_noinline(*a); |
---|
[35aab3] | 108 | } |
---|
| 109 | else |
---|
[896561] | 110 | { |
---|
| 111 | number aa=*a; |
---|
[a642c64] | 112 | nlNormalize(aa,r); |
---|
[896561] | 113 | *a=aa; |
---|
| 114 | } |
---|
[35aab3] | 115 | } |
---|
| 116 | return s; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /*2 |
---|
| 120 | * write a rational number |
---|
| 121 | */ |
---|
[36f915] | 122 | void nlWrite (number &a, const coeffs r) |
---|
[35aab3] | 123 | { |
---|
| 124 | char *s,*z; |
---|
| 125 | if (SR_HDL(a) & SR_INT) |
---|
| 126 | { |
---|
[f12c611] | 127 | StringAppend("%ld",SR_TO_INT(a)); |
---|
[35aab3] | 128 | } |
---|
| 129 | else if (a==NULL) |
---|
| 130 | { |
---|
| 131 | StringAppendS("o"); |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | if (a->s==0) |
---|
| 136 | { |
---|
[36f915] | 137 | nlNormalize(a,r); |
---|
[493225] | 138 | nlWrite(a,r); |
---|
[35aab3] | 139 | return; |
---|
| 140 | } |
---|
[a604c3] | 141 | int l=mpz_sizeinbase(a->z,10); |
---|
[32c80f] | 142 | if (a->s<2) l=si_max(l,(int)mpz_sizeinbase(a->n,10)); |
---|
[35aab3] | 143 | l+=2; |
---|
| 144 | s=(char*)omAlloc(l); |
---|
[a604c3] | 145 | z=mpz_get_str(s,10,a->z); |
---|
[35aab3] | 146 | StringAppendS(z); |
---|
| 147 | if (a->s!=3) |
---|
| 148 | { |
---|
| 149 | StringAppendS("/"); |
---|
[a604c3] | 150 | z=mpz_get_str(s,10,a->n); |
---|
[35aab3] | 151 | StringAppendS(z); |
---|
| 152 | } |
---|
[7d90aa] | 153 | omFreeSize((void *)s,l); |
---|
[35aab3] | 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
[d663f2] | 157 | |
---|