This commit is contained in:
blueloveTH 2024-08-30 18:14:44 +08:00
parent 19563e33d2
commit 24f3656356
2 changed files with 10 additions and 0 deletions

View File

@ -233,6 +233,8 @@ PK_EXPORT py_f64 py_tofloat(py_Ref);
/// If successful, return true and set the value to `out`.
/// Otherwise, return false and raise `TypeError`.
PK_EXPORT bool py_castfloat(py_Ref, py_f64* out) PY_RAISE;
/// 32-bit version of `py_castfloat`.
PK_EXPORT bool py_castfloat32(py_Ref, float* out) PY_RAISE;
/// Cast a `int` object in python to `int64_t`.
PK_EXPORT bool py_castint(py_Ref, py_i64* out) PY_RAISE;
/// Convert a `bool` object in python to `bool`.

View File

@ -24,6 +24,14 @@ bool py_castfloat(py_Ref self, double* out) {
}
}
bool py_castfloat32(py_Ref self, float *out){
switch(self->type) {
case tp_int: *out = (float)self->_i64; return true;
case tp_float: *out = (float)self->_f64; return true;
default: return TypeError("expected 'int' or 'float', got '%t'", self->type);
}
}
bool py_castint(py_Ref self, int64_t* out) {
if(self->type == tp_int) {
*out = self->_i64;