mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
make float
more precise
This commit is contained in:
parent
d350eb184b
commit
1a8fef1014
@ -101,6 +101,10 @@ struct NumberTraits<4> {
|
|||||||
static int_t stoi(Args&&... args) { return std::stoi(std::forward<Args>(args)...); }
|
static int_t stoi(Args&&... args) { return std::stoi(std::forward<Args>(args)...); }
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
|
static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
|
||||||
|
|
||||||
|
static constexpr int_t c0 = 0b00000000011111111111111111111100;
|
||||||
|
static constexpr int_t c1 = 0b11111111111111111111111111111100;
|
||||||
|
static constexpr int_t c2 = 0b00000000000000000000000000000011;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
@ -112,6 +116,10 @@ struct NumberTraits<8> {
|
|||||||
static int_t stoi(Args&&... args) { return std::stoll(std::forward<Args>(args)...); }
|
static int_t stoi(Args&&... args) { return std::stoll(std::forward<Args>(args)...); }
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
|
static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
|
||||||
|
|
||||||
|
static constexpr int_t c0 = 0b0000000000001111111111111111111111111111111111111111111111111100;
|
||||||
|
static constexpr int_t c1 = 0b1111111111111111111111111111111111111111111111111111111111111100;
|
||||||
|
static constexpr int_t c2 = 0b0000000000000000000000000000000000000000000000000000000000000011;
|
||||||
};
|
};
|
||||||
|
|
||||||
using Number = NumberTraits<sizeof(void*)>;
|
using Number = NumberTraits<sizeof(void*)>;
|
||||||
|
@ -343,7 +343,7 @@ inline void init_builtins(VM* _vm) {
|
|||||||
f64 val = _CAST(f64, obj);
|
f64 val = _CAST(f64, obj);
|
||||||
if(std::isinf(val) || std::isnan(val)) return VAR(std::to_string(val));
|
if(std::isinf(val) || std::isnan(val)) return VAR(std::to_string(val));
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << std::setprecision(std::numeric_limits<f64>::max_digits10-1-2) << val;
|
ss << std::setprecision(std::numeric_limits<f64>::max_digits10-2) << val;
|
||||||
std::string s = ss.str();
|
std::string s = ss.str();
|
||||||
if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0";
|
if(std::all_of(s.begin()+1, s.end(), isdigit)) s += ".0";
|
||||||
return VAR(s);
|
return VAR(s);
|
||||||
|
24
src/vm.h
24
src/vm.h
@ -709,24 +709,20 @@ PY_CAST_INT(unsigned long long)
|
|||||||
|
|
||||||
template<> inline float py_cast<float>(VM* vm, PyObject* obj){
|
template<> inline float py_cast<float>(VM* vm, PyObject* obj){
|
||||||
vm->check_float(obj);
|
vm->check_float(obj);
|
||||||
i64 bits = BITS(obj);
|
i64 bits = BITS(obj) & Number::c1;
|
||||||
bits = (bits >> 2) << 2;
|
|
||||||
return BitsCvt(bits)._float;
|
return BitsCvt(bits)._float;
|
||||||
}
|
}
|
||||||
template<> inline float _py_cast<float>(VM* vm, PyObject* obj){
|
template<> inline float _py_cast<float>(VM* vm, PyObject* obj){
|
||||||
i64 bits = BITS(obj);
|
i64 bits = BITS(obj) & Number::c1;
|
||||||
bits = (bits >> 2) << 2;
|
|
||||||
return BitsCvt(bits)._float;
|
return BitsCvt(bits)._float;
|
||||||
}
|
}
|
||||||
template<> inline double py_cast<double>(VM* vm, PyObject* obj){
|
template<> inline double py_cast<double>(VM* vm, PyObject* obj){
|
||||||
vm->check_float(obj);
|
vm->check_float(obj);
|
||||||
i64 bits = BITS(obj);
|
i64 bits = BITS(obj) & Number::c1;
|
||||||
bits = (bits >> 2) << 2;
|
|
||||||
return BitsCvt(bits)._float;
|
return BitsCvt(bits)._float;
|
||||||
}
|
}
|
||||||
template<> inline double _py_cast<double>(VM* vm, PyObject* obj){
|
template<> inline double _py_cast<double>(VM* vm, PyObject* obj){
|
||||||
i64 bits = BITS(obj);
|
i64 bits = BITS(obj) & Number::c1;
|
||||||
bits = (bits >> 2) << 2;
|
|
||||||
return BitsCvt(bits)._float;
|
return BitsCvt(bits)._float;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -752,11 +748,17 @@ PY_VAR_INT(unsigned int)
|
|||||||
PY_VAR_INT(unsigned long)
|
PY_VAR_INT(unsigned long)
|
||||||
PY_VAR_INT(unsigned long long)
|
PY_VAR_INT(unsigned long long)
|
||||||
|
|
||||||
|
|
||||||
#define PY_VAR_FLOAT(T) \
|
#define PY_VAR_FLOAT(T) \
|
||||||
inline PyObject* py_var(VM* vm, T _val){ \
|
inline PyObject* py_var(VM* vm, T _val){ \
|
||||||
f64 val = static_cast<f64>(_val); \
|
BitsCvt val(static_cast<f64>(_val)); \
|
||||||
i64 bits = BitsCvt(val)._int; \
|
i64 bits = val._int & Number::c1; \
|
||||||
bits = (bits >> 2) << 2; \
|
i64 tail = val._int & Number::c2; \
|
||||||
|
if(tail == 0b10){ \
|
||||||
|
if((bits&Number::c0)!=Number::c0 && (bits&0b100)) bits += 0b100; \
|
||||||
|
}else if(tail == 0b11){ \
|
||||||
|
if((bits&Number::c0)!=Number::c0) bits += 0b100; \
|
||||||
|
} \
|
||||||
bits |= 0b10; \
|
bits |= 0b10; \
|
||||||
return reinterpret_cast<PyObject*>(bits); \
|
return reinterpret_cast<PyObject*>(bits); \
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user