This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Populate metaconfig branch.
[metaconfig.git] / U / compline / d_gconvert.U
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
4 ?RCS: 
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:
24 ?MAKE:d_Gconvert: Compile cat Inlibc rm _o
25 ?MAKE:  -pick add $@ %<
26 ?S:d_Gconvert:
27 ?S:     This variable holds what Gconvert is defined as to convert
28 ?S:     floating point numbers into strings. It could be 'gconvert'
29 ?S:     or a more complex macro emulating gconvert with gcvt() or sprintf.
30 ?S:     Possible values are:
31 ?S:             d_Gconvert='gconvert((x),(n),(t),(b))'
32 ?S:             d_Gconvert='gcvt((x),(n),(b))'
33 ?S:             d_Gconvert='sprintf((b),"%.*g",(n),(x))'
34 ?S:.
35 ?C:Gconvert:
36 ?C:     This preprocessor macro is defined to convert a floating point
37 ?C:     number to a string without a trailing decimal point.  This
38 ?C:     emulates the behavior of sprintf("%g"), but is sometimes much more
39 ?C:     efficient.  If gconvert() is not available, but gcvt() drops the
40 ?C:     trailing decimal point, then gcvt() is used.  If all else fails,
41 ?C:     a macro using sprintf("%g") is used. Arguments for the Gconvert
42 ?C:     macro are: value, number of digits, whether trailing zeros should
43 ?C:     be retained, and the output buffer.
44 ?C:     Possible values are:
45 ?C:             d_Gconvert='gconvert((x),(n),(t),(b))'
46 ?C:             d_Gconvert='gcvt((x),(n),(b))'
47 ?C:             d_Gconvert='sprintf((b),"%.*g",(n),(x))'
48 ?C:     The last two assume trailing zeros should not be kept.
49 ?C:.
50 ?H:#define Gconvert(x,n,t,b) $d_Gconvert
51 ?H:.
52 ?T: xxx_list xxx_convert
53 ?F:!try
54 ?X: 
55 : Check how to convert floats to strings.
56 if test "X$d_Gconvert" = X; then
57         echo " "
58         echo "Checking for an efficient way to convert floats to strings."
59         $cat >try.c <<'EOP'
60 #ifdef TRY_gconvert
61 #define Gconvert(x,n,t,b) gconvert((x),(n),(t),(b))
62 char *myname = "gconvert";
63 #endif
64 #ifdef TRY_gcvt
65 #define Gconvert(x,n,t,b) gcvt((x),(n),(b))
66 char *myname = "gcvt";
67 #endif
68 #ifdef TRY_sprintf
69 #define Gconvert(x,n,t,b) sprintf((b),"%.*g",(n),(x))
70 char *myname = "sprintf";
71 #endif
72
73 #include <stdio.h>
74
75 int
76 checkit(expect, got)
77 char *expect;
78 char *got;
79 {
80     if (strcmp(expect, got)) {
81                 printf("%s oddity:  Expected %s, got %s\n",
82                         myname, expect, got);
83                 exit(1);
84         }
85 }
86
87 int main()
88
89         char buf[64]; 
90         buf[63] = '\0';
91
92         /* This must be 1st test on (which?) platform */
93         /* Alan Burlison <AlanBurlsin@unn.unisys.com> */
94         Gconvert(0.1, 8, 0, buf);
95         checkit("0.1", buf);
96
97         Gconvert(1.0, 8, 0, buf); 
98         checkit("1", buf);
99
100         Gconvert(0.0, 8, 0, buf); 
101         checkit("0", buf);
102
103         Gconvert(-1.0, 8, 0, buf); 
104         checkit("-1", buf);
105
106         /* Some Linux gcvt's give 1.e+5 here. */
107         Gconvert(100000.0, 8, 0, buf); 
108         checkit("100000", buf);
109         
110         /* Some Linux gcvt's give -1.e+5 here. */
111         Gconvert(-100000.0, 8, 0, buf); 
112         checkit("-100000", buf);
113
114         exit(0);
115 }
116 EOP
117 ?X: List of order in which to search for functions.
118 ?X: Usual order of efficiency is gconvert gcvt sprintf
119 ?X: If a hint file sets a d_Gconvert="gconvert" or "gcvt" or "sprintf",
120 ?X: then that is taken as a hint for which function to try first.
121 ?X: (e.g. that function may be in a problematic /usr/ucblib library, and
122 ?X: the user may or may not choose to use -lucb stuff.)
123 ?X: Any other hint file (or previous config.sh) setting is left intact.
124         case "$d_Gconvert" in
125         gconvert*) xxx_list='gconvert gcvt sprintf' ;;
126         gcvt*) xxx_list='gcvt gconvert sprintf' ;;
127         sprintf*) xxx_list='sprintf gconvert gcvt' ;;
128         *) xxx_list='gconvert gcvt sprintf' ;;
129         esac
130
131         for xxx_convert in $xxx_list; do
132                 echo "Trying $xxx_convert"
133                 $rm -f try try$_o
134                 set try -DTRY_$xxx_convert
135                 if eval $compile; then
136                         echo "$xxx_convert" found. >&4
137                         if ./try; then
138                                 echo "I'll use $xxx_convert to convert floats into a string." >&4
139                                 break;
140                         else
141                                 echo "...But $xxx_convert didn't work as I expected."
142                         fi
143                 else
144                         echo "$xxx_convert NOT found." >&4
145                 fi
146         done
147                 
148         case "$xxx_convert" in
149         gconvert) d_Gconvert='gconvert((x),(n),(t),(b))' ;;
150         gcvt) d_Gconvert='gcvt((x),(n),(b))' ;;
151         *) d_Gconvert='sprintf((b),"%.*g",(n),(x))' ;;
152         esac
153 fi
154