Browse Source

Util: Update Format::Builder floating-point precision

master
Riyyi 2 years ago
parent
commit
8049477795
  1. 9
      src/util/format/builder.cpp
  2. 6
      src/util/format/builder.h

9
src/util/format/builder.cpp

@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include <algorithm> // min
#include <cstddef> // size_t #include <cstddef> // size_t
#include <iomanip> // setprecision #include <iomanip> // setprecision
#include <ios> // defaultfloat, fixed #include <ios> // defaultfloat, fixed
@ -26,9 +27,9 @@ void Builder::putLiteral(std::string_view literal)
} }
} }
void Builder::putF32(float number, size_t precision) const void Builder::putF32(float number, uint8_t precision) const
{ {
precision = (precision > std::numeric_limits<float>::digits10) ? 6 : precision; precision = std::min(precision, static_cast<uint8_t>(std::numeric_limits<float>::digits10));
std::stringstream stream; std::stringstream stream;
stream stream
@ -39,9 +40,9 @@ void Builder::putF32(float number, size_t precision) const
m_builder << string; m_builder << string;
} }
void Builder::putF64(double number, size_t precision) const void Builder::putF64(double number, uint8_t precision) const
{ {
precision = (precision > std::numeric_limits<double>::digits10) ? 6 : precision; precision = std::min(precision, static_cast<uint8_t>(std::numeric_limits<double>::digits10));
std::stringstream stream; std::stringstream stream;
stream stream

6
src/util/format/builder.h

@ -7,7 +7,7 @@
#pragma once #pragma once
#include <cstddef> // size_t #include <cstddef> // size_t
#include <cstdint> // int32_t, uint32_t, int64_t #include <cstdint> // int32_t, int64_t, uint8_t, uint32_t
#include <sstream> // stringstream #include <sstream> // stringstream
#include <string_view> #include <string_view>
@ -26,8 +26,8 @@ public:
void putU32(uint32_t number) const { m_builder << number; } // unsigned int void putU32(uint32_t number) const { m_builder << number; } // unsigned int
void putI64(int64_t number) const { m_builder << number; } // long int void putI64(int64_t number) const { m_builder << number; } // long int
void putU64(size_t number) const { m_builder << number; } // long unsigned int void putU64(size_t number) const { m_builder << number; } // long unsigned int
void putF32(float number, size_t precision = 6) const; void putF32(float number, uint8_t precision = 6) const;
void putF64(double number, size_t precision = 6) const; void putF64(double number, uint8_t precision = 6) const;
void putCharacter(char character) const { m_builder.write(&character, 1); } void putCharacter(char character) const { m_builder.write(&character, 1); }
void putString(const std::string_view string) const { m_builder.write(string.data(), string.length()); } void putString(const std::string_view string) const { m_builder.write(string.data(), string.length()); }
void putPointer(const void* pointer) const { m_builder << pointer; } void putPointer(const void* pointer) const { m_builder << pointer; }

Loading…
Cancel
Save