FixedDouble.

GitOrigin-RevId: 6779a7885b0ddfa80321b3ccd368524725b27f59
This commit is contained in:
levlam 2018-02-11 14:38:08 +03:00
parent ecf5405d86
commit 13def6a897
3 changed files with 32 additions and 6 deletions

View File

@ -63,7 +63,7 @@ StringBuilder &StringBuilder::operator<<(long long unsigned int x) {
return *this;
}
StringBuilder &StringBuilder::operator<<(double x) {
StringBuilder &StringBuilder::operator<<(FixedDouble x) {
if (unlikely(end_ptr_ < current_ptr_)) {
return on_error();
}
@ -76,7 +76,8 @@ StringBuilder &StringBuilder::operator<<(double x) {
ss->str(std::string());
ss->clear();
}
*ss << x;
ss->precision(x.precision);
*ss << x.d;
int len = narrow_cast<int>(static_cast<std::streamoff>(ss->tellp()));
auto left = end_ptr_ + reserved_size - current_ptr_;

View File

@ -94,7 +94,18 @@ class StringBuilder {
StringBuilder &operator<<(long long unsigned int x);
StringBuilder &operator<<(double x);
struct FixedDouble {
double d;
int precision;
FixedDouble(double d, int precision) : d(d), precision(precision) {
}
};
StringBuilder &operator<<(FixedDouble x);
StringBuilder &operator<<(double x) {
return *this << FixedDouble(x, 6);
}
StringBuilder &operator<<(const void *ptr);

View File

@ -191,8 +191,8 @@ TEST(Misc, to_integer) {
ASSERT_TRUE(to_integer_safe<uint64>("-12345678910111213").is_error());
}
static void test_to_double_one(CSlice str, Slice expected) {
auto result = PSTRING() << to_double(str);
static void test_to_double_one(CSlice str, Slice expected, int precision = 6) {
auto result = PSTRING() << td::StringBuilder::FixedDouble(to_double(str), precision);
if (expected != result) {
LOG(ERROR) << "To double conversion failed: have " << str << ", expected " << expected << ", parsed "
<< to_double(str) << ", got " << result;
@ -210,10 +210,24 @@ static void test_to_double() {
test_to_double_one(" inFasdasd", "0.000000");
test_to_double_one(" NaN", "nan");
test_to_double_one(" 12345678910111213141516171819 asdasd", "12345678910111213670658736128.000000");
test_to_double_one("1.234567891011121314E123", "1234567891011121363209105003376291141757777526749278953577304234065881343284952489418916814035346625663604561924259911303168.000000");
test_to_double_one("1.234567891011121314E123",
"1234567891011121363209105003376291141757777526749278953577304234065881343284952489418916814035346"
"625663604561924259911303168.000000");
test_to_double_one("1.234567891011121314E-9", "0.000000");
test_to_double_one("123456789", "123456789.000000");
test_to_double_one("-1,234567891011121314E123", "-1.000000");
test_to_double_one("123456789", "123456789", 0);
test_to_double_one("1.23456789", "1", 0);
test_to_double_one("1.23456789", "1.2", 1);
test_to_double_one("1.23456789", "1.23", 2);
test_to_double_one("1.23456789", "1.235", 3);
test_to_double_one("1.23456789", "1.2346", 4);
test_to_double_one("1.23456789", "1.23457", 5);
test_to_double_one("1.23456789", "1.234568", 6);
test_to_double_one("1.23456789", "1.2345679", 7);
test_to_double_one("1.23456789", "1.23456789", 8);
test_to_double_one("1.23456789", "1.234567890", 9);
test_to_double_one("1.23456789", "1.2345678900", 10);
}
TEST(Misc, to_double) {