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
|
#include "UnityPrefix.h"
#if UNITY_EDITOR
// This are the definitions of std::numeric_limits<>::max_digits10, which we cannot use
// because it is only in the C++11 standard.
const int kMaxFloatDigits = std::floor(std::numeric_limits<float>::digits * 3010.0/10000.0 + 2);
const int kMaxDoubleDigits = std::floor(std::numeric_limits<double>::digits * 3010.0/10000.0 + 2);
#include "External/gdtoa/gdtoa.h"
bool FloatToStringAccurate (float f, char* buffer, size_t maximumSize)
{
return g_ffmt (buffer, (float*)&f, kMaxFloatDigits, maximumSize) != NULL;
}
bool DoubleToStringAccurate (double f, char* buffer, size_t maximumSize)
{
return g_dfmt (buffer, (double*)&f, kMaxDoubleDigits, maximumSize) != NULL;
}
bool FloatToStringAccurate (float f, UnityStr& output)
{
char buf[64];
if (FloatToStringAccurate (f, buf, 64))
{
output = buf;
return true;
}
else
return false;
}
bool DoubleToStringAccurate (double f, UnityStr& output)
{
char buf[64];
if (DoubleToStringAccurate (f, buf, 64))
{
output = buf;
return true;
}
else
return false;
}
float StringToFloatAccurate (const char* buffer)
{
return strtof (buffer, NULL);
}
double StringToDoubleAccurate (const char* buffer)
{
return strtod (buffer, NULL);
}
#if ENABLE_UNIT_TESTS
#include "../../External/UnitTest++/src/UnitTest++.h"
SUITE (FloatStringConversionTests)
{
TEST(FloatToStringConversion_AccurateWorks)
{
// Make sure no locale is used
CHECK_EQUAL (0.0F, StringToFloatAccurate("0,5"));
UnityStr buf;
FloatToStringAccurate(1.0F, buf);
CHECK_EQUAL ("1", buf);
CHECK_EQUAL (1.0F, StringToFloatAccurate("1.0"));
FloatToStringAccurate(1.5F, buf);
CHECK_EQUAL ("1.5", buf);
CHECK_EQUAL (1.5F, StringToFloatAccurate("1.5"));
}
}
#endif
#endif
|