From 52ff3e8fc1d210dec28a7d829dd435577e73a603 Mon Sep 17 00:00:00 2001 From: Corentin Schreiber Date: Mon, 29 Apr 2024 08:41:03 +0100 Subject: [PATCH] Fix 1e-6 not having the right number of digits --- include/snitch/snitch_append.hpp | 5 +++-- tests/runtime_tests/string_utility.cpp | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/snitch/snitch_append.hpp b/include/snitch/snitch_append.hpp index 86c37ee3..c9549054 100644 --- a/include/snitch/snitch_append.hpp +++ b/include/snitch/snitch_append.hpp @@ -144,12 +144,13 @@ set_precision(signed_fixed_data fd, std::size_t p) noexcept { only_zero = false; } fd.digits = fd.digits / 10u; + base_digits -= 1u; } else { - fd.digits = round_half_to_even(fd.digits, only_zero); + fd.digits = round_half_to_even(fd.digits, only_zero); + base_digits = num_digits(static_cast(fd.digits)); } fd.exponent += 1; - base_digits -= 1u; } return fd; diff --git a/tests/runtime_tests/string_utility.cpp b/tests/runtime_tests/string_utility.cpp index 14173fb3..a14b667d 100644 --- a/tests/runtime_tests/string_utility.cpp +++ b/tests/runtime_tests/string_utility.cpp @@ -375,6 +375,7 @@ TEST_CASE("append floats", "[utility]") { CONSTEXPR_CHECK(a(-1.0f) == ae{"-1.000000e+00"sv, true}); CONSTEXPR_CHECK(a(10.0f) == ae{"1.000000e+01"sv, true}); CONSTEXPR_CHECK(a(1e4f) == ae{"1.000000e+04"sv, true}); + CONSTEXPR_CHECK(a(1e-6f) == ae{"1.000000e-06"sv, true}); // The number below is a tricky one: it is exactly representable, but intermediate // calculations requires more digits than can be stored on fixed-point 64 bits. // Furthermore, rounding is an exact tie, and exposes the round-half-to-even behavior. @@ -496,6 +497,7 @@ TEST_CASE("append doubles", "[utility]") { CONSTEXPR_CHECK(a(-1.0) == ae{"-1.000000000000000e+00"sv, true}); CONSTEXPR_CHECK(a(10.0) == ae{"1.000000000000000e+01"sv, true}); CONSTEXPR_CHECK(a(1e4) == ae{"1.000000000000000e+04"sv, true}); + CONSTEXPR_CHECK(a(1e-6) == ae{"1.000000000000000e-06"sv, true}); CONSTEXPR_CHECK(a(2.3456e301) == ae{"2.345600000000000e+301"sv, true}); CONSTEXPR_CHECK(a(-2.3456e301) == ae{"-2.345600000000000e+301"sv, true}); CONSTEXPR_CHECK(a(1.797693134862315e308) == ae{"1.797693134862315e+308"sv, true});