Back to index

lightning-sunbird  0.9+nobinonly
jsdtoa.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef jsdtoa_h___
00041 #define jsdtoa_h___
00042 /*
00043  * Public interface to portable double-precision floating point to string
00044  * and back conversion package.
00045  */
00046 
00047 #include "jscompat.h"
00048 
00049 JS_BEGIN_EXTERN_C
00050 
00051 /*
00052  * JS_strtod() returns as a double-precision floating-point number
00053  * the  value represented by the character string pointed to by
00054  * s00.  The string is scanned up to  the  first  unrecognized
00055  * character.
00056  * If the value of se is not (char **)NULL,  a  pointer  to
00057  * the  character terminating the scan is returned in the location pointed
00058  * to by se.  If no number can be  formed, se is set to s00r, and
00059  * zero is returned.
00060  *
00061  * *err is set to zero on success; it's set to JS_DTOA_ERANGE on range
00062  * errors and JS_DTOA_ENOMEM on memory failure.
00063  */
00064 #define JS_DTOA_ERANGE 1
00065 #define JS_DTOA_ENOMEM 2
00066 JS_FRIEND_API(double)
00067 JS_strtod(const char *s00, char **se, int *err);
00068 
00069 /*
00070  * Modes for converting floating-point numbers to strings.
00071  *
00072  * Some of the modes can round-trip; this means that if the number is converted to
00073  * a string using one of these mode and then converted back to a number, the result
00074  * will be identical to the original number (except that, due to ECMA, -0 will get converted
00075  * to +0).  These round-trip modes return the minimum number of significand digits that
00076  * permit the round trip.
00077  *
00078  * Some of the modes take an integer parameter <precision>.
00079  */
00080 /* NB: Keep this in sync with number_constants[]. */
00081 typedef enum JSDToStrMode {
00082     DTOSTR_STANDARD,              /* Either fixed or exponential format; round-trip */
00083     DTOSTR_STANDARD_EXPONENTIAL,  /* Always exponential format; round-trip */
00084     DTOSTR_FIXED,                 /* Round to <precision> digits after the decimal point; exponential if number is large */
00085     DTOSTR_EXPONENTIAL,           /* Always exponential format; <precision> significant digits */
00086     DTOSTR_PRECISION              /* Either fixed or exponential format; <precision> significant digits */
00087 } JSDToStrMode;
00088 
00089 
00090 /* Maximum number of characters (including trailing null) that a DTOSTR_STANDARD or DTOSTR_STANDARD_EXPONENTIAL
00091  * conversion can produce.  This maximum is reached for a number like -0.0000012345678901234567. */
00092 #define DTOSTR_STANDARD_BUFFER_SIZE 26
00093 
00094 /* Maximum number of characters (including trailing null) that one of the other conversions
00095  * can produce.  This maximum is reached for TO_FIXED, which can generate up to 21 digits before the decimal point. */
00096 #define DTOSTR_VARIABLE_BUFFER_SIZE(precision) ((precision)+24 > DTOSTR_STANDARD_BUFFER_SIZE ? (precision)+24 : DTOSTR_STANDARD_BUFFER_SIZE)
00097 
00098 /*
00099  * Convert dval according to the given mode and return a pointer to the resulting ASCII string.
00100  * The result is held somewhere in buffer, but not necessarily at the beginning.  The size of
00101  * buffer is given in bufferSize, and must be at least as large as given by the above macros.
00102  *
00103  * Return NULL if out of memory.
00104  */
00105 JS_FRIEND_API(char *)
00106 JS_dtostr(char *buffer, size_t bufferSize, JSDToStrMode mode, int precision, double dval);
00107 
00108 /*
00109  * Convert d to a string in the given base.  The integral part of d will be printed exactly
00110  * in that base, regardless of how large it is, because there is no exponential notation for non-base-ten
00111  * numbers.  The fractional part will be rounded to as few digits as possible while still preserving
00112  * the round-trip property (analogous to that of printing decimal numbers).  In other words, if one were
00113  * to read the resulting string in via a hypothetical base-number-reading routine that rounds to the nearest
00114  * IEEE double (and to an even significand if there are two equally near doubles), then the result would
00115  * equal d (except for -0.0, which converts to "0", and NaN, which is not equal to itself).
00116  *
00117  * Return NULL if out of memory.  If the result is not NULL, it must be released via free().
00118  */
00119 JS_FRIEND_API(char *)
00120 JS_dtobasestr(int base, double d);
00121 
00122 /*
00123  * Clean up any persistent RAM allocated during the execution of DtoA
00124  * routines, and remove any locks that might have been created.
00125  */
00126 extern void js_FinishDtoa(void);
00127 
00128 JS_END_EXTERN_C
00129 
00130 #endif /* jsdtoa_h___ */