-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
90 lines (75 loc) · 1.28 KB
/
utils.c
File metadata and controls
90 lines (75 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "utils.h"
char * ftoa(double f, char * buf, int precision)
{
char * ptr = buf;
char * p = ptr;
char * p1;
char c;
long intPart;
// check precision bounds
if (precision > MAX_PRECISION)
precision = MAX_PRECISION;
// sign stuff
if (f < 0)
{
f = -f;
*ptr++ = '-';
}
if (precision < 0) // negative precision == automatic precision guess
{
if (f < 1.0) precision = 6;
else if (f < 10.0) precision = 5;
else if (f < 100.0) precision = 4;
else if (f < 1000.0) precision = 3;
else if (f < 10000.0) precision = 2;
else if (f < 100000.0) precision = 1;
else precision = 0;
}
// round value according the precision
if (precision)
f += rounders[precision];
// integer part...
intPart = f;
f -= intPart;
if (!intPart)
*ptr++ = '0';
else
{
// save start pointer
p = ptr;
// convert (reverse order)
while (intPart)
{
*p++ = '0' + intPart % 10;
intPart /= 10;
}
// save end pos
p1 = p;
// reverse result
while (p > ptr)
{
c = *--p;
*p = *ptr;
*ptr++ = c;
}
// restore end pos
ptr = p1;
}
// decimal part
if (precision)
{
// place decimal point
*ptr++ = '.';
// convert
while (precision--)
{
f *= 10.0;
c = f;
*ptr++ = '0' + c;
f -= c;
}
}
// terminating zero
*ptr = 0;
return buf;
}