17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * ecvt converts to decimal
327c478bd9Sstevel@tonic-gate * the number of digits is specified by ndigit
337c478bd9Sstevel@tonic-gate * decpt is set to the position of the decimal point
347c478bd9Sstevel@tonic-gate * sign is set to 0 for positive, 1 for negative
357c478bd9Sstevel@tonic-gate *
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate
38*7257d1b4Sraf #pragma weak _ecvt = ecvt
39*7257d1b4Sraf #pragma weak _fcvt = fcvt
40*7257d1b4Sraf
41*7257d1b4Sraf #include "lint.h"
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <values.h>
447c478bd9Sstevel@tonic-gate #include <nan.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include "tsd.h"
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define NMAX ((DSIGNIF * 3 + 19)/10) /* restrict max precision */
497c478bd9Sstevel@tonic-gate #define NDIG 80
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static char *cvt(double, int, int *, int *, int);
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate char *
ecvt(double value,int ndigit,int * decpt,int * sign)547c478bd9Sstevel@tonic-gate ecvt(double value, int ndigit, int *decpt, int *sign)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate return (cvt(value, ndigit, decpt, sign, 0));
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate char *
fcvt(double value,int ndigit,int * decpt,int * sign)607c478bd9Sstevel@tonic-gate fcvt(double value, int ndigit, int *decpt, int *sign)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate return (cvt(value, ndigit, decpt, sign, 1));
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate static char *
cvt(double value,int ndigit,int * decpt,int * sign,int f_flag)667c478bd9Sstevel@tonic-gate cvt(double value, int ndigit, int *decpt, int *sign, int f_flag)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate char *buf = tsdalloc(_T_ECVT, NDIG, NULL);
697c478bd9Sstevel@tonic-gate char *p = &buf[0], *p_last = &buf[ndigit];
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate buf[0] = '\0';
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate if (IsNANorINF(value)) {
747c478bd9Sstevel@tonic-gate if (IsINF(value)) /* value is an INF, return "inf" */
757c478bd9Sstevel@tonic-gate (void) strncpy(buf, "inf", NDIG);
767c478bd9Sstevel@tonic-gate else /* value is a NaN, return "NaN" */
777c478bd9Sstevel@tonic-gate (void) strncpy(buf, "nan", NDIG);
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate return (buf);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate if ((*sign = (value < 0.0)) != 0)
837c478bd9Sstevel@tonic-gate value = -value;
847c478bd9Sstevel@tonic-gate *decpt = 0;
857c478bd9Sstevel@tonic-gate if (value != 0.0) {
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * rescale to range [1.0, 10.0)
887c478bd9Sstevel@tonic-gate * in binary for speed and to minimize error build-up
897c478bd9Sstevel@tonic-gate * even for the IEEE standard with its high exponents,
907c478bd9Sstevel@tonic-gate * it's probably better for speed to just loop on them
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate static const struct s { double p10; int n; } s[] = {
937c478bd9Sstevel@tonic-gate 1e32, 32,
947c478bd9Sstevel@tonic-gate 1e16, 16,
957c478bd9Sstevel@tonic-gate 1e8, 8,
967c478bd9Sstevel@tonic-gate 1e4, 4,
977c478bd9Sstevel@tonic-gate 1e2, 2,
987c478bd9Sstevel@tonic-gate 1e1, 1,
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate const struct s *sp = s;
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate ++*decpt;
1037c478bd9Sstevel@tonic-gate if (value >= 2.0 * MAXPOWTWO) /* can't be precisely integral */
1047c478bd9Sstevel@tonic-gate do {
1057c478bd9Sstevel@tonic-gate for (; value >= sp->p10; *decpt += sp->n)
1067c478bd9Sstevel@tonic-gate value /= sp->p10;
1077c478bd9Sstevel@tonic-gate } while (sp++->n > 1);
1087c478bd9Sstevel@tonic-gate else if (value >= 10.0) { /* convert integer part separately */
1097c478bd9Sstevel@tonic-gate double pow10 = 10.0, powtemp;
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate while ((powtemp = 10.0 * pow10) <= value)
1127c478bd9Sstevel@tonic-gate pow10 = powtemp;
1137c478bd9Sstevel@tonic-gate for (; ; pow10 /= 10.0) {
1147c478bd9Sstevel@tonic-gate int digit = value/pow10;
1157c478bd9Sstevel@tonic-gate *p++ = digit + '0';
1167c478bd9Sstevel@tonic-gate value -= digit * pow10;
1177c478bd9Sstevel@tonic-gate ++*decpt;
1187c478bd9Sstevel@tonic-gate if (pow10 <= 10.0)
1197c478bd9Sstevel@tonic-gate break;
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate } else if (value < 1.0)
1227c478bd9Sstevel@tonic-gate do {
1237c478bd9Sstevel@tonic-gate for (; value * sp->p10 < 10.0; *decpt -= sp->n)
1247c478bd9Sstevel@tonic-gate value *= sp->p10;
1257c478bd9Sstevel@tonic-gate } while (sp++->n > 1);
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate if (f_flag)
1287c478bd9Sstevel@tonic-gate p_last += *decpt;
1297c478bd9Sstevel@tonic-gate if (p_last >= buf) {
1307c478bd9Sstevel@tonic-gate if (p_last > &buf[NDIG - 2])
1317c478bd9Sstevel@tonic-gate p_last = &buf[NDIG - 2];
1327c478bd9Sstevel@tonic-gate for (; ; ++p) {
1337c478bd9Sstevel@tonic-gate if (value == 0 || p >= &buf[NMAX])
1347c478bd9Sstevel@tonic-gate *p = '0';
1357c478bd9Sstevel@tonic-gate else {
1367c478bd9Sstevel@tonic-gate int intx; /* intx in [0, 9] */
1377c478bd9Sstevel@tonic-gate *p = (intx = (int)value) + '0';
1387c478bd9Sstevel@tonic-gate value = 10.0 * (value - (double)intx);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate if (p >= p_last) {
1417c478bd9Sstevel@tonic-gate p = p_last;
1427c478bd9Sstevel@tonic-gate break;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate if (*p >= '5') /* check rounding in last place + 1 */
1467c478bd9Sstevel@tonic-gate do {
1477c478bd9Sstevel@tonic-gate if (p == buf) { /* rollover from 99999... */
1487c478bd9Sstevel@tonic-gate buf[0] = '1'; /* later digits are 0 */
1497c478bd9Sstevel@tonic-gate ++*decpt;
1507c478bd9Sstevel@tonic-gate if (f_flag)
1517c478bd9Sstevel@tonic-gate ++p_last;
1527c478bd9Sstevel@tonic-gate break;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate *p = '0';
1557c478bd9Sstevel@tonic-gate } while (++*--p > '9'); /* propagate carries left */
1567c478bd9Sstevel@tonic-gate *p_last = '\0';
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate return (buf);
1597c478bd9Sstevel@tonic-gate }
160