The Mozilla
Organization
At A Glance
Feedback
Get Involved
Newsgroups
License Terms
Newsbot
Developer Docs
Roadmap
Projects
Ports
Module Owners
Hacking
Get the Source
Build It
Testing
Download
Bugzilla
Bug Writing
Tools
View Source
Tree Status
New Checkins
Submit A Bug
FAQ
Search
[Contents] [Previous] [Next] [Last]

Chapter 26
Floating Point Number to String Conversion

NSPR provides functions that convert double-precision floating point numbers to and from their character string representations. The header file prdtoa.h declares these functions.

These conversion functions were originally written by David M. Gay of AT&T. They use IEEE double-precision (not IEEE double-extended) arithmetic.

PR_strtod

Syntax
#include <prdtoa.h>

PRFloat64 PR_strtod(const char *s00, char **se);
Parameters
The function has the following parameters:
s00 The input string to be scanned.
se A pointer that, if not NULL, will be assigned the address of the last character scanned in the input string.
Returns
The result of the conversion is a PRFloat64 value representative of the input string. If the parameter se is not NULL the location it references will also be set.
Description
PR_strtod converts the prefix of the input decimal string pointed to by s00 to a nearest double-precision floating point number. Ties are broken by the IEEE round-even rule. The string is scanned up to the first unrecognized character. If the value of se is not (char **) NULL, PR_strtod stores a pointer to the character terminating the scan in *endptr. If the answer would overflow, a properly signed HUGE_VAL (infinity) is returned. If the answer would underflow, a properly signed 0 is returned. In both cases, PR_GetError() returns the error code PR_RANGE_ERROR. If no number can be formed, se is set to s00, and 0 is returned.

PR_dtoa

Syntax
#include <prdtoa.h>

PRStatus PR_dtoa(
    PRFloat64 d,
    PRIntn mode,
    PRIntn ndigits,
    PRIntn *decpt,
    PRIntn *sign,
    char **rve,
    char *buf,
    PRSize bufsz);
Parameters
PR_dtoa takes the following parameters:
d The floating point number to be converted to ASCII.
mode The type of conversion to employ.
0 Shortest string that yields d when read in and rounded to nearest.
1 Like 0, but with Steele & White stopping rule [2]; e.g., with IEEE P754 arithmetic, mode 0 gives 1e23 whereas mode 1 gives 9.999999999999999e22.
2 max(1, ndigits) significant digits. This gives a return value similar to that of ecvt, except that trailing zeros are suppressed.
3 Through ndights past the decimal point. This gives a return value similar to that from fcvt, except that trailing zeros are suppressed, and ndigits can be negative.
4,5,8,9 Same as modes [2..3] but using left to right digit generation.
6-9 Same as modes [2..3] but don't try fast floating-point estimate (if applicable).
all others Treated as mode 2.
ndigits The number of digits desired in the output string.
decpt A pointer to a memory location where the runtime will store the offset, relative to the beginning of the output string, of the conversion's decimal point.
sign A location where the runtime can store an indication that the conversion was of a negative value.
rve If not NULL this location is set to the address of the end of the result.
buf The address of the buffer in which to store the result.
bufsz The size of the buffer provided to hold the result.
Results
The principle output is the null-terminated string stored in buf. Trailing zeros are suppressed.

If rve is not NULL, *rve is set to point to the end of the returned value.

If the input parameter, d, is +infinity, -infinity or NAN, *decpt is set to 9999.

Description
Sufficient space is allocated to the return value to hold the suppressed trailing zeros.

PR_cnvtf

Syntax
#include <prdtoa.h>
void PR_cnvtf(char *buf, PRIntn bufsz, PRIntn prcsn, PRFloat64 fval);
Description
PR_cnvtf is a simpler interface to convert a floating point number to a string. In fact, it is written to conform to the ECMA standard of Javascript (ECMAScript).

The input argument fval is the double-precision floating point number to be converted, and prcsn is the number of digits of precision to generate floating point value. On return, the result is written to the buffer pointed to by buf of size bufsz.

References

Gay's implementation is inspired by these two papers.

[1] William D. Clinger, "How to Read Floating Point Numbers Accurately," Proc. ACM SIGPLAN '90, pp. 92-101.

[2] Guy L. Steele, Jr. and Jon L. White, "How to Print Floating-Point Numbers Accurately," Proc. ACM SIGPLAN '90, pp. 92-101.



[Contents] [Previous] [Next] [Last]
Last updated: Wed Jul 15 09:53:03 PDT 1998

Copyright © 1998 Netscape Communications Corporation
Copyright © 1998-1999 The Mozilla Organization.
Last modified July 17, 1998.