]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/m2/gm2-libs-ch/dtoa.cc
1 /* dtoa.c provide floating point string conversion routines.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius@glam.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/>. */
31 #include "gm2-libs-host.h"
38 #define MAX_FP_DIGITS 500
40 typedef enum Mode
{ maxsignicant
, decimaldigits
} Mode
;
42 /* maxsignicant: return a string containing max(1,ndigits)
43 significant digits. The return string contains the string
44 produced by ecvt. decimaldigits: return a string produced by
45 fcvt. The string will contain ndigits past the decimal point
46 (ndigits may be negative). */
49 dtoa_strtod (const char *s
, int *error
)
55 d
= strtod (s
, &endp
);
56 if (endp
!= NULL
&& (*endp
== '\0'))
57 *error
= (errno
!= 0);
63 /* dtoa_calcmaxsig - calculates the position of the decimal point it
64 also removes the decimal point and exponent from string, p. */
67 dtoa_calcmaxsig (char *p
, int ndigits
)
84 return strlen (p
) + x
;
87 memmove (o
, o
+ 1, ndigits
- (o
- p
));
92 /* dtoa_calcdecimal - calculates the position of the decimal point it
93 also removes the decimal point and exponent from string, p. It
94 truncates the digits in p accordingly to ndigits. Ie ndigits is
95 the number of digits after the '.' */
98 dtoa_calcdecimal (char *p
, int str_size
, int ndigits
)
121 memmove (o
, o
+ 1, l
- (o
- p
));
126 if ((x
+ ndigits
>= 0) && (x
+ ndigits
< str_size
))
127 p
[x
+ ndigits
] = (char)0;
132 dtoa_calcsign (char *p
, int str_size
)
136 memmove (p
, p
+ 1, str_size
- 1);
144 dtoa_dtoa (double d
, int mode
, int ndigits
, int *decpt
, int *sign
)
153 ndigits
+= 20; /* enough for exponent. */
154 p
= (char *) malloc (ndigits
);
155 snprintf (format
, 50, "%s%d%s", "%.", ndigits
- 20, "E");
156 snprintf (p
, ndigits
, format
, d
);
157 *sign
= dtoa_calcsign (p
, ndigits
);
158 *decpt
= dtoa_calcmaxsig (p
, ndigits
);
161 p
= (char *) malloc (MAX_FP_DIGITS
+ 20);
162 snprintf (format
, 50, "%s%d%s", "%.", MAX_FP_DIGITS
, "E");
163 snprintf (p
, MAX_FP_DIGITS
+ 20, format
, d
);
164 *sign
= dtoa_calcsign (p
, MAX_FP_DIGITS
+ 20);
165 *decpt
= dtoa_calcdecimal (p
, MAX_FP_DIGITS
+ 20, ndigits
);
172 /* GNU Modula-2 hooks */
175 _M2_dtoa_init (int, char **, char **)
180 _M2_dtoa_finish (int, char **, char **)
192 struct _M2_dtoa_ctor
{ _M2_dtoa_ctor (); } _M2_dtoa_ctor
;
194 _M2_dtoa_ctor::_M2_dtoa_ctor (void)
196 M2RTS_RegisterModule ("dtoa", _M2_dtoa_init
, _M2_dtoa_finish
,