1 /* dtoa.cc convert double to ascii and visa versa.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
32 #define EXPORT(FUNC) m2pim ## _dtoa_ ## FUNC
33 #define M2EXPORT(FUNC) m2pim ## _M2_dtoa_ ## FUNC
34 #define M2LIBNAME "m2pim"
36 #if defined(HAVE_STRINGS)
40 #if defined(HAVE_STRING)
44 #if defined(HAVE_STDDEF_H)
45 /* Obtain a definition for NULL. */
49 #if defined(HAVE_STDIO_H)
50 /* Obtain a definition for NULL. */
54 #if defined(HAVE_TIME_H)
55 /* Obtain a definition for NULL. */
59 #if defined(HAVE_STRING_H)
60 /* Obtain a definition for NULL. */
64 #if defined(HAVE_WCHAR_H)
65 /* Obtain a definition for NULL. */
69 #if defined(HAVE_STDLIB_H)
70 /* Obtain a prototype for free and malloc. */
75 #define NULL (void *)0
78 #if defined(HAVE_STDLIB_H)
79 #if !defined(_ISOC99_SOURCE)
80 #define _ISOC99_SOURCE
85 #if defined(HAVE_ERRNO_H)
89 #if defined(HAVE_SYS_ERRNO_H)
90 #include <sys/errno.h>
93 #if defined(HAVE_STRING_H)
95 #define MAX_FP_DIGITS 500
97 typedef enum Mode
{ maxsignicant
, decimaldigits
} Mode
;
99 /* maxsignicant: return a string containing max(1,ndigits) significant
100 digits. The return string contains the string produced by ecvt.
102 decimaldigits: return a string produced by fcvt. The string will
103 contain ndigits past the decimal point (ndigits may be negative). */
106 EXPORT(strtod
) (const char *s
, bool *error
)
111 #if defined(HAVE_ERRNO_H)
114 d
= strtod (s
, &endp
);
115 if (endp
!= NULL
&& (*endp
== '\0'))
116 #if defined(HAVE_ERRNO_H)
117 *error
= (errno
!= 0);
126 /* dtoa_calcmaxsig calculates the position of the decimal point
127 it also removes the decimal point and exponent from string, p. */
130 EXPORT(calcmaxsig
) (char *p
, int ndigits
)
147 return strlen (p
) + x
;
150 memmove (o
, o
+ 1, ndigits
- (o
- p
));
155 /* dtoa_calcdecimal calculates the position of the decimal point
156 it also removes the decimal point and exponent from string, p.
157 It truncates the digits in p accordingly to ndigits.
158 Ie ndigits is the number of digits after the '.'. */
161 EXPORT(calcdecimal
) (char *p
, int str_size
, int ndigits
)
184 memmove (o
, o
+ 1, l
- (o
- p
));
189 if ((x
+ ndigits
>= 0) && (x
+ ndigits
< str_size
))
190 p
[x
+ ndigits
] = (char)0;
195 EXPORT(calcsign
) (char *p
, int str_size
)
199 memmove (p
, p
+ 1, str_size
- 1);
207 EXPORT(dtoa
) (double d
, int mode
, int ndigits
, int *decpt
, bool *sign
)
216 ndigits
+= 20; /* Enough for exponent. */
217 p
= (char *) malloc (ndigits
);
218 snprintf (format
, 50, "%s%d%s", "%.", ndigits
- 20, "E");
219 snprintf (p
, ndigits
, format
, d
);
220 *sign
= EXPORT(calcsign
) (p
, ndigits
);
221 *decpt
= EXPORT(calcmaxsig
) (p
, ndigits
);
224 p
= (char *) malloc (MAX_FP_DIGITS
+ 20);
225 snprintf (format
, 50, "%s%d%s", "%.", MAX_FP_DIGITS
, "E");
226 snprintf (p
, MAX_FP_DIGITS
+ 20, format
, d
);
227 *sign
= EXPORT(calcsign
) (p
, MAX_FP_DIGITS
+ 20);
228 *decpt
= EXPORT(calcdecimal
) (p
, MAX_FP_DIGITS
+ 20, ndigits
);
238 /* GNU Modula-2 linking hooks. */
241 M2EXPORT(init
) (int, char **, char **)
246 M2EXPORT(fini
) (int, char **, char **)
255 extern "C" void __attribute__((__constructor__
))
256 M2EXPORT(ctor
) (void)
258 m2pim_M2RTS_RegisterModule ("dtoa", M2LIBNAME
,
259 M2EXPORT(init
), M2EXPORT(fini
),