Update pocketpy.h

This commit is contained in:
blueloveTH 2022-12-10 21:49:18 +08:00
parent 229a836444
commit c5b263d5a8
7 changed files with 58 additions and 8 deletions

View File

@ -17,7 +17,9 @@ The initial version. Hello, world!
+ Fix a bug about comment and indentation + Fix a bug about comment and indentation
+ Fix a bug about compile error line number + Fix a bug about compile error line number
## 0.4.8+4 ## 0.4.8+5
+ Downgrade to `sdk>=2.17.0` + Downgrade to `sdk>=2.17.0`
+ Fix some bugs about compile error + Fix some bugs about compile error
+ Fix a bug for jsonify `nan` or `inf`
+ Add `math.isnan` and `math.isinf`

View File

@ -1,6 +1,6 @@
name: pocketpy name: pocketpy
description: A lightweight Python interpreter for game engines. description: A lightweight Python interpreter for game engines.
version: 0.4.8+4 version: 0.4.8+5
homepage: https://pocketpy.dev homepage: https://pocketpy.dev
repository: https://github.com/blueloveth/pocketpy repository: https://github.com/blueloveth/pocketpy

View File

@ -6481,7 +6481,11 @@ void __initializeBuiltinFunctions(VM* _vm) {
}); });
_vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) { _vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) {
return vm->PyStr(std::to_string((float)vm->PyFloat_AS_C(args[0]))); _Float val = vm->PyFloat_AS_C(args[0]);
if(std::isinf(val) || std::isnan(val)){
vm->valueError("cannot jsonify 'nan' or 'inf'");
}
return vm->PyStr(std::to_string(val));
}); });
/************ PyString ************/ /************ PyString ************/
@ -6887,6 +6891,16 @@ void __addModuleMath(VM* vm){
_Float b = vm->numToFloat(args[1]); _Float b = vm->numToFloat(args[1]);
return vm->PyBool(fabs(a - b) < 1e-9); return vm->PyBool(fabs(a - b) < 1e-9);
}); });
vm->bindFunc(mod, "isnan", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkArgSize(args, 1);
return vm->PyBool(std::isnan(vm->numToFloat(args[0])));
});
vm->bindFunc(mod, "isinf", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkArgSize(args, 1);
return vm->PyBool(std::isinf(vm->numToFloat(args[0])));
});
} }
class _PkExported{ class _PkExported{

@ -1 +1 @@
Subproject commit c645191b67d056996ee4d0d6a387758c16edf7e2 Subproject commit b1d3e4f63e539272d5b77bfb44ed8f5778efef9e

View File

@ -6481,7 +6481,11 @@ void __initializeBuiltinFunctions(VM* _vm) {
}); });
_vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) { _vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) {
return vm->PyStr(std::to_string((float)vm->PyFloat_AS_C(args[0]))); _Float val = vm->PyFloat_AS_C(args[0]);
if(std::isinf(val) || std::isnan(val)){
vm->valueError("cannot jsonify 'nan' or 'inf'");
}
return vm->PyStr(std::to_string(val));
}); });
/************ PyString ************/ /************ PyString ************/
@ -6887,6 +6891,16 @@ void __addModuleMath(VM* vm){
_Float b = vm->numToFloat(args[1]); _Float b = vm->numToFloat(args[1]);
return vm->PyBool(fabs(a - b) < 1e-9); return vm->PyBool(fabs(a - b) < 1e-9);
}); });
vm->bindFunc(mod, "isnan", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkArgSize(args, 1);
return vm->PyBool(std::isnan(vm->numToFloat(args[0])));
});
vm->bindFunc(mod, "isinf", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkArgSize(args, 1);
return vm->PyBool(std::isinf(vm->numToFloat(args[0])));
});
} }
class _PkExported{ class _PkExported{

View File

@ -280,7 +280,11 @@ void __initializeBuiltinFunctions(VM* _vm) {
}); });
_vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) { _vm->bindMethod("float", "__json__", [](VM* vm, const pkpy::ArgList& args) {
return vm->PyStr(std::to_string((float)vm->PyFloat_AS_C(args[0]))); _Float val = vm->PyFloat_AS_C(args[0]);
if(std::isinf(val) || std::isnan(val)){
vm->valueError("cannot jsonify 'nan' or 'inf'");
}
return vm->PyStr(std::to_string(val));
}); });
/************ PyString ************/ /************ PyString ************/
@ -688,6 +692,16 @@ void __addModuleMath(VM* vm){
_Float b = vm->numToFloat(args[1]); _Float b = vm->numToFloat(args[1]);
return vm->PyBool(fabs(a - b) < 1e-9); return vm->PyBool(fabs(a - b) < 1e-9);
}); });
vm->bindFunc(mod, "isnan", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkArgSize(args, 1);
return vm->PyBool(std::isnan(vm->numToFloat(args[0])));
});
vm->bindFunc(mod, "isinf", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkArgSize(args, 1);
return vm->PyBool(std::isinf(vm->numToFloat(args[0])));
});
} }
class _PkExported{ class _PkExported{

View File

@ -1,4 +1,4 @@
from math import log, log10, log2, sin, cos, tan, e, pi, isclose from math import log, log10, log2, sin, cos, tan, e, pi, isclose, isnan, isinf
assert isclose(e, 2.718281828459045) assert isclose(e, 2.718281828459045)
assert isclose(pi, 3.141592653589793) assert isclose(pi, 3.141592653589793)
@ -8,3 +8,9 @@ assert isclose(log2(10), 3.321928094887362)
assert isclose(sin(0), 0.0) assert isclose(sin(0), 0.0)
assert isclose(cos(0), 1.0) assert isclose(cos(0), 1.0)
assert isclose(tan(0), 0.0) assert isclose(tan(0), 0.0)
a = -0.1
a = a**a
assert isnan(a)
assert not isinf(a)
assert isinf(float("inf"))