Debian Codegraph @

packages/seacas/libraries/suplib/numstr1.f

raw
1 C Copyright(C) 1999-2020 National Technology & Engineering Solutions
2 C of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3 C NTESS, the U.S. Government retains certain rights in this software.
4 C
5 C See packages/seacas/LICENSE for details
6 C=======================================================================
7 SUBROUTINE NUMSTR1 (NSIG, RNUM, RSTR, LSTR)
8 C=======================================================================
9
10 C --*** NUMSTR *** (STRLIB) Convert real numbers to strings
11 C --
12 C --NUMSTR converts a set of real numbers into a consistent set of
13 C --strings. It will convert to engineering notation with all
14 C --exponents the same, if possible.
15 C --
16 C --Parameters:
17 C -- NSIG - IN - the maximum number of significant digits, max of 8
18 C -- RNUM - IN - the array of real numbers to be converted
19 C -- RSTR - OUT - the set of real number strings
20 C -- LSTR - OUT - the maximum length of the number strings
21
22 C --Routines Called:
23 C -- IENGRX - (included) Get engineering notation exponent
24
25 INTEGER NSIG
26 REAL RNUM
27 CHARACTER*(*) RSTR
28 INTEGER LSTR
29
30 CHARACTER*20 BLANKS
31 CHARACTER*10 SCRFMT
32 CHARACTER*20 SCRSTR
33 CHARACTER*20 TMPSTR
34 CHARACTER*15 FFMT
35
36 C --Convert all to E notation and find the minimum and maximum exponent
37 C -- MINE and MAXE are the minimum and maximum exponents
38 C -- ISIGN is the number of digits for the sign
39 C -- (0 if all positive, 1 if any number negative)
40
41 BLANKS = ' '
42
43 WRITE (SCRFMT, 10000, IOSTAT=IDUM) NSIG+7, NSIG
44 10000 FORMAT ('(0PE', I2.2, '.', I2.2, ')')
45
46 ISIGN = 0
47 MINE = 9999
48 MINE2 = 9999
49 MAXE = -9999
50 MAXES = MAXE
51 IF (RNUM .NE. 0.0) THEN
52 WRITE (SCRSTR(1:NSIG+7), SCRFMT, IOSTAT=IDUM) RNUM
53 READ (SCRSTR(NSIG+5:NSIG+7), '(I3)', IOSTAT=IDUM) IE
54 IF (MINE .GT. IE) MINE2 = MINE
55 MINE = MIN (MINE, IE)
56 MAXE = MAX (MAXE, IE)
57 IF (RNUM .LT. 0.0) THEN
58 ISIGN = 1
59 MAXES = MAX (MAXES, IE)
60 END IF
61 END IF
62
63 C --Correct for one very small number (should be zero)
64
65 IF ((MINE2 .LT. 1000) .AND. ((MINE2 - MINE) .GE. 6)) MINE = MINE2
66
67 C --Handle all zero case
68
69 IF (MINE .GT. MAXE) THEN
70 MINE = 0
71 MAXE = 0
72 MAXES = 0
73 END IF
74
75 C --Determine the new exponent NEWEXP (use engineering notation)
76
77 NEWEXP = IENGRX (MAXE, MINE)
78 IF (ISIGN .EQ. 1) THEN
79 IF (MAX (1, MAXE - NEWEXP) .GT. MAX (1, MAXES - NEWEXP))
80 & ISIGN = 0
81 END IF
82
83 C --Check if the numbers can all be sensibly converted to a common exponent
84
85 IF (((MAXE - NEWEXP) .LE. 4)
86 & .AND. ((NEWEXP - MINE) .LE. 2)
87 & .AND. (-MINE .LT. (NSIG - MAXE))) THEN
88
89 C --Determine the new F format
90 C -- EXPDIV is the number to divide by to get the number
91 C -- without an exponent
92 C -- NWHOLE is the number of digits before the decimal
93 C -- NFRAC is the number of digits after the decimal
94 C -- NTOTAL is the total number of digits
95 C --The new exponent is tagged on the end of the F-format number
96
97 EXPDIV = 10.0 ** NEWEXP
98
99 NWHOLE = MAX (1, MAXE - NEWEXP)
100 NFRAC = MAX (0, MIN (NEWEXP - MINE + NSIG,
101 & NSIG - (MAXE - NEWEXP)))
102 NTOTAL = ISIGN + NWHOLE + 1 + NFRAC
103 IF (EXPDIV .NE. 0.0) THEN
104 WRITE (FFMT, 10010, IOSTAT=IDUM) NTOTAL, NFRAC
105 10010 FORMAT ('(F', I2.2, '.', I2.2, ')')
106 ELSE
107 WRITE (FFMT, 10020, IOSTAT=IDUM) NTOTAL
108 10020 FORMAT ('(A', I2.2, 3X, ')')
109 END IF
110
111 IF (NEWEXP .EQ. 0) THEN
112 LSTR = NTOTAL
113 ELSE IF ((NEWEXP .LE. -10) .OR. (NEWEXP .GE. 10)) THEN
114 WRITE (FFMT(8:15), 10030, IOSTAT=IDUM) NEWEXP
115 10030 FORMAT (',''E', SP, I3.2, ''')')
116 LSTR = NTOTAL + 4
117 ELSE
118 WRITE (FFMT(8:15), 10040, IOSTAT=IDUM) NEWEXP
119 10040 FORMAT (',''E', SP, I2.1, ''')')
120 LSTR = NTOTAL + 3
121 END IF
122
123 C --Convert all numbers to the new exponent by using the F format
124
125 IF (EXPDIV .NE. 0.0) THEN
126 WRITE (RSTR, FFMT, IOSTAT=IDUM) RNUM/EXPDIV
127 if (rstr(:1) .eq. '*') then
128 C ... Roundoff occurred. Adjust format and try again...
129 IF (EXPDIV .NE. 0.0) THEN
130 WRITE (FFMT(:7), 10010, IOSTAT=IDUM) NTOTAL,
131 $ NFRAC-1
132 WRITE (RSTR, FFMT, IOSTAT=IDUM) RNUM/EXPDIV
133 end if
134 end if
135 ELSE
136 WRITE (RSTR, FFMT, IOSTAT=IDUM) '********************'
137 END IF
138
139 ELSE
140
141 C --Do not try to use a common exponent, but use engineering notation;
142 C --Algorithm as above
143
144 LSTR = 0
145 MINEXP = IENGRX (MINE, MINE)
146 MAXEXP = IENGRX (MAXE, MAXE)
147
148 WRITE (SCRSTR(1:NSIG+7), SCRFMT, IOSTAT=IDUM) RNUM
149 READ (SCRSTR(NSIG+5:NSIG+7), '(I3)', IOSTAT=IDUM) IE
150 ISIGN = 0
151 IF (RNUM .LT. 0.0) ISIGN = 1
152
153 NEWEXP = IENGRX (IE, IE)
154
155 EXPDIV = 10.0 ** NEWEXP
156
157 NWHOLE = MAX (1, IE - NEWEXP)
158 NFRAC = MAX (0, MIN (NEWEXP - IE + NSIG,
159 & NSIG - (IE - NEWEXP)))
160 IF ((RNUM .EQ. 0.0) .AND. (MINE .GE. 0))
161 & NFRAC = NFRAC - 1
162 NTOTAL = ISIGN + NWHOLE + 1 + NFRAC
163 IF (EXPDIV .NE. 0.0) THEN
164 WRITE (FFMT, 10010, IOSTAT=IDUM) NTOTAL, NFRAC
165 ELSE
166 WRITE (FFMT, 10020, IOSTAT=IDUM) NTOTAL
167 END IF
168
169 IF ((MINEXP .LE. -10) .OR. (MAXEXP .GE. 10)) THEN
170 WRITE (FFMT(8:15), 10030, IOSTAT=IDUM) NEWEXP
171 LSTR = MAX (LSTR, NTOTAL + 4)
172 ELSE
173 WRITE (FFMT(8:15), 10040, IOSTAT=IDUM) NEWEXP
174 LSTR = MAX (LSTR, NTOTAL + 3)
175 END IF
176
177 IF (EXPDIV .NE. 0.0) THEN
178 WRITE (RSTR, FFMT, IOSTAT=IDUM) RNUM/EXPDIV
179 if (rstr(:1) .eq. '*') then
180 C ... Roundoff occurred. Adjust format and try again...
181 IF (EXPDIV .NE. 0.0) THEN
182 WRITE (FFMT(:7), 10010, IOSTAT=IDUM) NTOTAL,
183 $ NFRAC-1
184 WRITE (RSTR, FFMT, IOSTAT=IDUM) RNUM/EXPDIV
185 end if
186 end if
187 ELSE
188 WRITE (RSTR, FFMT, IOSTAT=IDUM) '********************'
189 END IF
190
191 C --Adjust the strings so that they are right-justified at
192 C --a common length
193
194 IB = INDEX (RSTR(:LSTR), ' ')
195 IF (IB .GT. 0) THEN
196 NB = LSTR - IB + 1
197 TMPSTR = RSTR(:IB-1)
198 RSTR = BLANKS(:NB) // TMPSTR
199 END IF
200
201 END IF
202
203 RETURN
204 END