parent
6934b123d4
commit
b9b2aeecaf
@ -1287,7 +1287,9 @@ bool xml_attribute::set_value(unsigned int rhs);
|
|||||||
bool xml_attribute::set_value(long rhs);
|
bool xml_attribute::set_value(long rhs);
|
||||||
bool xml_attribute::set_value(unsigned long rhs);
|
bool xml_attribute::set_value(unsigned long rhs);
|
||||||
bool xml_attribute::set_value(double rhs);
|
bool xml_attribute::set_value(double rhs);
|
||||||
|
bool xml_attribute::set_value(double rhs, int precision);
|
||||||
bool xml_attribute::set_value(float rhs);
|
bool xml_attribute::set_value(float rhs);
|
||||||
|
bool xml_attribute::set_value(float rhs, int precision);
|
||||||
bool xml_attribute::set_value(bool rhs);
|
bool xml_attribute::set_value(bool rhs);
|
||||||
bool xml_attribute::set_value(long long rhs);
|
bool xml_attribute::set_value(long long rhs);
|
||||||
bool xml_attribute::set_value(unsigned long long rhs);
|
bool xml_attribute::set_value(unsigned long long rhs);
|
||||||
@ -1439,7 +1441,9 @@ bool xml_text::set(unsigned int rhs);
|
|||||||
bool xml_text::set(long rhs);
|
bool xml_text::set(long rhs);
|
||||||
bool xml_text::set(unsigned long rhs);
|
bool xml_text::set(unsigned long rhs);
|
||||||
bool xml_text::set(double rhs);
|
bool xml_text::set(double rhs);
|
||||||
|
bool xml_text::set(double rhs, int precision);
|
||||||
bool xml_text::set(float rhs);
|
bool xml_text::set(float rhs);
|
||||||
|
bool xml_text::set(float rhs, int precision);
|
||||||
bool xml_text::set(bool rhs);
|
bool xml_text::set(bool rhs);
|
||||||
bool xml_text::set(long long rhs);
|
bool xml_text::set(long long rhs);
|
||||||
bool xml_text::set(unsigned long long rhs);
|
bool xml_text::set(unsigned long long rhs);
|
||||||
|
@ -4665,19 +4665,19 @@ PUGI__NS_BEGIN
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename String, typename Header>
|
template <typename String, typename Header>
|
||||||
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, float value)
|
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, float value, int precision)
|
||||||
{
|
{
|
||||||
char buf[128];
|
char buf[128];
|
||||||
PUGI__SNPRINTF(buf, "%.9g", double(value));
|
PUGI__SNPRINTF(buf, "%.*g", precision, double(value));
|
||||||
|
|
||||||
return set_value_ascii(dest, header, header_mask, buf);
|
return set_value_ascii(dest, header, header_mask, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename String, typename Header>
|
template <typename String, typename Header>
|
||||||
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, double value)
|
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, double value, int precision)
|
||||||
{
|
{
|
||||||
char buf[128];
|
char buf[128];
|
||||||
PUGI__SNPRINTF(buf, "%.17g", value);
|
PUGI__SNPRINTF(buf, "%.*g", precision, value);
|
||||||
|
|
||||||
return set_value_ascii(dest, header, header_mask, buf);
|
return set_value_ascii(dest, header, header_mask, buf);
|
||||||
}
|
}
|
||||||
@ -5342,14 +5342,28 @@ namespace pugi
|
|||||||
{
|
{
|
||||||
if (!_attr) return false;
|
if (!_attr) return false;
|
||||||
|
|
||||||
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
|
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, default_double_precision);
|
||||||
|
}
|
||||||
|
|
||||||
|
PUGI__FN bool xml_attribute::set_value(double rhs, int precision)
|
||||||
|
{
|
||||||
|
if (!_attr) return false;
|
||||||
|
|
||||||
|
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
PUGI__FN bool xml_attribute::set_value(float rhs)
|
PUGI__FN bool xml_attribute::set_value(float rhs)
|
||||||
{
|
{
|
||||||
if (!_attr) return false;
|
if (!_attr) return false;
|
||||||
|
|
||||||
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs);
|
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, default_float_precision);
|
||||||
|
}
|
||||||
|
|
||||||
|
PUGI__FN bool xml_attribute::set_value(float rhs, int precision)
|
||||||
|
{
|
||||||
|
if (!_attr) return false;
|
||||||
|
|
||||||
|
return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs, precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
PUGI__FN bool xml_attribute::set_value(bool rhs)
|
PUGI__FN bool xml_attribute::set_value(bool rhs)
|
||||||
@ -6545,14 +6559,28 @@ namespace pugi
|
|||||||
{
|
{
|
||||||
xml_node_struct* dn = _data_new();
|
xml_node_struct* dn = _data_new();
|
||||||
|
|
||||||
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
|
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, default_float_precision) : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
PUGI__FN bool xml_text::set(float rhs, int precision)
|
||||||
|
{
|
||||||
|
xml_node_struct* dn = _data_new();
|
||||||
|
|
||||||
|
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, precision) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
PUGI__FN bool xml_text::set(double rhs)
|
PUGI__FN bool xml_text::set(double rhs)
|
||||||
{
|
{
|
||||||
xml_node_struct* dn = _data_new();
|
xml_node_struct* dn = _data_new();
|
||||||
|
|
||||||
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
|
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, default_double_precision) : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
PUGI__FN bool xml_text::set(double rhs, int precision)
|
||||||
|
{
|
||||||
|
xml_node_struct* dn = _data_new();
|
||||||
|
|
||||||
|
return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs, precision) : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
PUGI__FN bool xml_text::set(bool rhs)
|
PUGI__FN bool xml_text::set(bool rhs)
|
||||||
|
@ -263,6 +263,9 @@ namespace pugi
|
|||||||
// Nodes are indented depending on their depth in DOM tree, a default declaration is output if document has none.
|
// Nodes are indented depending on their depth in DOM tree, a default declaration is output if document has none.
|
||||||
const unsigned int format_default = format_indent;
|
const unsigned int format_default = format_indent;
|
||||||
|
|
||||||
|
const int default_double_precision = 17;
|
||||||
|
const int default_float_precision = 9;
|
||||||
|
|
||||||
// Forward declarations
|
// Forward declarations
|
||||||
struct xml_attribute_struct;
|
struct xml_attribute_struct;
|
||||||
struct xml_node_struct;
|
struct xml_node_struct;
|
||||||
@ -410,7 +413,9 @@ namespace pugi
|
|||||||
bool set_value(long rhs);
|
bool set_value(long rhs);
|
||||||
bool set_value(unsigned long rhs);
|
bool set_value(unsigned long rhs);
|
||||||
bool set_value(double rhs);
|
bool set_value(double rhs);
|
||||||
|
bool set_value(double rhs, int precision);
|
||||||
bool set_value(float rhs);
|
bool set_value(float rhs);
|
||||||
|
bool set_value(float rhs, int precision);
|
||||||
bool set_value(bool rhs);
|
bool set_value(bool rhs);
|
||||||
|
|
||||||
#ifdef PUGIXML_HAS_LONG_LONG
|
#ifdef PUGIXML_HAS_LONG_LONG
|
||||||
@ -767,7 +772,9 @@ namespace pugi
|
|||||||
bool set(long rhs);
|
bool set(long rhs);
|
||||||
bool set(unsigned long rhs);
|
bool set(unsigned long rhs);
|
||||||
bool set(double rhs);
|
bool set(double rhs);
|
||||||
|
bool set(double rhs, int precision);
|
||||||
bool set(float rhs);
|
bool set(float rhs);
|
||||||
|
bool set(float rhs, int precision);
|
||||||
bool set(bool rhs);
|
bool set(bool rhs);
|
||||||
|
|
||||||
#ifdef PUGIXML_HAS_LONG_LONG
|
#ifdef PUGIXML_HAS_LONG_LONG
|
||||||
|
@ -1762,6 +1762,31 @@ TEST(dom_fp_roundtrip_min_max_double)
|
|||||||
CHECK(fp_equal(node.text().as_double(), std::numeric_limits<double>::max()));
|
CHECK(fp_equal(node.text().as_double(), std::numeric_limits<double>::max()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(dom_fp_double_custom_precision)
|
||||||
|
{
|
||||||
|
xml_document doc;
|
||||||
|
xml_node node = doc.append_child(STR("node"));
|
||||||
|
xml_attribute attr = node.append_attribute(STR("attr"));
|
||||||
|
|
||||||
|
attr.set_value(std::numeric_limits<double>::min(), 20);
|
||||||
|
CHECK(fp_equal(attr.as_double(), std::numeric_limits<double>::min()));
|
||||||
|
|
||||||
|
attr.set_value(1.0f, 5);
|
||||||
|
CHECK(fp_equal(attr.as_double(), static_cast<double>(1.0f)));
|
||||||
|
|
||||||
|
attr.set_value(3.1415926f, 3);
|
||||||
|
CHECK(!fp_equal(attr.as_double(), static_cast<double>(3.1415926f)));
|
||||||
|
|
||||||
|
node.text().set(1.0f, 5);
|
||||||
|
CHECK(fp_equal(node.text().as_double(), static_cast<double>(1.0f)));
|
||||||
|
|
||||||
|
node.text().set(3.1415926f, 3);
|
||||||
|
CHECK(!fp_equal(node.text().as_double(), static_cast<double>(3.1415926f)));
|
||||||
|
|
||||||
|
node.text().set(std::numeric_limits<double>::max(), 20);
|
||||||
|
CHECK(fp_equal(node.text().as_double(), std::numeric_limits<double>::max()));
|
||||||
|
}
|
||||||
|
|
||||||
const double fp_roundtrip_base[] =
|
const double fp_roundtrip_base[] =
|
||||||
{
|
{
|
||||||
0.31830988618379067154,
|
0.31830988618379067154,
|
||||||
|
Loading…
Reference in New Issue
Block a user