Skip to content

Commit

Permalink
Support formatting for f32 and f64
Browse files Browse the repository at this point in the history
  • Loading branch information
danakj committed May 31, 2023
1 parent 2e7d1ba commit 25167e6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions subspace/num/__private/float_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,14 @@
struct fmt::formatter<::sus::num::T, Char> { \
template <typename ParseContext> \
constexpr decltype(auto) parse(ParseContext& ctx) { \
return ctx.begin(); \
return underlying_.parse(ctx); \
} \
\
template <typename FormatContext> \
constexpr auto format(::sus::num::T t, FormatContext& ctx) const { \
return format_to(ctx.out(), "{}", t.primitive_value); \
return underlying_.format(t.primitive_value, ctx); \
} \
\
private: \
fmt::formatter<PrimitiveT, Char> underlying_; \
};
2 changes: 2 additions & 0 deletions subspace/num/f32_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,8 @@ TEST(f32, fmt) {
static_assert(fmt::is_formattable<f32, char>::value);
EXPECT_EQ(fmt::format("{}", -123.456_f32), "-123.456");
EXPECT_EQ(fmt::format("{}", 1234.567_f32), "1234.567");
EXPECT_EQ(fmt::format("{0:.1f}", 1234.567_f32), "1234.6");
EXPECT_EQ(fmt::format("{0:.4f}", 1234.567_f32), "1234.5670");
}

} // namespace
2 changes: 2 additions & 0 deletions subspace/num/f64_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,8 @@ TEST(f64, fmt) {
static_assert(fmt::is_formattable<f64, char>::value);
EXPECT_EQ(fmt::format("{}", -123789.456_f64), "-123789.456");
EXPECT_EQ(fmt::format("{}", 1234890.567_f64), "1234890.567");
EXPECT_EQ(fmt::format("{0:.1f}", 1234890.567_f64), "1234890.6");
EXPECT_EQ(fmt::format("{0:.4f}", 1234890.567_f64), "1234890.5670");
}

} // namespace
2 changes: 1 addition & 1 deletion subspace/num/float.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <concepts>
#include <functional> // TODO: remove this but we need to hash things > size_t.

#include "fmt/core.h"
#include "fmt/format.h"
#include "subspace/macros/__private/compiler_bugs.h"
#include "subspace/macros/pure.h"
#include "subspace/marker/unsafe.h"
Expand Down

0 comments on commit 25167e6

Please sign in to comment.