Imported Upstream version 1.4.16
[platform/upstream/m4.git] / m4 / strtod.m4
1 # strtod.m4 serial 19
2 dnl Copyright (C) 2002-2003, 2006-2011 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_STRTOD],
8 [
9   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
10   dnl Test whether strtod is declared.
11   dnl Don't call AC_FUNC_STRTOD, because it does not have the right guess
12   dnl when cross-compiling.
13   dnl Don't call AC_CHECK_FUNCS([strtod]) because it would collide with the
14   dnl ac_cv_func_strtod variable set by the AC_FUNC_STRTOD macro.
15   AC_CHECK_DECLS_ONCE([strtod])
16   if test $ac_cv_have_decl_strtod != yes; then
17     HAVE_STRTOD=0
18   else
19     AC_CACHE_CHECK([whether strtod obeys C99], [gl_cv_func_strtod_works],
20       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
21 #include <stdlib.h>
22 #include <math.h>
23 #include <errno.h>
24 /* Compare two numbers with ==.
25    This is a separate function because IRIX 6.5 "cc -O" miscompiles an
26    'x == x' test.  */
27 static int
28 numeric_equal (double x, double y)
29 {
30   return x == y;
31 }
32 ]], [[
33   int result = 0;
34   {
35     /* In some old versions of Linux (2000 or before), strtod mis-parses
36        strings with leading '+'.  */
37     const char *string = " +69";
38     char *term;
39     double value = strtod (string, &term);
40     if (value != 69 || term != (string + 4))
41       result |= 1;
42   }
43   {
44     /* Under Solaris 2.4, strtod returns the wrong value for the
45        terminating character under some conditions.  */
46     const char *string = "NaN";
47     char *term;
48     strtod (string, &term);
49     if (term != string && *(term - 1) == 0)
50       result |= 2;
51   }
52   {
53     /* Older glibc and Cygwin mis-parse "-0x".  */
54     const char *string = "-0x";
55     char *term;
56     double value = strtod (string, &term);
57     double zero = 0.0;
58     if (1.0 / value != -1.0 / zero || term != (string + 2))
59       result |= 4;
60   }
61   {
62     /* Many platforms do not parse hex floats.  */
63     const char *string = "0XaP+1";
64     char *term;
65     double value = strtod (string, &term);
66     if (value != 20.0 || term != (string + 6))
67       result |= 8;
68   }
69   {
70     /* Many platforms do not parse infinities.  HP-UX 11.31 parses inf,
71        but mistakenly sets errno.  */
72     const char *string = "inf";
73     char *term;
74     double value;
75     errno = 0;
76     value = strtod (string, &term);
77     if (value != HUGE_VAL || term != (string + 3) || errno)
78       result |= 16;
79   }
80   {
81     /* glibc 2.7 and cygwin 1.5.24 misparse "nan()".  */
82     const char *string = "nan()";
83     char *term;
84     double value = strtod (string, &term);
85     if (numeric_equal (value, value) || term != (string + 5))
86       result |= 32;
87   }
88   {
89     /* darwin 10.6.1 misparses "nan(".  */
90     const char *string = "nan(";
91     char *term;
92     double value = strtod (string, &term);
93     if (numeric_equal (value, value) || term != (string + 3))
94       result |= 64;
95   }
96   return result;
97 ]])],
98         [gl_cv_func_strtod_works=yes],
99         [gl_cv_func_strtod_works=no],
100         [dnl The last known bugs in glibc strtod(), as of this writing,
101          dnl were fixed in version 2.8
102          AC_EGREP_CPP([Lucky user],
103            [
104 #include <features.h>
105 #ifdef __GNU_LIBRARY__
106  #if ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 8) || (__GLIBC__ > 2)) \
107      && !defined __UCLIBC__
108   Lucky user
109  #endif
110 #endif
111            ],
112            [gl_cv_func_strtod_works=yes],
113            [gl_cv_func_strtod_works="guessing no"])])])
114     if test "$gl_cv_func_strtod_works" != yes; then
115       REPLACE_STRTOD=1
116     fi
117   fi
118   if test $HAVE_STRTOD = 0 || test $REPLACE_STRTOD = 1; then
119     AC_LIBOBJ([strtod])
120     gl_PREREQ_STRTOD
121   fi
122 ])
123
124 # Prerequisites of lib/strtod.c.
125 AC_DEFUN([gl_PREREQ_STRTOD], [
126   AC_REQUIRE([gl_CHECK_LDEXP_NO_LIBM])
127   if test $gl_cv_func_ldexp_no_libm = yes; then
128     AC_DEFINE([HAVE_LDEXP_IN_LIBC], [1],
129       [Define if the ldexp function is available in libc.])
130   fi
131 ])