Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
wref.c
Go to the documentation of this file.00001 #include "stdlib.h"
00002 #include "f2c.h"
00003 #include "fio.h"
00004 #include "fmt.h"
00005 #include "fp.h"
00006 #ifndef VAX
00007 #include "ctype.h"
00008 #endif
00009
00010 #ifndef KR_headers
00011 #undef abs
00012 #undef min
00013 #undef max
00014 #include "stdlib.h"
00015 #include "string.h"
00016 #endif
00017
00018 #ifdef KR_headers
00019 wrt_E(p,w,d,e,len) ufloat *p; ftnlen len;
00020 #else
00021 wrt_E(ufloat *p, int w, int d, int e, ftnlen len)
00022 #endif
00023 {
00024 char buf[FMAX+EXPMAXDIGS+4], *s, *se;
00025 int d1, delta, e1, i, sign, signspace;
00026 double dd;
00027 #ifdef WANT_LEAD_0
00028 int insert0 = 0;
00029 #endif
00030 #ifndef VAX
00031 int e0 = e;
00032 #endif
00033
00034 if(e <= 0)
00035 e = 2;
00036 if(f__scale) {
00037 if(f__scale >= d + 2 || f__scale <= -d)
00038 goto nogood;
00039 }
00040 if(f__scale <= 0)
00041 --d;
00042 if (len == sizeof(real))
00043 dd = p->pf;
00044 else
00045 dd = p->pd;
00046 if (dd < 0.) {
00047 signspace = sign = 1;
00048 dd = -dd;
00049 }
00050 else {
00051 sign = 0;
00052 signspace = (int)f__cplus;
00053 #ifndef VAX
00054 if (!dd)
00055 dd = 0.;
00056 #endif
00057 }
00058 delta = w - (2
00059 + 2 + signspace + d + e);
00060 #ifdef WANT_LEAD_0
00061 if (f__scale <= 0 && delta > 0) {
00062 delta--;
00063 insert0 = 1;
00064 }
00065 else
00066 #endif
00067 if (delta < 0) {
00068 nogood:
00069 while(--w >= 0)
00070 PUT('*');
00071 return(0);
00072 }
00073 if (f__scale < 0)
00074 d += f__scale;
00075 if (d > FMAX) {
00076 d1 = d - FMAX;
00077 d = FMAX;
00078 }
00079 else
00080 d1 = 0;
00081 sprintf(buf,"%#.*E", d, dd);
00082 #ifndef VAX
00083
00084 if (!isdigit(buf[0])) {
00085 switch(buf[0]) {
00086 case 'n':
00087 case 'N':
00088 signspace = 0;
00089 }
00090 delta = w - strlen(buf) - signspace;
00091 if (delta < 0)
00092 goto nogood;
00093 while(--delta >= 0)
00094 PUT(' ');
00095 if (signspace)
00096 PUT(sign ? '-' : '+');
00097 for(s = buf; *s; s++)
00098 PUT(*s);
00099 return 0;
00100 }
00101 #endif
00102 se = buf + d + 3;
00103 #ifdef GOOD_SPRINTF_EXPONENT
00104 if (f__scale != 1 && dd)
00105 sprintf(se, "%+.2d", atoi(se) + 1 - f__scale);
00106 #else
00107 if (dd)
00108 sprintf(se, "%+.2d", atoi(se) + 1 - f__scale);
00109 else
00110 strcpy(se, "+00");
00111 #endif
00112 s = ++se;
00113 if (e < 2) {
00114 if (*s != '0')
00115 goto nogood;
00116 }
00117 #ifndef VAX
00118
00119 if (s[2]) {
00120 #ifdef Pedantic
00121 if (!e0 && !s[3])
00122 for(s -= 2, e1 = 2; s[0] = s[1]; s++);
00123
00124
00125
00126
00127
00128
00129 #else
00130 if (!e0) {
00131 for(s -= 2, e1 = 2; s[0] = s[1]; s++)
00132 #ifdef CRAY
00133 delta--;
00134 if ((delta += 4) < 0)
00135 goto nogood
00136 #endif
00137 ;
00138 }
00139 #endif
00140 else if (e0 >= 0)
00141 goto shift;
00142 else
00143 e1 = e;
00144 }
00145 else
00146 shift:
00147 #endif
00148 for(s += 2, e1 = 2; *s; ++e1, ++s)
00149 if (e1 >= e)
00150 goto nogood;
00151 while(--delta >= 0)
00152 PUT(' ');
00153 if (signspace)
00154 PUT(sign ? '-' : '+');
00155 s = buf;
00156 i = f__scale;
00157 if (f__scale <= 0) {
00158 #ifdef WANT_LEAD_0
00159 if (insert0)
00160 PUT('0');
00161 #endif
00162 PUT('.');
00163 for(; i < 0; ++i)
00164 PUT('0');
00165 PUT(*s);
00166 s += 2;
00167 }
00168 else if (f__scale > 1) {
00169 PUT(*s);
00170 s += 2;
00171 while(--i > 0)
00172 PUT(*s++);
00173 PUT('.');
00174 }
00175 if (d1) {
00176 se -= 2;
00177 while(s < se) PUT(*s++);
00178 se += 2;
00179 do PUT('0'); while(--d1 > 0);
00180 }
00181 while(s < se)
00182 PUT(*s++);
00183 if (e < 2)
00184 PUT(s[1]);
00185 else {
00186 while(++e1 <= e)
00187 PUT('0');
00188 while(*s)
00189 PUT(*s++);
00190 }
00191 return 0;
00192 }
00193
00194 #ifdef KR_headers
00195 wrt_F(p,w,d,len) ufloat *p; ftnlen len;
00196 #else
00197 wrt_F(ufloat *p, int w, int d, ftnlen len)
00198 #endif
00199 {
00200 int d1, sign, n;
00201 double x;
00202 char *b, buf[MAXINTDIGS+MAXFRACDIGS+4], *s;
00203
00204 x= (len==sizeof(real)?p->pf:p->pd);
00205 if (d < MAXFRACDIGS)
00206 d1 = 0;
00207 else {
00208 d1 = d - MAXFRACDIGS;
00209 d = MAXFRACDIGS;
00210 }
00211 if (x < 0.)
00212 { x = -x; sign = 1; }
00213 else {
00214 sign = 0;
00215 #ifndef VAX
00216 if (!x)
00217 x = 0.;
00218 #endif
00219 }
00220
00221 if (n = f__scale)
00222 if (n > 0)
00223 do x *= 10.; while(--n > 0);
00224 else
00225 do x *= 0.1; while(++n < 0);
00226
00227 #ifdef USE_STRLEN
00228 sprintf(b = buf, "%#.*f", d, x);
00229 n = strlen(b) + d1;
00230 #else
00231 n = sprintf(b = buf, "%#.*f", d, x) + d1;
00232 #endif
00233
00234 #ifndef WANT_LEAD_0
00235 if (buf[0] == '0' && d)
00236 { ++b; --n; }
00237 #endif
00238 if (sign) {
00239
00240 for(s = b;;) {
00241 while(*s == '0') s++;
00242 switch(*s) {
00243 case '.':
00244 s++; continue;
00245 case 0:
00246 sign = 0;
00247 }
00248 break;
00249 }
00250 }
00251 if (sign || f__cplus)
00252 ++n;
00253 if (n > w) {
00254 #ifdef WANT_LEAD_0
00255 if (buf[0] == '0' && --n == w)
00256 ++b;
00257 else
00258 #endif
00259 {
00260 while(--w >= 0)
00261 PUT('*');
00262 return 0;
00263 }
00264 }
00265 for(w -= n; --w >= 0; )
00266 PUT(' ');
00267 if (sign)
00268 PUT('-');
00269 else if (f__cplus)
00270 PUT('+');
00271 while(n = *b++)
00272 PUT(n);
00273 while(--d1 >= 0)
00274 PUT('0');
00275 return 0;
00276 }