From e8b1449979e22668984323214aaddc4aae12e273 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 16 Oct 2023 00:08:26 +0800 Subject: [PATCH] ... --- include/pocketpy/str.h | 10 ++++++++++ src/pocketpy.cpp | 9 +++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index a3905977..d0b85f2b 100644 --- a/include/pocketpy/str.h +++ b/include/pocketpy/str.h @@ -183,6 +183,16 @@ struct SStream{ template SStream& operator<<(T val){ + if constexpr(std::is_floating_point_v){ + if(std::isinf(val) || std::isnan(val)){ + return (*this) << std::to_string(val); + } + std::stringstream ss; // float + ss << std::setprecision(std::numeric_limits::max_digits10-1) << val; + std::string s = ss.str(); + if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0"; + return (*this) << s; + } (*this) << std::to_string(val); return *this; } diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 2788d83f..5fc50fd2 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -541,12 +541,9 @@ void init_builtins(VM* _vm) { _vm->bind__repr__(_vm->tp_float, [](VM* vm, PyObject* obj) { f64 val = _CAST(f64, obj); - if(std::isinf(val) || std::isnan(val)) return VAR(std::to_string(val)); - std::stringstream ss; // float - ss << std::setprecision(std::numeric_limits::max_digits10-1) << val; - std::string s = ss.str(); - if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0"; - return VAR(s); + SStream ss; + ss << val; + return VAR(ss.str()); }); /************ str ************/