1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 |
* |
3 |
* ***** BEGIN LICENSE BLOCK ***** |
4 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
5 |
* |
6 |
* The contents of this file are subject to the Mozilla Public License Version |
7 |
* 1.1 (the "License"); you may not use this file except in compliance with |
8 |
* the License. You may obtain a copy of the License at |
9 |
* http://www.mozilla.org/MPL/ |
10 |
* |
11 |
* Software distributed under the License is distributed on an "AS IS" basis, |
12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
13 |
* for the specific language governing rights and limitations under the |
14 |
* License. |
15 |
* |
16 |
* The Original Code is Mozilla Communicator client code, released |
17 |
* March 31, 1998. |
18 |
* |
19 |
* The Initial Developer of the Original Code is |
20 |
* Netscape Communications Corporation. |
21 |
* Portions created by the Initial Developer are Copyright (C) 1998 |
22 |
* the Initial Developer. All Rights Reserved. |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* Alternatively, the contents of this file may be used under the terms of |
27 |
* either of the GNU General Public License Version 2 or later (the "GPL"), |
28 |
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
29 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
30 |
* of those above. If you wish to allow use of your version of this file only |
31 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
32 |
* use your version of this file under the terms of the MPL, indicate your |
33 |
* decision by deleting the provisions above and replace them with the notice |
34 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
35 |
* the provisions above, a recipient may use your version of this file under |
36 |
* the terms of any one of the MPL, the GPL or the LGPL. |
37 |
* |
38 |
* ***** END LICENSE BLOCK ***** */ |
39 |
|
40 |
#ifndef jsdtoa_h___ |
41 |
#define jsdtoa_h___ |
42 |
/* |
43 |
* Public interface to portable double-precision floating point to string |
44 |
* and back conversion package. |
45 |
*/ |
46 |
|
47 |
#include "jscompat.h" |
48 |
|
49 |
JS_BEGIN_EXTERN_C |
50 |
|
51 |
/* |
52 |
* JS_strtod() returns as a double-precision floating-point number |
53 |
* the value represented by the character string pointed to by |
54 |
* s00. The string is scanned up to the first unrecognized |
55 |
* character. |
56 |
* If the value of se is not (char **)NULL, a pointer to |
57 |
* the character terminating the scan is returned in the location pointed |
58 |
* to by se. If no number can be formed, se is set to s00r, and |
59 |
* zero is returned. |
60 |
* |
61 |
* *err is set to zero on success; it's set to JS_DTOA_ERANGE on range |
62 |
* errors and JS_DTOA_ENOMEM on memory failure. |
63 |
*/ |
64 |
#define JS_DTOA_ERANGE 1 |
65 |
#define JS_DTOA_ENOMEM 2 |
66 |
JS_FRIEND_API(double) |
67 |
JS_strtod(const char *s00, char **se, int *err); |
68 |
|
69 |
/* |
70 |
* Modes for converting floating-point numbers to strings. |
71 |
* |
72 |
* Some of the modes can round-trip; this means that if the number is converted to |
73 |
* a string using one of these mode and then converted back to a number, the result |
74 |
* will be identical to the original number (except that, due to ECMA, -0 will get converted |
75 |
* to +0). These round-trip modes return the minimum number of significand digits that |
76 |
* permit the round trip. |
77 |
* |
78 |
* Some of the modes take an integer parameter <precision>. |
79 |
*/ |
80 |
/* NB: Keep this in sync with number_constants[]. */ |
81 |
typedef enum JSDToStrMode { |
82 |
DTOSTR_STANDARD, /* Either fixed or exponential format; round-trip */ |
83 |
DTOSTR_STANDARD_EXPONENTIAL, /* Always exponential format; round-trip */ |
84 |
DTOSTR_FIXED, /* Round to <precision> digits after the decimal point; exponential if number is large */ |
85 |
DTOSTR_EXPONENTIAL, /* Always exponential format; <precision> significant digits */ |
86 |
DTOSTR_PRECISION /* Either fixed or exponential format; <precision> significant digits */ |
87 |
} JSDToStrMode; |
88 |
|
89 |
|
90 |
/* Maximum number of characters (including trailing null) that a DTOSTR_STANDARD or DTOSTR_STANDARD_EXPONENTIAL |
91 |
* conversion can produce. This maximum is reached for a number like -0.0000012345678901234567. */ |
92 |
#define DTOSTR_STANDARD_BUFFER_SIZE 26 |
93 |
|
94 |
/* Maximum number of characters (including trailing null) that one of the other conversions |
95 |
* can produce. This maximum is reached for TO_FIXED, which can generate up to 21 digits before the decimal point. */ |
96 |
#define DTOSTR_VARIABLE_BUFFER_SIZE(precision) ((precision)+24 > DTOSTR_STANDARD_BUFFER_SIZE ? (precision)+24 : DTOSTR_STANDARD_BUFFER_SIZE) |
97 |
|
98 |
/* |
99 |
* Convert dval according to the given mode and return a pointer to the resulting ASCII string. |
100 |
* The result is held somewhere in buffer, but not necessarily at the beginning. The size of |
101 |
* buffer is given in bufferSize, and must be at least as large as given by the above macros. |
102 |
* |
103 |
* Return NULL if out of memory. |
104 |
*/ |
105 |
JS_FRIEND_API(char *) |
106 |
JS_dtostr(char *buffer, size_t bufferSize, JSDToStrMode mode, int precision, double dval); |
107 |
|
108 |
/* |
109 |
* Convert d to a string in the given base. The integral part of d will be printed exactly |
110 |
* in that base, regardless of how large it is, because there is no exponential notation for non-base-ten |
111 |
* numbers. The fractional part will be rounded to as few digits as possible while still preserving |
112 |
* the round-trip property (analogous to that of printing decimal numbers). In other words, if one were |
113 |
* to read the resulting string in via a hypothetical base-number-reading routine that rounds to the nearest |
114 |
* IEEE double (and to an even significand if there are two equally near doubles), then the result would |
115 |
* equal d (except for -0.0, which converts to "0", and NaN, which is not equal to itself). |
116 |
* |
117 |
* Return NULL if out of memory. If the result is not NULL, it must be released via free(). |
118 |
*/ |
119 |
JS_FRIEND_API(char *) |
120 |
JS_dtobasestr(int base, double d); |
121 |
|
122 |
/* |
123 |
* Clean up any persistent RAM allocated during the execution of DtoA |
124 |
* routines, and remove any locks that might have been created. |
125 |
*/ |
126 |
JS_FRIEND_API(JSBool) js_InitDtoa(void); |
127 |
JS_FRIEND_API(void) js_FinishDtoa(void); |
128 |
|
129 |
JS_END_EXTERN_C |
130 |
|
131 |
#endif /* jsdtoa_h___ */ |