This commit is contained in:
blueloveTH 2023-10-16 00:08:26 +08:00
parent 5f6c7f3f23
commit e8b1449979
2 changed files with 13 additions and 6 deletions

View File

@ -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;
}

View File

@ -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 ************/