Commit | Line | Data |
---|---|---|
959f3c4c JH |
1 | ?RCS: $Id: d_gconvert.U,v 3.0.1.3 1997/02/28 15:33:38 ram Exp $ |
2 | ?RCS: | |
3 | ?RCS: Copyright (c) 1991-1993, Raphael Manfredi | |
0065f4af | 4 | ?RCS: |
959f3c4c JH |
5 | ?RCS: You may redistribute only under the terms of the Artistic Licence, |
6 | ?RCS: as specified in the README file that comes with the distribution. | |
7 | ?RCS: You may reuse parts of this distribution only within the terms of | |
8 | ?RCS: that same Artistic Licence; a copy of which may be found at the root | |
9 | ?RCS: of the source tree for dist 3.0. | |
10 | ?RCS: | |
11 | ?RCS: Original Author: Andy Dougherty <doughera@lafcol.lafayette.edu> | |
12 | ?RCS: | |
13 | ?RCS: $Log: d_gconvert.U,v $ | |
14 | ?RCS: Revision 3.0.1.3 1997/02/28 15:33:38 ram | |
15 | ?RCS: patch61: integrated new unit from perl5 | |
16 | ?RCS: | |
17 | ?RCS: Revision 3.0.1.2 1995/07/25 13:55:59 ram | |
18 | ?RCS: patch56: improved comments about the Gconvert macro (ADO) | |
19 | ?RCS: patch56: force compile-link test since it may exist but be unusable (ADO) | |
20 | ?RCS: | |
21 | ?RCS: Revision 3.0.1.1 1994/10/29 16:12:51 ram | |
22 | ?RCS: patch36: created by ADO | |
23 | ?RCS: | |
fd2d81e4 | 24 | ?MAKE:d_Gconvert: \ |
40501436 | 25 | Compile cat Inlibc rm _o rm_try i_stdlib i_string d_qgcvt run \ |
86ff0982 | 26 | uselongdouble d_longdbl d_PRIgldbl sPRIgldbl |
959f3c4c JH |
27 | ?MAKE: -pick add $@ %< |
28 | ?S:d_Gconvert: | |
29 | ?S: This variable holds what Gconvert is defined as to convert | |
f8a7e493 JH |
30 | ?S: floating point numbers into strings. By default, Configure |
31 | ?S: sets this macro to use the first of gconvert, gcvt, or sprintf | |
32 | ?S: that pass sprintf-%g-like behaviour tests. If perl is using | |
33 | ?S: long doubles, the macro uses the first of the following | |
34 | ?S: functions that pass Configure's tests: qgcvt, sprintf (if | |
35 | ?S: Configure knows how to make sprintf format long doubles--see | |
2cb64bf6 MB |
36 | ?S: sPRIgldbl), gconvert, gcvt, and sprintf (casting to double). |
37 | ?S: The gconvert_preference and gconvert_ld_preference variables | |
f8a7e493 | 38 | ?S: can be used to alter Configure's preferences, for doubles and |
2cb64bf6 MB |
39 | ?S: long doubles, respectively. If present, they contain a |
40 | ?S: space-separated list of one or more of the above function | |
f8a7e493 JH |
41 | ?S: names in the order they should be tried. |
42 | ?S: | |
43 | ?S: d_Gconvert may be set to override Configure with a platform- | |
44 | ?S: specific function. If this function expects a double, a | |
45 | ?S: different value may need to be set by the uselongdouble.cbu | |
46 | ?S: call-back unit so that long doubles can be formatted without | |
47 | ?S: loss of precision. | |
959f3c4c JH |
48 | ?S:. |
49 | ?C:Gconvert: | |
50 | ?C: This preprocessor macro is defined to convert a floating point | |
51 | ?C: number to a string without a trailing decimal point. This | |
52 | ?C: emulates the behavior of sprintf("%g"), but is sometimes much more | |
53 | ?C: efficient. If gconvert() is not available, but gcvt() drops the | |
54 | ?C: trailing decimal point, then gcvt() is used. If all else fails, | |
55 | ?C: a macro using sprintf("%g") is used. Arguments for the Gconvert | |
56 | ?C: macro are: value, number of digits, whether trailing zeros should | |
57 | ?C: be retained, and the output buffer. | |
fd2d81e4 | 58 | ?C: The usual values are: |
959f3c4c JH |
59 | ?C: d_Gconvert='gconvert((x),(n),(t),(b))' |
60 | ?C: d_Gconvert='gcvt((x),(n),(b))' | |
61 | ?C: d_Gconvert='sprintf((b),"%.*g",(n),(x))' | |
62 | ?C: The last two assume trailing zeros should not be kept. | |
63 | ?C:. | |
64 | ?H:#define Gconvert(x,n,t,b) $d_Gconvert | |
65 | ?H:. | |
fd2d81e4 | 66 | ?T: xxx_list xxx_convert xxx_ld_list |
0065f4af | 67 | ?F:!try |
fd2d81e4 JH |
68 | ?LINT:extern gconvert_preference |
69 | ?LINT:extern gconvert_ld_preference | |
959f3c4c | 70 | : Check how to convert floats to strings. |
fd2d81e4 JH |
71 | |
72 | if test "X$d_Gconvert" = X; then | |
73 | ||
de47c9df JH |
74 | echo " " |
75 | echo "Checking for an efficient way to convert floats to strings." | |
76 | echo " " > try.c | |
77 | case "$uselongdouble" in | |
78 | "$define") echo "#define USE_LONG_DOUBLE" >>try.c ;; | |
79 | esac | |
80 | case "$d_longdbl" in | |
81 | "$define") echo "#define HAS_LONG_DOUBLE" >>try.c ;; | |
82 | esac | |
83 | case "$d_PRIgldbl" in | |
84 | "$define") echo "#define HAS_PRIgldbl" >>try.c ;; | |
85 | esac | |
86 | $cat >>try.c <<EOP | |
959f3c4c JH |
87 | #ifdef TRY_gconvert |
88 | #define Gconvert(x,n,t,b) gconvert((x),(n),(t),(b)) | |
89 | char *myname = "gconvert"; | |
90 | #endif | |
91 | #ifdef TRY_gcvt | |
92 | #define Gconvert(x,n,t,b) gcvt((x),(n),(b)) | |
93 | char *myname = "gcvt"; | |
94 | #endif | |
edd6115f JH |
95 | #ifdef TRY_qgcvt |
96 | #define Gconvert(x,n,t,b) qgcvt((x),(n),(b)) | |
97 | char *myname = "qgcvt"; | |
65179b48 | 98 | #define DOUBLETYPE long double |
edd6115f | 99 | #endif |
959f3c4c | 100 | #ifdef TRY_sprintf |
fd2d81e4 JH |
101 | #if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE) |
102 | #ifdef HAS_PRIgldbl | |
86ff0982 JH |
103 | #define Gconvert(x,n,t,b) sprintf((b),"%.*"$sPRIgldbl,(n),(x)) |
104 | #else | |
fd2d81e4 JH |
105 | #define Gconvert(x,n,t,b) sprintf((b),"%.*g",(n),(double)(x)) |
106 | #endif | |
107 | #else | |
959f3c4c | 108 | #define Gconvert(x,n,t,b) sprintf((b),"%.*g",(n),(x)) |
86ff0982 | 109 | #endif |
959f3c4c JH |
110 | char *myname = "sprintf"; |
111 | #endif | |
112 | ||
65179b48 | 113 | #ifndef DOUBLETYPE |
86ff0982 JH |
114 | #if defined(USE_LONG_DOUBLE) && defined(HAS_LONG_DOUBLE) |
115 | #define DOUBLETYPE long double | |
116 | #else | |
65179b48 JH |
117 | #define DOUBLETYPE double |
118 | #endif | |
86ff0982 | 119 | #endif |
65179b48 | 120 | |
959f3c4c JH |
121 | #include <stdio.h> |
122 | ||
40501436 | 123 | #$i_stdlib I_STDLIB |
65179b48 JH |
124 | #ifdef I_STDLIB |
125 | #include <stdlib.h> | |
126 | #endif | |
40501436 MB |
127 | #$i_string I_STRING |
128 | #ifdef I_STRING | |
129 | # include <string.h> | |
130 | #else | |
131 | # include <strings.h> | |
132 | #endif | |
65179b48 | 133 | |
40501436 | 134 | int checkit(char *expect, char *got) |
959f3c4c JH |
135 | { |
136 | if (strcmp(expect, got)) { | |
137 | printf("%s oddity: Expected %s, got %s\n", | |
138 | myname, expect, got); | |
139 | exit(1); | |
140 | } | |
141 | } | |
142 | ||
143 | int main() | |
2cb64bf6 MB |
144 | { |
145 | char buf[64]; | |
959f3c4c JH |
146 | buf[63] = '\0'; |
147 | ||
148 | /* This must be 1st test on (which?) platform */ | |
149 | /* Alan Burlison <AlanBurlsin@unn.unisys.com> */ | |
65179b48 | 150 | Gconvert((DOUBLETYPE)0.1, 8, 0, buf); |
959f3c4c JH |
151 | checkit("0.1", buf); |
152 | ||
2cb64bf6 | 153 | Gconvert((DOUBLETYPE)0.01, 8, 0, buf); |
fd2d81e4 JH |
154 | checkit("0.01", buf); |
155 | ||
2cb64bf6 | 156 | Gconvert((DOUBLETYPE)0.001, 8, 0, buf); |
fd2d81e4 JH |
157 | checkit("0.001", buf); |
158 | ||
2cb64bf6 | 159 | Gconvert((DOUBLETYPE)0.0001, 8, 0, buf); |
fd2d81e4 JH |
160 | checkit("0.0001", buf); |
161 | ||
162 | Gconvert((DOUBLETYPE)0.00009, 8, 0, buf); | |
163 | if (strlen(buf) > 5) | |
164 | checkit("9e-005", buf); /* for Microsoft ?? */ | |
165 | else | |
166 | checkit("9e-05", buf); | |
167 | ||
2cb64bf6 | 168 | Gconvert((DOUBLETYPE)1.0, 8, 0, buf); |
959f3c4c JH |
169 | checkit("1", buf); |
170 | ||
2cb64bf6 | 171 | Gconvert((DOUBLETYPE)1.1, 8, 0, buf); |
86ff0982 JH |
172 | checkit("1.1", buf); |
173 | ||
2cb64bf6 | 174 | Gconvert((DOUBLETYPE)1.01, 8, 0, buf); |
86ff0982 JH |
175 | checkit("1.01", buf); |
176 | ||
2cb64bf6 | 177 | Gconvert((DOUBLETYPE)1.001, 8, 0, buf); |
86ff0982 JH |
178 | checkit("1.001", buf); |
179 | ||
2cb64bf6 | 180 | Gconvert((DOUBLETYPE)1.0001, 8, 0, buf); |
86ff0982 JH |
181 | checkit("1.0001", buf); |
182 | ||
2cb64bf6 | 183 | Gconvert((DOUBLETYPE)1.00001, 8, 0, buf); |
86ff0982 JH |
184 | checkit("1.00001", buf); |
185 | ||
2cb64bf6 | 186 | Gconvert((DOUBLETYPE)1.000001, 8, 0, buf); |
86ff0982 JH |
187 | checkit("1.000001", buf); |
188 | ||
2cb64bf6 | 189 | Gconvert((DOUBLETYPE)0.0, 8, 0, buf); |
959f3c4c JH |
190 | checkit("0", buf); |
191 | ||
2cb64bf6 | 192 | Gconvert((DOUBLETYPE)-1.0, 8, 0, buf); |
959f3c4c JH |
193 | checkit("-1", buf); |
194 | ||
195 | /* Some Linux gcvt's give 1.e+5 here. */ | |
2cb64bf6 | 196 | Gconvert((DOUBLETYPE)100000.0, 8, 0, buf); |
959f3c4c | 197 | checkit("100000", buf); |
2cb64bf6 | 198 | |
959f3c4c | 199 | /* Some Linux gcvt's give -1.e+5 here. */ |
2cb64bf6 | 200 | Gconvert((DOUBLETYPE)-100000.0, 8, 0, buf); |
959f3c4c JH |
201 | checkit("-100000", buf); |
202 | ||
2cb64bf6 | 203 | Gconvert((DOUBLETYPE)123.456, 8, 0, buf); |
6cadf241 JH |
204 | checkit("123.456", buf); |
205 | ||
fd2d81e4 | 206 | /* Testing of 1e+129 in bigintpm.t must not get extra '.' here. */ |
f3a5eaad JH |
207 | Gconvert((DOUBLETYPE)1e34, 8, 0, buf); |
208 | /* 34 should be enough to scare even long double | |
209 | * places into using the e notation. */ | |
fd2d81e4 | 210 | if (strlen(buf) > 5) |
f3a5eaad | 211 | checkit("1e+034", buf); /* for Microsoft */ |
fd2d81e4 | 212 | else |
f3a5eaad | 213 | checkit("1e+34", buf); |
1c4981aa | 214 | |
e790fbe1 JH |
215 | /* For Perl, if you add additional tests here, also add them to |
216 | * t/base/num.t for benefit of platforms not using Configure or | |
217 | * overriding d_Gconvert */ | |
218 | ||
959f3c4c JH |
219 | exit(0); |
220 | } | |
221 | EOP | |
222 | ?X: List of order in which to search for functions. | |
223 | ?X: Usual order of efficiency is gconvert gcvt sprintf | |
224 | ?X: If a hint file sets a d_Gconvert="gconvert" or "gcvt" or "sprintf", | |
225 | ?X: then that is taken as a hint for which function to try first. | |
226 | ?X: (e.g. that function may be in a problematic /usr/ucblib library, and | |
227 | ?X: the user may or may not choose to use -lucb stuff.) | |
228 | ?X: Any other hint file (or previous config.sh) setting is left intact. | |
fd2d81e4 JH |
229 | : first add preferred functions to our list |
230 | xxx_list="" | |
231 | for xxx_convert in $gconvert_preference; do | |
232 | case $xxx_convert in | |
233 | gcvt|gconvert|sprintf) xxx_list="$xxx_list $xxx_convert" ;; | |
234 | *) echo "Discarding unrecognized gconvert_preference $xxx_convert" >&4 ;; | |
2cb64bf6 | 235 | esac |
fd2d81e4 JH |
236 | done |
237 | : then add any others | |
238 | for xxx_convert in gconvert gcvt sprintf; do | |
239 | case "$xxx_list" in | |
240 | *$xxx_convert*) ;; | |
241 | *) xxx_list="$xxx_list $xxx_convert" ;; | |
2cb64bf6 | 242 | esac |
fd2d81e4 | 243 | done |
de47c9df | 244 | |
fd2d81e4 JH |
245 | case "$d_longdbl$uselongdouble" in |
246 | "$define$define") | |
04c34a22 | 247 | : again, add preferred functions to our list first |
fd2d81e4 JH |
248 | xxx_ld_list="" |
249 | for xxx_convert in $gconvert_ld_preference; do | |
250 | case $xxx_convert in | |
251 | qgcvt|gcvt|gconvert|sprintf) xxx_ld_list="$xxx_ld_list $xxx_convert" ;; | |
252 | *) echo "Discarding unrecognized gconvert_ld_preference $xxx_convert" ;; | |
253 | esac | |
254 | done | |
255 | : then add qgcvt, sprintf--then, in xxx_list order, gconvert and gcvt | |
256 | for xxx_convert in qgcvt sprintf $xxx_list; do | |
257 | case "$xxx_ld_list" in | |
258 | $xxx_convert*|*" $xxx_convert"*) ;; | |
259 | *) xxx_ld_list="$xxx_ld_list $xxx_convert" ;; | |
260 | esac | |
261 | done | |
262 | : if sprintf cannot do long doubles, move it to the end | |
263 | if test "$d_PRIgldbl" != "$define"; then | |
264 | xxx_ld_list="`echo $xxx_ld_list|sed s/sprintf//` sprintf" | |
265 | fi | |
266 | : if no qgcvt, remove it | |
267 | if test "$d_qgcvt" != "$define"; then | |
268 | xxx_ld_list="`echo $xxx_ld_list|sed s/qgcvt//`" | |
269 | fi | |
270 | : use the ld_list | |
271 | xxx_list="$xxx_ld_list" | |
de47c9df JH |
272 | ;; |
273 | esac | |
274 | ||
275 | for xxx_convert in $xxx_list; do | |
276 | echo "Trying $xxx_convert..." | |
3fb5973e MB |
277 | ?X: Do NOT use $rm_try here, as we need to keep try.c |
278 | $rm -f try try$_o core | |
de47c9df JH |
279 | set try -DTRY_$xxx_convert |
280 | if eval $compile; then | |
281 | echo "$xxx_convert() found." >&4 | |
0f00356b | 282 | if $run ./try; then |
de47c9df JH |
283 | echo "I'll use $xxx_convert to convert floats into a string." >&4 |
284 | break; | |
959f3c4c | 285 | else |
de47c9df | 286 | echo "...But $xxx_convert didn't work as I expected." |
fd2d81e4 | 287 | xxx_convert='' |
959f3c4c | 288 | fi |
de47c9df JH |
289 | else |
290 | echo "$xxx_convert NOT found." >&4 | |
291 | fi | |
292 | done | |
fd2d81e4 JH |
293 | |
294 | if test X$xxx_convert = X; then | |
295 | echo "*** WHOA THERE!!! ***" >&4 | |
296 | echo "None of ($xxx_list) seemed to work properly. I'll use sprintf." >&4 | |
297 | xxx_convert=sprintf | |
298 | fi | |
299 | ||
de47c9df JH |
300 | case "$xxx_convert" in |
301 | gconvert) d_Gconvert='gconvert((x),(n),(t),(b))' ;; | |
302 | gcvt) d_Gconvert='gcvt((x),(n),(b))' ;; | |
303 | qgcvt) d_Gconvert='qgcvt((x),(n),(b))' ;; | |
304 | *) case "$uselongdouble$d_longdbl$d_PRIgldbl" in | |
305 | "$define$define$define") | |
8208ea90 | 306 | d_Gconvert="sprintf((b),\"%.*\"$sPRIgldbl,(n),(x))" ;; |
fd2d81e4 JH |
307 | "$define$define$undef") |
308 | d_Gconvert='sprintf((b),"%.*g",(n),(double)(x))' ;; | |
de47c9df JH |
309 | *) d_Gconvert='sprintf((b),"%.*g",(n),(x))' ;; |
310 | esac | |
2cb64bf6 | 311 | ;; |
de47c9df | 312 | esac |
959f3c4c | 313 | |
fd2d81e4 | 314 | fi |
3fb5973e | 315 | $rm_try |
fd2d81e4 | 316 |