-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmtdp.f
125 lines (125 loc) · 4.25 KB
/
fmtdp.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2016 Daniel Everhart
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject
* to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************
* FMTDP: Format a DOUBLE PRECISION value to fit in buffer with max
* precision.
************************************************************************
* VAL * I * DOUBLE PRECISION value to format
* WD * I * Desired field width
* BUFF * O * Buffer in which to write formatted value
************************************************************************
* Error will result in writing '*' characters to the buffer.
* The format for real numbers is:
* Fw.d
* Ew.dEe
* Gw.d
* W is the total width, D is the number of digits to the right of
* the floating point. This subroutine starts with the total width,
* and figures out how much width is allowaded for D.
************************************************************************
* W * 'w' of Gw.d format descriptor
* D * 'd' of Gw.d format descriptor
* E * 'e' of Ew.dEe format descriptor
* IOS * IOSTAT for read/write operations
* FIOS * IOSTAT for reading FVAL from TBUF
* EIOS * IOSTAT for reading EVAL from TBUF
* FSTR * Fw.d format string
* ESTR * Ew.dEe format string
* TBUF * Temporary buffer for writing formated real value
* FVAL * Value read from buffer based of Fw.d format string
* EVAL * Value read from buffer based of Ew.d format string
************************************************************************
SUBROUTINE FMTDP(VAL, BUFF, WD)
IMPLICIT NONE
DOUBLE PRECISION VAL
CHARACTER*(*) BUFF
INTEGER WD
*
INTEGER W, D, E, POW10
INTEGER IOS, FIOS, EIOS
CHARACTER*16 FSTR, ESTR
CHARACTER*32 TBUF
DOUBLE PRECISION FVAL, EVAL
*
FVAL = 0D0
EVAL = 0D0
W = MIN(LEN(BUFF), WD)
DO 100 D = 1, W
100 BUFF(D:D) = '*'
*
** Special case when value is 0D0
*
IF (VAL.EQ.0D0) THEN
IF (WD.EQ.1) THEN
BUFF = '.'
ELSEIF (WD.EQ.2) THEN
BUFF = '.0'
ELSE
WRITE(FSTR,920, IOSTAT=IOS) W, W-2
WRITE(BUFF, FSTR, IOSTAT=IOS) VAL
ENDIF
RETURN
ENDIF
*
** Attempt to write value using Fw.d
*
D = W
TBUF = '*'
DO 200 WHILE (TBUF(1:1).EQ.'*')
D = D - 1
IF (D.LT.0) EXIT
WRITE(FSTR,920, IOSTAT=IOS) W, D
200 WRITE(TBUF, FSTR, IOSTAT=IOS) VAL
READ(TBUF,*,IOSTAT=FIOS) FVAL
*
** Attempt to write value using Ew.d
*
D = W
E = INT(LOG(REAL(INT(LOG(VAL)))))
TBUF = '*'
DO 300 WHILE (TBUF(1:1).EQ.'*')
D = D - 1
IF (D.LT.0) EXIT
WRITE(ESTR,930, IOSTAT=IOS) W, D, E
300 WRITE(TBUF, ESTR, IOSTAT=IOS) VAL
READ(TBUF,*,IOSTAT=EIOS) EVAL
*
** Determine which method yields a value which is nearest the
** original value.
*
IF(FIOS.NE.0) THEN
WRITE(BUFF, ESTR, IOSTAT=IOS) VAL
ELSE
IF (ABS(EVAL-VAL).GE.ABS(FVAL-VAL)) THEN
WRITE(BUFF, FSTR, IOSTAT=IOS) VAL
ELSE
WRITE(BUFF, ESTR, IOSTAT=IOS) VAL
END IF
END IF
*
RETURN
920 FORMAT('(F'I2'.'I2')')
930 FORMAT('(E'I2'.'I2'E'I1')')
END SUBROUTINE FMTDP