|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef jsdtoa_h |
|
8 #define jsdtoa_h |
|
9 |
|
10 /* |
|
11 * Public interface to portable double-precision floating point to string |
|
12 * and back conversion package. |
|
13 */ |
|
14 |
|
15 #include <stddef.h> |
|
16 |
|
17 struct DtoaState; |
|
18 |
|
19 DtoaState * |
|
20 js_NewDtoaState(); |
|
21 |
|
22 void |
|
23 js_DestroyDtoaState(DtoaState *state); |
|
24 |
|
25 /* |
|
26 * js_strtod_harder() returns as a double-precision floating-point number the |
|
27 * value represented by the character string pointed to by s00. The string is |
|
28 * scanned up to the first unrecognized character. |
|
29 * |
|
30 * If se is not nullptr, *se receives a pointer to the character terminating |
|
31 * the scan. If no number can be formed, *se receives a pointer to the first |
|
32 * unparseable character in s00, and zero is returned. |
|
33 * |
|
34 * On overflow, this function returns infinity and does not indicate an error. |
|
35 * |
|
36 * *err is set to zero on success; it's set to JS_DTOA_ENOMEM on memory failure. |
|
37 */ |
|
38 #define JS_DTOA_ENOMEM 2 |
|
39 double |
|
40 js_strtod_harder(DtoaState *state, const char *s00, char **se, int *err); |
|
41 |
|
42 /* |
|
43 * Modes for converting floating-point numbers to strings. |
|
44 * |
|
45 * Some of the modes can round-trip; this means that if the number is converted to |
|
46 * a string using one of these mode and then converted back to a number, the result |
|
47 * will be identical to the original number (except that, due to ECMA, -0 will get converted |
|
48 * to +0). These round-trip modes return the minimum number of significand digits that |
|
49 * permit the round trip. |
|
50 * |
|
51 * Some of the modes take an integer parameter <precision>. |
|
52 */ |
|
53 /* NB: Keep this in sync with number_constants[]. */ |
|
54 typedef enum JSDToStrMode { |
|
55 DTOSTR_STANDARD, /* Either fixed or exponential format; round-trip */ |
|
56 DTOSTR_STANDARD_EXPONENTIAL, /* Always exponential format; round-trip */ |
|
57 DTOSTR_FIXED, /* Round to <precision> digits after the decimal point; exponential if number is large */ |
|
58 DTOSTR_EXPONENTIAL, /* Always exponential format; <precision> significant digits */ |
|
59 DTOSTR_PRECISION /* Either fixed or exponential format; <precision> significant digits */ |
|
60 } JSDToStrMode; |
|
61 |
|
62 |
|
63 /* Maximum number of characters (including trailing null) that a DTOSTR_STANDARD or DTOSTR_STANDARD_EXPONENTIAL |
|
64 * conversion can produce. This maximum is reached for a number like -0.0000012345678901234567. */ |
|
65 #define DTOSTR_STANDARD_BUFFER_SIZE 26 |
|
66 |
|
67 /* Maximum number of characters (including trailing null) that one of the other conversions |
|
68 * can produce. This maximum is reached for TO_FIXED, which can generate up to 21 digits before the decimal point. */ |
|
69 #define DTOSTR_VARIABLE_BUFFER_SIZE(precision) ((precision)+24 > DTOSTR_STANDARD_BUFFER_SIZE ? (precision)+24 : DTOSTR_STANDARD_BUFFER_SIZE) |
|
70 |
|
71 /* |
|
72 * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a |
|
73 * better function to use. |
|
74 * |
|
75 * Convert dval according to the given mode and return a pointer to the |
|
76 * resulting ASCII string. If mode == DTOSTR_STANDARD and precision == 0 it's |
|
77 * equivalent to ToString() as specified by ECMA-262-5 section 9.8.1, but it |
|
78 * doesn't handle integers specially so should be avoided in that case (that's |
|
79 * why js::NumberToCString() is better). |
|
80 * |
|
81 * The result is held somewhere in buffer, but not necessarily at the |
|
82 * beginning. The size of buffer is given in bufferSize, and must be at least |
|
83 * as large as given by the above macros. |
|
84 * |
|
85 * Return nullptr if out of memory. |
|
86 */ |
|
87 char * |
|
88 js_dtostr(DtoaState *state, char *buffer, size_t bufferSize, JSDToStrMode mode, int precision, |
|
89 double dval); |
|
90 |
|
91 /* |
|
92 * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a |
|
93 * better function to use. |
|
94 * |
|
95 * Convert d to a string in the given base. The integral part of d will be |
|
96 * printed exactly in that base, regardless of how large it is, because there |
|
97 * is no exponential notation for non-base-ten numbers. The fractional part |
|
98 * will be rounded to as few digits as possible while still preserving the |
|
99 * round-trip property (analogous to that of printing decimal numbers). In |
|
100 * other words, if one were to read the resulting string in via a hypothetical |
|
101 * base-number-reading routine that rounds to the nearest IEEE double (and to |
|
102 * an even significand if there are two equally near doubles), then the result |
|
103 * would equal d (except for -0.0, which converts to "0", and NaN, which is |
|
104 * not equal to itself). |
|
105 * |
|
106 * Return nullptr if out of memory. If the result is not nullptr, it must be |
|
107 * released via js_free(). |
|
108 */ |
|
109 char * |
|
110 js_dtobasestr(DtoaState *state, int base, double d); |
|
111 |
|
112 #endif /* jsdtoa_h */ |