mirror of
https://github.com/jbeder/yaml-cpp.git
synced 2025-09-09 04:41:16 +00:00
Fix float precision (#649)
The issue is that numbers like 2.01 or 3.01 can not be precisely represented with binary floating point numbers. This replaces all occurrences of 'std::numeric_limits<T>::digits10 + 1' with 'std::numeric_limits<T>::max_digits10'. Background: Using 'std::numeric_limits<T>::digits10 + 1' is not precise enough. Converting a 'float' into a 'string' and back to a 'float' will not always produce the original 'float' value. To guarantee that the 'string' representation has sufficient precision the value 'std::numeric_limits<T>::max_digits10' has to be used.
This commit is contained in:

committed by
Jesse Beder

parent
b659858b19
commit
abf941b20d
@@ -901,18 +901,18 @@ TEST_F(EmitterTest, SingleChar) {
|
||||
|
||||
TEST_F(EmitterTest, DefaultPrecision) {
|
||||
out << BeginSeq;
|
||||
out << 1.234f;
|
||||
out << 3.14159265358979;
|
||||
out << 1.3125f;
|
||||
out << 1.23455810546875;
|
||||
out << EndSeq;
|
||||
ExpectEmit("- 1.234\n- 3.14159265358979");
|
||||
ExpectEmit("- 1.3125\n- 1.23455810546875");
|
||||
}
|
||||
|
||||
TEST_F(EmitterTest, SetPrecision) {
|
||||
out << BeginSeq;
|
||||
out << FloatPrecision(3) << 1.234f;
|
||||
out << DoublePrecision(6) << 3.14159265358979;
|
||||
out << FloatPrecision(3) << 1.3125f;
|
||||
out << DoublePrecision(6) << 1.23455810546875;
|
||||
out << EndSeq;
|
||||
ExpectEmit("- 1.23\n- 3.14159");
|
||||
ExpectEmit("- 1.31\n- 1.23456");
|
||||
}
|
||||
|
||||
TEST_F(EmitterTest, DashInBlockContext) {
|
||||
|
Reference in New Issue
Block a user