ecvt.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/* Copyright (c) 1988 AT&T */
/* All Rights Reserved */
/*
* ecvt converts to decimal
* the number of digits is specified by ndigit
* decpt is set to the position of the decimal point
* sign is set to 0 for positive, 1 for negative
*
*/
#include "synonyms.h"
#include <values.h>
#include <nan.h>
#include <string.h>
#include "tsd.h"
#define NDIG 80
static char *cvt(double, int, int *, int *, int);
char *
{
}
char *
{
}
static char *
{
buf[0] = '\0';
if (IsNANorINF(value)) {
else /* value is a NaN, return "NaN" */
return (buf);
}
*decpt = 0;
if (value != 0.0) {
/*
* rescale to range [1.0, 10.0)
* in binary for speed and to minimize error build-up
* even for the IEEE standard with its high exponents,
* it's probably better for speed to just loop on them
*/
static const struct s { double p10; int n; } s[] = {
1e32, 32,
1e16, 16,
1e8, 8,
1e4, 4,
1e2, 2,
1e1, 1,
};
const struct s *sp = s;
++*decpt;
do {
} while (sp++->n > 1);
for (; ; pow10 /= 10.0) {
*p++ = digit + '0';
++*decpt;
if (pow10 <= 10.0)
break;
}
} else if (value < 1.0)
do {
} while (sp++->n > 1);
}
if (f_flag)
for (; ; ++p) {
*p = '0';
else {
int intx; /* intx in [0, 9] */
}
if (p >= p_last) {
p = p_last;
break;
}
}
if (*p >= '5') /* check rounding in last place + 1 */
do {
if (p == buf) { /* rollover from 99999... */
++*decpt;
if (f_flag)
++p_last;
break;
}
*p = '0';
} while (++*--p > '9'); /* propagate carries left */
*p_last = '\0';
}
return (buf);
}