[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* $Id: longrat0.cc,v 1.1.1.1 2003-10-06 12:15:54 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 |
---|
| 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 | */ |
---|
| 27 | char * 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 | */ |
---|
| 49 | char * nlRead (char *s, number *a) |
---|
| 50 | { |
---|
| 51 | if (*s<'0' || *s>'9') |
---|
| 52 | { |
---|
| 53 | *a = 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 | */ |
---|
| 109 | void 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 | |
---|