| 1 | ?RCS: $Id$ |
| 2 | ?RCS: |
| 3 | ?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi |
| 4 | ?RCS: |
| 5 | ?RCS: You may redistribute only under the terms of the Artistic License, |
| 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 License; a copy of which may be found at the root |
| 9 | ?RCS: of the source tree for dist 4.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: cat cc ccflags ldflags libs 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:. |
| 31 | ?C:Gconvert: |
| 32 | ?C: This preprocessor macro is defined to convert a floating point |
| 33 | ?C: number to a string without a trailing decimal point. This |
| 34 | ?C: emulates the behavior of sprintf("%g"), but is sometimes much more |
| 35 | ?C: efficient. If gconvert() is not available, but gcvt() drops the |
| 36 | ?C: trailing decimal point, then gcvt() is used. If all else fails, |
| 37 | ?C: a macro using sprintf("%g") is used. Arguments for the Gconvert |
| 38 | ?C: macro are: value, number of digits, whether trailing zeros should |
| 39 | ?C: be retained, and the output buffer. |
| 40 | ?C: Possible values are: |
| 41 | ?C: d_Gconvert='gconvert((x),(n),(t),(b))' |
| 42 | ?C: d_Gconvert='gcvt((x),(n),(b))' |
| 43 | ?C: d_Gconvert='sprintf((b),"%.*g",(n),(x))' |
| 44 | ?C: The last two assume trailing zeros should not be kept. |
| 45 | ?C:. |
| 46 | ?H:#define Gconvert(x,n,t,b) $d_Gconvert |
| 47 | ?H:. |
| 48 | ?T: xxx_list xxx_convert |
| 49 | ?F:!try |
| 50 | ?X: |
| 51 | : Check how to convert floats to strings. |
| 52 | echo " " |
| 53 | echo "Checking for an efficient way to convert floats to strings." |
| 54 | ?X: We want to be sure to drop trailing decimal points (perl5 |
| 55 | ?X: needs this). |
| 56 | $cat >try.c <<'EOP' |
| 57 | #ifdef TRY_gconvert |
| 58 | #define Gconvert(x,n,t,b) gconvert((x),(n),(t),(b)) |
| 59 | char *myname = "gconvert"; |
| 60 | #endif |
| 61 | #ifdef TRY_gcvt |
| 62 | #define Gconvert(x,n,t,b) gcvt((x),(n),(b)) |
| 63 | char *myname = "gcvt"; |
| 64 | #endif |
| 65 | #ifdef TRY_sprintf |
| 66 | #define Gconvert(x,n,t,b) sprintf((b),"%.*g",(n),(x)) |
| 67 | char *myname = "sprintf"; |
| 68 | #endif |
| 69 | |
| 70 | #include <stdio.h> |
| 71 | |
| 72 | int |
| 73 | checkit(expect, got) |
| 74 | char *expect; |
| 75 | char *got; |
| 76 | { |
| 77 | if (strcmp(expect, got)) { |
| 78 | printf("%s oddity: Expected %s, got %s\n", |
| 79 | myname, expect, got); |
| 80 | exit(1); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | int |
| 85 | int main() |
| 86 | { |
| 87 | char buf[64]; |
| 88 | buf[63] = '\0'; |
| 89 | |
| 90 | /* This must be 1st test on (which?) platform */ |
| 91 | /* Alan Burlison <AlanBurlsin@unn.unisys.com> */ |
| 92 | Gconvert(0.1, 8, 0, buf); |
| 93 | checkit("0.1", buf); |
| 94 | |
| 95 | Gconvert(1.0, 8, 0, buf); |
| 96 | checkit("1", buf); |
| 97 | |
| 98 | Gconvert(0.0, 8, 0, buf); |
| 99 | checkit("0", buf); |
| 100 | |
| 101 | Gconvert(-1.0, 8, 0, buf); |
| 102 | checkit("-1", buf); |
| 103 | |
| 104 | /* Some Linux gcvt's give 1.e+5 here. */ |
| 105 | Gconvert(100000.0, 8, 0, buf); |
| 106 | checkit("100000", buf); |
| 107 | |
| 108 | /* Some Linux gcvt's give -1.e+5 here. */ |
| 109 | Gconvert(-100000.0, 8, 0, buf); |
| 110 | checkit("-100000", buf); |
| 111 | |
| 112 | exit(0); |
| 113 | } |
| 114 | EOP |
| 115 | ?X: List of order in which to search for functions. |
| 116 | ?X: Usual order of efficiency is gconvert gcvt sprintf |
| 117 | ?X: Respect a previous or hinted value. |
| 118 | case "$d_Gconvert" in |
| 119 | gconvert*) xxx_list='gconvert gcvt sprintf' ;; |
| 120 | gcvt*) xxx_list='gcvt gconvert sprintf' ;; |
| 121 | sprintf*) xxx_list='sprintf gconvert gcvt' ;; |
| 122 | *) xxx_list='gconvert gcvt sprintf' ;; |
| 123 | esac |
| 124 | |
| 125 | for xxx_convert in $xxx_list; do |
| 126 | echo "Trying $xxx_convert" |
| 127 | $rm -f try try$_o |
| 128 | if $cc $ccflags -DTRY_$xxx_convert $ldflags -o try \ |
| 129 | try.c $libs > /dev/null 2>&1 ; then |
| 130 | echo "$xxx_convert" found. >&4 |
| 131 | if ./try; then |
| 132 | echo "I'll use $xxx_convert to convert floats into a string." >&4 |
| 133 | break; |
| 134 | else |
| 135 | echo "...But $xxx_convert didn't work as I expected." |
| 136 | fi |
| 137 | else |
| 138 | echo "$xxx_convert NOT found." >&4 |
| 139 | fi |
| 140 | done |
| 141 | |
| 142 | case "$xxx_convert" in |
| 143 | gconvert) d_Gconvert='gconvert((x),(n),(t),(b))' ;; |
| 144 | gcvt) d_Gconvert='gcvt((x),(n),(b))' ;; |
| 145 | *) d_Gconvert='sprintf((b),"%.*g",(n),(x))' ;; |
| 146 | esac |
| 147 | |