blueloveTH vor 1 Jahr
Ursprung
Commit
24f3656356
2 geänderte Dateien mit 10 neuen und 0 gelöschten Zeilen
  1. 2 0
      include/pocketpy/pocketpy.h
  2. 8 0
      src/public/cast.c

+ 2 - 0
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`.

+ 8 - 0
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;