-
Notifications
You must be signed in to change notification settings - Fork 2
/
ftoa.cs
104 lines (90 loc) · 2.82 KB
/
ftoa.cs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
namespace Performance
{
// Ported from
// https://github.com/antongus/stm32tpl/blob/master/ftoa.c
public class ftoa
{
private const int MAX_PRECISION = 10;
private static double[] rounders =
{
0.5, // 0
0.05, // 1
0.005, // 2
0.0005, // 3
0.00005, // 4
0.000005, // 5
0.0000005, // 6
0.00000005, // 7
0.000000005, // 8
0.0000000005, // 9
0.00000000005 // 10
};
public static void Convert(ref StackBuffer buffer, double f, int precision)
{
if (f > ulong.MaxValue)
{
buffer.Append("Out-of-range");
return;
}
ulong intPart;
ulong baseVal = 10; // baseVal has to be the same type as the integer part
// check precision bounds
if (precision > MAX_PRECISION)
precision = MAX_PRECISION;
// sign stuff
if (f < 0)
{
f = -f;
buffer.Append('-');
}
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 > 0)
f += rounders[precision];
// integer part...
intPart = (ulong) Math.Truncate(f);
f -= intPart;
if (intPart == 0)
{
buffer.Append('0');
}
else
{
var tempBuffer = new StackBuffer(stackalloc char[MAX_PRECISION]);
// convert (reverse order)
while (intPart != 0)
{
tempBuffer.Append((char) ('0' + intPart % baseVal));
intPart /= baseVal;
}
tempBuffer.Reverse();
for (var i = 0; i < tempBuffer.Count; i++)
buffer.Append(tempBuffer[i]);
}
// decimal part
if (precision != 0)
{
// place decimal point
buffer.Append('.');
// convert
while (precision-- != 0)
{
f *= 10.0;
var c = (char) f;
buffer.Append((char) ('0' + c));
f -= c;
}
}
}
}
}