Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jsdtoa.h
blobeacfb4d517d7a76b9da6a059cf769d307a63e271
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
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.
24 * Contributor(s):
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.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsdtoa_h___
41 #define jsdtoa_h___
43 * Public interface to portable double-precision floating point to string
44 * and back conversion package.
47 #include "jscompat.h"
49 JS_BEGIN_EXTERN_C
51 struct DtoaState;
53 DtoaState *
54 js_NewDtoaState();
56 void
57 js_DestroyDtoaState(DtoaState *state);
60 * js_strtod_harder() returns as a double-precision floating-point number the
61 * value represented by the character string pointed to by s00. The string is
62 * scanned up to the first unrecognized character.
64 * If se is not NULL, *se receives a pointer to the character terminating the
65 * scan. If no number can be formed, *se receives a pointer to the first
66 * unparseable character in s00, and zero is returned.
68 * *err is set to zero on success; it's set to JS_DTOA_ERANGE on range
69 * errors and JS_DTOA_ENOMEM on memory failure.
71 #define JS_DTOA_ERANGE 1
72 #define JS_DTOA_ENOMEM 2
73 double
74 js_strtod_harder(DtoaState *state, const char *s00, char **se, int *err);
77 * Modes for converting floating-point numbers to strings.
79 * Some of the modes can round-trip; this means that if the number is converted to
80 * a string using one of these mode and then converted back to a number, the result
81 * will be identical to the original number (except that, due to ECMA, -0 will get converted
82 * to +0). These round-trip modes return the minimum number of significand digits that
83 * permit the round trip.
85 * Some of the modes take an integer parameter <precision>.
87 /* NB: Keep this in sync with number_constants[]. */
88 typedef enum JSDToStrMode {
89 DTOSTR_STANDARD, /* Either fixed or exponential format; round-trip */
90 DTOSTR_STANDARD_EXPONENTIAL, /* Always exponential format; round-trip */
91 DTOSTR_FIXED, /* Round to <precision> digits after the decimal point; exponential if number is large */
92 DTOSTR_EXPONENTIAL, /* Always exponential format; <precision> significant digits */
93 DTOSTR_PRECISION /* Either fixed or exponential format; <precision> significant digits */
94 } JSDToStrMode;
97 /* Maximum number of characters (including trailing null) that a DTOSTR_STANDARD or DTOSTR_STANDARD_EXPONENTIAL
98 * conversion can produce. This maximum is reached for a number like -0.0000012345678901234567. */
99 #define DTOSTR_STANDARD_BUFFER_SIZE 26
101 /* Maximum number of characters (including trailing null) that one of the other conversions
102 * can produce. This maximum is reached for TO_FIXED, which can generate up to 21 digits before the decimal point. */
103 #define DTOSTR_VARIABLE_BUFFER_SIZE(precision) ((precision)+24 > DTOSTR_STANDARD_BUFFER_SIZE ? (precision)+24 : DTOSTR_STANDARD_BUFFER_SIZE)
106 * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a
107 * better function to use.
109 * Convert dval according to the given mode and return a pointer to the
110 * resulting ASCII string. If mode == DTOSTR_STANDARD and precision == 0 it's
111 * equivalent to ToString() as specified by ECMA-262-5 section 9.8.1, but it
112 * doesn't handle integers specially so should be avoided in that case (that's
113 * why js::NumberToCString() is better).
115 * The result is held somewhere in buffer, but not necessarily at the
116 * beginning. The size of buffer is given in bufferSize, and must be at least
117 * as large as given by the above macros.
119 * Return NULL if out of memory.
121 char *
122 js_dtostr(DtoaState *state, char *buffer, size_t bufferSize, JSDToStrMode mode, int precision,
123 double dval);
126 * DO NOT USE THIS FUNCTION IF YOU CAN AVOID IT. js::NumberToCString() is a
127 * better function to use.
129 * Convert d to a string in the given base. The integral part of d will be
130 * printed exactly in that base, regardless of how large it is, because there
131 * is no exponential notation for non-base-ten numbers. The fractional part
132 * will be rounded to as few digits as possible while still preserving the
133 * round-trip property (analogous to that of printing decimal numbers). In
134 * other words, if one were to read the resulting string in via a hypothetical
135 * base-number-reading routine that rounds to the nearest IEEE double (and to
136 * an even significand if there are two equally near doubles), then the result
137 * would equal d (except for -0.0, which converts to "0", and NaN, which is
138 * not equal to itself).
140 * Return NULL if out of memory. If the result is not NULL, it must be
141 * released via js_free().
143 char *
144 js_dtobasestr(DtoaState *state, int base, double d);
146 JS_END_EXTERN_C
148 #endif /* jsdtoa_h___ */