summaryrefslogtreecommitdiff
path: root/mcon/U/d_gconvert.U
blob: 367da203d66f477c61a195cb301faa77b890ee14 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
?RCS: $Id$
?RCS:
?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
?RCS: 
?RCS: You may redistribute only under the terms of the Artistic Licence,
?RCS: as specified in the README file that comes with the distribution.
?RCS: You may reuse parts of this distribution only within the terms of
?RCS: that same Artistic Licence; a copy of which may be found at the root
?RCS: of the source tree for dist 4.0.
?RCS:
?RCS: Original Author: Andy Dougherty <doughera@lafcol.lafayette.edu>
?RCS:
?RCS: $Log: d_gconvert.U,v $
?RCS: Revision 3.0.1.3  1997/02/28  15:33:38  ram
?RCS: patch61: integrated new unit from perl5
?RCS:
?RCS: Revision 3.0.1.2  1995/07/25  13:55:59  ram
?RCS: patch56: improved comments about the Gconvert macro (ADO)
?RCS: patch56: force compile-link test since it may exist but be unusable (ADO)
?RCS:
?RCS: Revision 3.0.1.1  1994/10/29  16:12:51  ram
?RCS: patch36: created by ADO
?RCS:
?MAKE:d_Gconvert: cat cc ccflags ldflags libs Inlibc rm _o
?MAKE:	-pick add $@ %<
?S:d_Gconvert:
?S:	This variable holds what Gconvert is defined as to convert
?S:	floating point numbers into strings. It could be 'gconvert'
?S:	or a more complex macro emulating gconvert with gcvt() or sprintf.
?S:.
?C:Gconvert:
?C:	This preprocessor macro is defined to convert a floating point
?C:	number to a string without a trailing decimal point.  This
?C:	emulates the behavior of sprintf("%g"), but is sometimes much more
?C:	efficient.  If gconvert() is not available, but gcvt() drops the
?C:	trailing decimal point, then gcvt() is used.  If all else fails,
?C:	a macro using sprintf("%g") is used. Arguments for the Gconvert
?C:	macro are: value, number of digits, whether trailing zeros should
?C:	be retained, and the output buffer.
?C:	Possible values are:
?C:		d_Gconvert='gconvert((x),(n),(t),(b))'
?C:		d_Gconvert='gcvt((x),(n),(b))'
?C:		d_Gconvert='sprintf((b),"%.*g",(n),(x))'
?C:	The last two assume trailing zeros should not be kept.
?C:.
?H:#define Gconvert(x,n,t,b) $d_Gconvert
?H:.
?T: xxx_list xxx_convert
?F:!try
?X: 
: Check how to convert floats to strings.
echo " "
echo "Checking for an efficient way to convert floats to strings."
?X: We want to be sure to drop trailing decimal points (perl5
?X: needs this).
$cat >try.c <<'EOP'
#ifdef TRY_gconvert
#define Gconvert(x,n,t,b) gconvert((x),(n),(t),(b))
char *myname = "gconvert";
#endif
#ifdef TRY_gcvt
#define Gconvert(x,n,t,b) gcvt((x),(n),(b))
char *myname = "gcvt";
#endif
#ifdef TRY_sprintf
#define Gconvert(x,n,t,b) sprintf((b),"%.*g",(n),(x))
char *myname = "sprintf";
#endif

#include <stdio.h>

int
checkit(expect, got)
char *expect;
char *got;
{
    if (strcmp(expect, got)) {
		printf("%s oddity:  Expected %s, got %s\n",
			myname, expect, got);
		exit(1);
	}
}

int
int main()
{ 
	char buf[64]; 
	buf[63] = '\0';

	/* This must be 1st test on (which?) platform */
	/* Alan Burlison <AlanBurlsin@unn.unisys.com> */
	Gconvert(0.1, 8, 0, buf);
	checkit("0.1", buf);

	Gconvert(1.0, 8, 0, buf); 
	checkit("1", buf);

	Gconvert(0.0, 8, 0, buf); 
	checkit("0", buf);

	Gconvert(-1.0, 8, 0, buf); 
	checkit("-1", buf);

	/* Some Linux gcvt's give 1.e+5 here. */
	Gconvert(100000.0, 8, 0, buf); 
	checkit("100000", buf);
	
	/* Some Linux gcvt's give -1.e+5 here. */
	Gconvert(-100000.0, 8, 0, buf); 
	checkit("-100000", buf);

	exit(0);
}
EOP
?X: List of order in which to search for functions.
?X: Usual order of efficiency is gconvert gcvt sprintf
?X: Respect a previous or hinted value.
case "$d_Gconvert" in
gconvert*) xxx_list='gconvert gcvt sprintf' ;;
gcvt*) xxx_list='gcvt gconvert sprintf' ;;
sprintf*) xxx_list='sprintf gconvert gcvt' ;;
*) xxx_list='gconvert gcvt sprintf' ;;
esac

for xxx_convert in $xxx_list; do
	echo "Trying $xxx_convert"
	$rm -f try try$_o
	if $cc $ccflags -DTRY_$xxx_convert $ldflags -o try \
		try.c $libs > /dev/null 2>&1 ; then
		echo "$xxx_convert" found. >&4
		if ./try; then
			echo "I'll use $xxx_convert to convert floats into a string." >&4
			break;
		else
			echo "...But $xxx_convert didn't work as I expected."
		fi
	else
		echo "$xxx_convert NOT found." >&4
	fi
done
		
case "$xxx_convert" in
gconvert) d_Gconvert='gconvert((x),(n),(t),(b))' ;;
gcvt) d_Gconvert='gcvt((x),(n),(b))' ;;
*) d_Gconvert='sprintf((b),"%.*g",(n),(x))' ;;
esac