Skip to content

Commit

Permalink
Fix 1e-6 not having the right number of digits
Browse files Browse the repository at this point in the history
  • Loading branch information
cschreib authored and CrustyAuklet committed Apr 30, 2024
1 parent dc67a89 commit dea6d07
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions include/snitch/snitch_append.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,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<large_uint_t>(fd.digits));
}

fd.exponent += 1;
base_digits -= 1u;
}

return fd;
Expand Down
2 changes: 2 additions & 0 deletions tests/runtime_tests/string_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,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.
Expand Down Expand Up @@ -502,6 +503,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});
Expand Down

0 comments on commit dea6d07

Please sign in to comment.