Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/astro/oskar/files/fcvt.h
16461 views
1
//
2
// fcvt.c
3
//
4
// Floating point to string conversion routines
5
//
6
// Copyright (C) 2002 Michael Ringgaard. All rights reserved.
7
//
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions
10
// are met:
11
//
12
// 1. Redistributions of source code must retain the above copyright
13
// notice, this list of conditions and the following disclaimer.
14
// 2. Redistributions in binary form must reproduce the above copyright
15
// notice, this list of conditions and the following disclaimer in the
16
// documentation and/or other materials provided with the distribution.
17
// 3. Neither the name of the project nor the names of its contributors
18
// may be used to endorse or promote products derived from this software
19
// without specific prior written permission.
20
//
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
25
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
// SUCH DAMAGE.
32
//
33
34
#include <math.h>
35
#define CVTBUFSIZE 64
36
37
//
38
// cvt.c - IEEE floating point formatting routines for FreeBSD
39
// from GNU libc-4.6.27
40
//
41
42
static char *cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag) {
43
int r2;
44
double fi, fj;
45
char *p, *p1;
46
47
if (ndigits < 0) ndigits = 0;
48
if (ndigits >= CVTBUFSIZE - 1) ndigits = CVTBUFSIZE - 2;
49
r2 = 0;
50
*sign = 0;
51
p = &buf[0];
52
if (arg < 0) {
53
*sign = 1;
54
arg = -arg;
55
}
56
arg = modf(arg, &fi);
57
p1 = &buf[CVTBUFSIZE];
58
59
if (fi != 0) {
60
p1 = &buf[CVTBUFSIZE];
61
while (fi != 0) {
62
fj = modf(fi / 10, &fi);
63
*--p1 = (int)((fj + .03) * 10) + '0';
64
r2++;
65
}
66
while (p1 < &buf[CVTBUFSIZE]) *p++ = *p1++;
67
} else if (arg > 0) {
68
while ((fj = arg * 10) < 1) {
69
arg = fj;
70
r2--;
71
}
72
}
73
p1 = &buf[ndigits];
74
if (eflag == 0) p1 += r2;
75
*decpt = r2;
76
if (p1 < &buf[0]) {
77
buf[0] = '\0';
78
return buf;
79
}
80
while (p <= p1 && p < &buf[CVTBUFSIZE]) {
81
arg *= 10;
82
arg = modf(arg, &fj);
83
*p++ = (int) fj + '0';
84
}
85
if (p1 >= &buf[CVTBUFSIZE]) {
86
buf[CVTBUFSIZE - 1] = '\0';
87
return buf;
88
}
89
p = p1;
90
*p1 += 5;
91
while (*p1 > '9') {
92
*p1 = '0';
93
if (p1 > buf) {
94
++*--p1;
95
} else {
96
*p1 = '1';
97
(*decpt)++;
98
if (eflag == 0) {
99
if (p > buf) *p = '0';
100
p++;
101
}
102
}
103
}
104
*p = '\0';
105
return buf;
106
}
107
108
inline char *ecvt(double arg, int ndigits, int *decpt, int *sign) {
109
char cvtbuf[CVTBUFSIZE];
110
return cvt(arg, ndigits, decpt, sign, cvtbuf, 1);
111
}
112
113
inline char *ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf) {
114
return cvt(arg, ndigits, decpt, sign, buf, 1);
115
}
116
117
inline char *fcvt(double arg, int ndigits, int *decpt, int *sign) {
118
char cvtbuf[CVTBUFSIZE];
119
return cvt(arg, ndigits, decpt, sign, cvtbuf, 0);
120
}
121
122
inline char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf) {
123
return cvt(arg, ndigits, decpt, sign, buf, 0);
124
}
125
126
127