blueloveTH 2 лет назад
Родитель
Сommit
e8b1449979
2 измененных файлов с 13 добавлено и 6 удалено
  1. 10 0
      include/pocketpy/str.h
  2. 3 6
      src/pocketpy.cpp

+ 10 - 0
include/pocketpy/str.h

@@ -183,6 +183,16 @@ struct SStream{
 
     template<typename T>
     SStream& operator<<(T val){
+        if constexpr(std::is_floating_point_v<T>){
+            if(std::isinf(val) || std::isnan(val)){
+                return (*this) << std::to_string(val);
+            }
+            std::stringstream ss; // float
+            ss << std::setprecision(std::numeric_limits<f64>::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;
     }

+ 3 - 6
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<f64>::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 ************/