| 1 |
#include "f2c.h" |
| 2 |
#include "fio.h" |
| 3 |
|
| 4 |
#ifndef KR_headers |
| 5 |
#undef abs |
| 6 |
#undef min |
| 7 |
#undef max |
| 8 |
#include "stdlib.h" |
| 9 |
#include "string.h" |
| 10 |
#endif |
| 11 |
|
| 12 |
#include "fmt.h" |
| 13 |
#include "fp.h" |
| 14 |
#ifndef VAX |
| 15 |
#include "ctype.h" |
| 16 |
#ifdef __cplusplus |
| 17 |
extern "C" { |
| 18 |
#endif |
| 19 |
#endif |
| 20 |
|
| 21 |
int |
| 22 |
#ifdef KR_headers |
| 23 |
wrt_E(p,w,d,e,len) ufloat *p; ftnlen len; |
| 24 |
#else |
| 25 |
wrt_E(ufloat *p, int w, int d, int e, ftnlen len) |
| 26 |
#endif |
| 27 |
{ |
| 28 |
char buf[FMAX+EXPMAXDIGS+4], *s, *se; |
| 29 |
int d1, delta, e1, i, sign, signspace; |
| 30 |
double dd; |
| 31 |
#ifdef WANT_LEAD_0 |
| 32 |
int insert0 = 0; |
| 33 |
#endif |
| 34 |
#ifndef VAX |
| 35 |
int e0 = e; |
| 36 |
#endif |
| 37 |
|
| 38 |
if(e <= 0) |
| 39 |
e = 2; |
| 40 |
if(f__scale) { |
| 41 |
if(f__scale >= d + 2 || f__scale <= -d) |
| 42 |
goto nogood; |
| 43 |
} |
| 44 |
if(f__scale <= 0) |
| 45 |
--d; |
| 46 |
if (len == sizeof(real)) |
| 47 |
dd = p->pf; |
| 48 |
else |
| 49 |
dd = p->pd; |
| 50 |
if (dd < 0.) { |
| 51 |
signspace = sign = 1; |
| 52 |
dd = -dd; |
| 53 |
} |
| 54 |
else { |
| 55 |
sign = 0; |
| 56 |
signspace = (int)f__cplus; |
| 57 |
#ifndef VAX |
| 58 |
if (!dd) { |
| 59 |
#ifdef SIGNED_ZEROS |
| 60 |
if (signbit_f2c(&dd)) |
| 61 |
signspace = sign = 1; |
| 62 |
#endif |
| 63 |
dd = 0.; /* avoid -0 */ |
| 64 |
} |
| 65 |
#endif |
| 66 |
} |
| 67 |
delta = w - (2 /* for the . and the d adjustment above */ |
| 68 |
+ 2 /* for the E+ */ + signspace + d + e); |
| 69 |
#ifdef WANT_LEAD_0 |
| 70 |
if (f__scale <= 0 && delta > 0) { |
| 71 |
delta--; |
| 72 |
insert0 = 1; |
| 73 |
} |
| 74 |
else |
| 75 |
#endif |
| 76 |
if (delta < 0) { |
| 77 |
nogood: |
| 78 |
while(--w >= 0) |
| 79 |
PUT('*'); |
| 80 |
return(0); |
| 81 |
} |
| 82 |
if (f__scale < 0) |
| 83 |
d += f__scale; |
| 84 |
if (d > FMAX) { |
| 85 |
d1 = d - FMAX; |
| 86 |
d = FMAX; |
| 87 |
} |
| 88 |
else |
| 89 |
d1 = 0; |
| 90 |
sprintf(buf,"%#.*E", d, dd); |
| 91 |
#ifndef VAX |
| 92 |
/* check for NaN, Infinity */ |
| 93 |
if (!isdigit(buf[0])) { |
| 94 |
switch(buf[0]) { |
| 95 |
case 'n': |
| 96 |
case 'N': |
| 97 |
signspace = 0; /* no sign for NaNs */ |
| 98 |
} |
| 99 |
delta = w - strlen(buf) - signspace; |
| 100 |
if (delta < 0) |
| 101 |
goto nogood; |
| 102 |
while(--delta >= 0) |
| 103 |
PUT(' '); |
| 104 |
if (signspace) |
| 105 |
PUT(sign ? '-' : '+'); |
| 106 |
for(s = buf; *s; s++) |
| 107 |
PUT(*s); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
#endif |
| 111 |
se = buf + d + 3; |
| 112 |
#ifdef GOOD_SPRINTF_EXPONENT /* When possible, exponent has 2 digits. */ |
| 113 |
if (f__scale != 1 && dd) |
| 114 |
sprintf(se, "%+.2d", atoi(se) + 1 - f__scale); |
| 115 |
#else |
| 116 |
if (dd) |
| 117 |
sprintf(se, "%+.2d", atoi(se) + 1 - f__scale); |
| 118 |
else |
| 119 |
strcpy(se, "+00"); |
| 120 |
#endif |
| 121 |
s = ++se; |
| 122 |
if (e < 2) { |
| 123 |
if (*s != '0') |
| 124 |
goto nogood; |
| 125 |
} |
| 126 |
#ifndef VAX |
| 127 |
/* accommodate 3 significant digits in exponent */ |
| 128 |
if (s[2]) { |
| 129 |
#ifdef Pedantic |
| 130 |
if (!e0 && !s[3]) |
| 131 |
for(s -= 2, e1 = 2; s[0] = s[1]; s++); |
| 132 |
|
| 133 |
/* Pedantic gives the behavior that Fortran 77 specifies, */ |
| 134 |
/* i.e., requires that E be specified for exponent fields */ |
| 135 |
/* of more than 3 digits. With Pedantic undefined, we get */ |
| 136 |
/* the behavior that Cray displays -- you get a bigger */ |
| 137 |
/* exponent field if it fits. */ |
| 138 |
#else |
| 139 |
if (!e0) { |
| 140 |
for(s -= 2, e1 = 2; s[0] = s[1]; s++) |
| 141 |
#ifdef CRAY |
| 142 |
delta--; |
| 143 |
if ((delta += 4) < 0) |
| 144 |
goto nogood |
| 145 |
#endif |
| 146 |
; |
| 147 |
} |
| 148 |
#endif |
| 149 |
else if (e0 >= 0) |
| 150 |
goto shift; |
| 151 |
else |
| 152 |
e1 = e; |
| 153 |
} |
| 154 |
else |
| 155 |
shift: |
| 156 |
#endif |
| 157 |
for(s += 2, e1 = 2; *s; ++e1, ++s) |
| 158 |
if (e1 >= e) |
| 159 |
goto nogood; |
| 160 |
while(--delta >= 0) |
| 161 |
PUT(' '); |
| 162 |
if (signspace) |
| 163 |
PUT(sign ? '-' : '+'); |
| 164 |
s = buf; |
| 165 |
i = f__scale; |
| 166 |
if (f__scale <= 0) { |
| 167 |
#ifdef WANT_LEAD_0 |
| 168 |
if (insert0) |
| 169 |
PUT('0'); |
| 170 |
#endif |
| 171 |
PUT('.'); |
| 172 |
for(; i < 0; ++i) |
| 173 |
PUT('0'); |
| 174 |
PUT(*s); |
| 175 |
s += 2; |
| 176 |
} |
| 177 |
else if (f__scale > 1) { |
| 178 |
PUT(*s); |
| 179 |
s += 2; |
| 180 |
while(--i > 0) |
| 181 |
PUT(*s++); |
| 182 |
PUT('.'); |
| 183 |
} |
| 184 |
if (d1) { |
| 185 |
se -= 2; |
| 186 |
while(s < se) PUT(*s++); |
| 187 |
se += 2; |
| 188 |
do PUT('0'); while(--d1 > 0); |
| 189 |
} |
| 190 |
while(s < se) |
| 191 |
PUT(*s++); |
| 192 |
if (e < 2) |
| 193 |
PUT(s[1]); |
| 194 |
else { |
| 195 |
while(++e1 <= e) |
| 196 |
PUT('0'); |
| 197 |
while(*s) |
| 198 |
PUT(*s++); |
| 199 |
} |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
int |
| 204 |
#ifdef KR_headers |
| 205 |
wrt_F(p,w,d,len) ufloat *p; ftnlen len; |
| 206 |
#else |
| 207 |
wrt_F(ufloat *p, int w, int d, ftnlen len) |
| 208 |
#endif |
| 209 |
{ |
| 210 |
int d1, sign, n; |
| 211 |
double x; |
| 212 |
char *b, buf[MAXINTDIGS+MAXFRACDIGS+4], *s; |
| 213 |
|
| 214 |
x= (len==sizeof(real)?p->pf:p->pd); |
| 215 |
if (d < MAXFRACDIGS) |
| 216 |
d1 = 0; |
| 217 |
else { |
| 218 |
d1 = d - MAXFRACDIGS; |
| 219 |
d = MAXFRACDIGS; |
| 220 |
} |
| 221 |
if (x < 0.) |
| 222 |
{ x = -x; sign = 1; } |
| 223 |
else { |
| 224 |
sign = 0; |
| 225 |
#ifndef VAX |
| 226 |
if (!x) { |
| 227 |
#ifdef SIGNED_ZEROS |
| 228 |
if (signbit_f2c(&x)) |
| 229 |
sign = 2; |
| 230 |
#endif |
| 231 |
x = 0.; |
| 232 |
} |
| 233 |
#endif |
| 234 |
} |
| 235 |
|
| 236 |
if (n = f__scale) |
| 237 |
if (n > 0) |
| 238 |
do x *= 10.; while(--n > 0); |
| 239 |
else |
| 240 |
do x *= 0.1; while(++n < 0); |
| 241 |
|
| 242 |
#ifdef USE_STRLEN |
| 243 |
sprintf(b = buf, "%#.*f", d, x); |
| 244 |
n = strlen(b) + d1; |
| 245 |
#else |
| 246 |
n = sprintf(b = buf, "%#.*f", d, x) + d1; |
| 247 |
#endif |
| 248 |
|
| 249 |
#ifndef WANT_LEAD_0 |
| 250 |
if (buf[0] == '0' && d) |
| 251 |
{ ++b; --n; } |
| 252 |
#endif |
| 253 |
if (sign == 1) { |
| 254 |
/* check for all zeros */ |
| 255 |
for(s = b;;) { |
| 256 |
while(*s == '0') s++; |
| 257 |
switch(*s) { |
| 258 |
case '.': |
| 259 |
s++; continue; |
| 260 |
case 0: |
| 261 |
sign = 0; |
| 262 |
} |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
if (sign || f__cplus) |
| 267 |
++n; |
| 268 |
if (n > w) { |
| 269 |
#ifdef WANT_LEAD_0 |
| 270 |
if (buf[0] == '0' && --n == w) |
| 271 |
++b; |
| 272 |
else |
| 273 |
#endif |
| 274 |
{ |
| 275 |
while(--w >= 0) |
| 276 |
PUT('*'); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
} |
| 280 |
for(w -= n; --w >= 0; ) |
| 281 |
PUT(' '); |
| 282 |
if (sign) |
| 283 |
PUT('-'); |
| 284 |
else if (f__cplus) |
| 285 |
PUT('+'); |
| 286 |
while(n = *b++) |
| 287 |
PUT(n); |
| 288 |
while(--d1 >= 0) |
| 289 |
PUT('0'); |
| 290 |
return 0; |
| 291 |
} |
| 292 |
#ifdef __cplusplus |
| 293 |
} |
| 294 |
#endif |