From 24f3656356f660db5d44f8c69537bf101edee269 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 30 Aug 2024 18:14:44 +0800 Subject: [PATCH] ... --- include/pocketpy/pocketpy.h | 2 ++ src/public/cast.c | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index f48a946a..52ff7e9c 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -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`. diff --git a/src/public/cast.c b/src/public/cast.c index 4b5b7811..21c113dd 100644 --- a/src/public/cast.c +++ b/src/public/cast.c @@ -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;