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