diff options
author | Sven Gothel <[email protected]> | 2022-08-05 09:41:15 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-08-05 09:41:15 +0200 |
commit | 0347d71dd0cadb3e19196afbdf5b4b0c8d904eb1 (patch) | |
tree | 313fe30d60d2ad16c705f1da8561c5683725d4b5 /include | |
parent | b5c4e7dd96c00029b15a7bc638d916abc7197d0a (diff) |
fraction<>::to_string(): Replace std::ostringstream precision w/ snprintf()
Diffstat (limited to 'include')
-rw-r--r-- | include/jau/fraction_type.hpp | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/include/jau/fraction_type.hpp b/include/jau/fraction_type.hpp index fbe9d74..408d2eb 100644 --- a/include/jau/fraction_type.hpp +++ b/include/jau/fraction_type.hpp @@ -32,13 +32,13 @@ #include <stdio.h> #include <cinttypes> -#include <sstream> #include <cstdint> #include <cmath> #include <jau/int_types.hpp> #include <jau/int_math.hpp> #include <jau/ordered_atomic.hpp> +#include <jau/cpp_pragma.hpp> namespace jau { @@ -361,10 +361,17 @@ namespace jau { if( overflow ) { r.append(" O! "); } else if( show_double ) { - std::ostringstream out; - out.precision( std::max<nsize_t>( 6, digits10(denom, false /* sign_is_digit */) ) ); - out << to_double(); - r.append(" ( " + out.str() + " )"); + const int precision = std::max<int>( 6, digits10(denom, false /* sign_is_digit */) ); + std::string fmt("%."); + fmt.append(std::to_string(precision)).append("f"); + char buf[96]; + PRAGMA_DISABLE_WARNING_PUSH + PRAGMA_DISABLE_WARNING_FORMAT_NONLITERAL + int res = ::snprintf(buf, sizeof(buf), fmt.c_str(), to_double()); + PRAGMA_DISABLE_WARNING_POP + if( 0 < res ) { + r.append(" ( " + std::string(buf) + " )"); + } } return r; } |