//1// fcvt.c2//3// Floating point to string conversion routines4//5// Copyright (C) 2002 Michael Ringgaard. All rights reserved.6//7// Redistribution and use in source and binary forms, with or without8// modification, are permitted provided that the following conditions9// are met:10//11// 1. Redistributions of source code must retain the above copyright12// notice, this list of conditions and the following disclaimer.13// 2. Redistributions in binary form must reproduce the above copyright14// notice, this list of conditions and the following disclaimer in the15// documentation and/or other materials provided with the distribution.16// 3. Neither the name of the project nor the names of its contributors17// may be used to endorse or promote products derived from this software18// without specific prior written permission.19//20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND21// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE24// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30// SUCH DAMAGE.31//3233#include <math.h>34#define CVTBUFSIZE 643536//37// cvt.c - IEEE floating point formatting routines for FreeBSD38// from GNU libc-4.6.2739//4041static char *cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag) {42int r2;43double fi, fj;44char *p, *p1;4546if (ndigits < 0) ndigits = 0;47if (ndigits >= CVTBUFSIZE - 1) ndigits = CVTBUFSIZE - 2;48r2 = 0;49*sign = 0;50p = &buf[0];51if (arg < 0) {52*sign = 1;53arg = -arg;54}55arg = modf(arg, &fi);56p1 = &buf[CVTBUFSIZE];5758if (fi != 0) {59p1 = &buf[CVTBUFSIZE];60while (fi != 0) {61fj = modf(fi / 10, &fi);62*--p1 = (int)((fj + .03) * 10) + '0';63r2++;64}65while (p1 < &buf[CVTBUFSIZE]) *p++ = *p1++;66} else if (arg > 0) {67while ((fj = arg * 10) < 1) {68arg = fj;69r2--;70}71}72p1 = &buf[ndigits];73if (eflag == 0) p1 += r2;74*decpt = r2;75if (p1 < &buf[0]) {76buf[0] = '\0';77return buf;78}79while (p <= p1 && p < &buf[CVTBUFSIZE]) {80arg *= 10;81arg = modf(arg, &fj);82*p++ = (int) fj + '0';83}84if (p1 >= &buf[CVTBUFSIZE]) {85buf[CVTBUFSIZE - 1] = '\0';86return buf;87}88p = p1;89*p1 += 5;90while (*p1 > '9') {91*p1 = '0';92if (p1 > buf) {93++*--p1;94} else {95*p1 = '1';96(*decpt)++;97if (eflag == 0) {98if (p > buf) *p = '0';99p++;100}101}102}103*p = '\0';104return buf;105}106107inline char *ecvt(double arg, int ndigits, int *decpt, int *sign) {108char cvtbuf[CVTBUFSIZE];109return cvt(arg, ndigits, decpt, sign, cvtbuf, 1);110}111112inline char *ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf) {113return cvt(arg, ndigits, decpt, sign, buf, 1);114}115116inline char *fcvt(double arg, int ndigits, int *decpt, int *sign) {117char cvtbuf[CVTBUFSIZE];118return cvt(arg, ndigits, decpt, sign, cvtbuf, 0);119}120121inline char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf) {122return cvt(arg, ndigits, decpt, sign, buf, 0);123}124125126127