ToString function now returns double values with precision 15 (#1576)

The DoubleToString function has been updated to handle higher precision doubles correctly. The unnecessary string length restriction has been removed, allowing the function to convert the full double value without prematurely truncating it. This change ensures that the string representation of doubles is more accurate, especially for very large or very small numbers. Unit tests have been added to verify the correct behavior for a range of double values.
This commit is contained in:
Aidar Samerkhanov
2024-01-12 11:32:34 +03:00
committed by GitHub
parent 2e4d27c59a
commit c772cab766
4 changed files with 53 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2023 Memgraph Ltd.
// Copyright 2024 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@@ -171,3 +171,22 @@ TEST(String, Substr) {
EXPECT_EQ(Substr(string, string.size() - 1, 1), string.substr(string.size() - 1, 1));
EXPECT_EQ(Substr(string, string.size() - 1, 2), string.substr(string.size() - 1, 2));
}
TEST(String, DoubleToString) {
EXPECT_EQ(DoubleToString(0), "0");
EXPECT_EQ(DoubleToString(1), "1");
EXPECT_EQ(DoubleToString(1234567890123456), "1234567890123456");
EXPECT_EQ(DoubleToString(static_cast<double>(12345678901234567)), "12345678901234568");
EXPECT_EQ(DoubleToString(0.5), "0.5");
EXPECT_EQ(DoubleToString(1.0), "1");
EXPECT_EQ(DoubleToString(5.8), "5.8");
EXPECT_EQ(DoubleToString(1.01234000), "1.01234");
EXPECT_EQ(DoubleToString(1.036837585345), "1.036837585345");
EXPECT_EQ(DoubleToString(103.6837585345), "103.683758534500001");
EXPECT_EQ(DoubleToString(1.01234567890123456789), "1.012345678901235");
EXPECT_EQ(DoubleToString(1234567.01234567891234567), "1234567.012345678871498");
EXPECT_EQ(DoubleToString(0.00001), "0.00001");
EXPECT_EQ(DoubleToString(0.00000000000001), "0.00000000000001");
EXPECT_EQ(DoubleToString(0.000000000000001), "0.000000000000001");
EXPECT_EQ(DoubleToString(0.0000000000000001), "0");
}