diff --git a/93_deterministic_float.py b/93_deterministic_float.py new file mode 100644 index 00000000..12e69ccb --- /dev/null +++ b/93_deterministic_float.py @@ -0,0 +1,96 @@ +import math + +assert math.pi == 3.14159265358979323846 +assert math.e == 2.7182818284590452354 +assert math.inf == math.inf +assert math.nan != math.nan + +assert math.ceil(math.pi) == 4.0 +assert math.ceil(-math.e) == -2.0 +assert math.ceil(math.inf) == math.inf +assert math.fabs(math.pi) == 3.14159265358979323846 +assert math.fabs(-math.pi) == 3.14159265358979323846 +assert math.floor(math.pi) == 3.0 +assert math.floor(-math.e) == -3.0 +assert math.trunc(math.pi) == 3.0 + +assert math.fsum([math.pi]*100) == 314.159265358979323846 +assert math.gcd(10, 5) == 5 +assert math.gcd(10, 6) == 2 +assert math.gcd(10, 7) == 1 +assert math.gcd(10, 10) == 10 +assert math.gcd(-10, 10) == 10 + +assert math.isfinite(math.pi) == 1 +assert math.isfinite(math.inf) == 0 +assert math.isfinite(math.nan) == 0 +assert math.isinf(math.pi) == 0 +assert math.isinf(math.inf) == 1 +assert math.isinf(math.nan) == 0 +assert math.isnan(math.pi) == 0 +assert math.isnan(math.inf) == 0 +assert math.isnan(math.nan) == 1 + +assert math.isclose(math.pi, 3.14159265358979323846) +assert math.isclose(math.sqrt(3), 1.732050807568877) + +assert math.exp(0) == 1.0 +assert math.exp(1) == math.e +assert math.exp(1.5) == 4.481689070338065 - 8.881784197001252e-16 +assert math.exp(3) == 20.08553692318767 - 3.552713678800501e-15 +assert math.exp(-3) == 0.04978706836786394 + 6.938893903907228e-18 +assert math.log(0) == -math.inf +assert math.log(1) == 0.0 +assert math.log(2) == 0.69314718055994530942 +assert math.log(math.e) == 1.0 +assert math.log(10) == 2.30258509299404568402 +assert math.log2(math.e) == 1.4426950408889634074 +assert math.log10(math.e) == 0.43429448190325182765 + +assert math.pow(2,2) == 4.0 +assert math.pow(1.41421356237309504880, 2) == 2.0 + 4.440892098500626e-16 +assert math.pow(0.70710678118654752440, 2) == 0.5000000000000001 +assert math.sqrt(2) == 1.41421356237309504880 +assert math.sqrt(math.pi) == 1.772453850905516 - 2.220446049250313e-16 + +assert math.cos(0) == 1.0 +assert math.cos(math.pi/2) == 6.123233995736766e-17 +assert math.cos(math.pi) == -1.0 +assert math.sin(0) == 0.0 +assert math.sin(math.pi/2) == 1.0 +assert math.sin(math.pi) == 1.224646799147353e-16 + 2.465190328815662e-32 +assert math.tan(0) == 0.0 +assert math.tan(math.pi/2) == 1.633123935319537e+16 +assert math.tan(math.pi) == -1.224646799147353e-16 - 2.465190328815662e-32 + +assert math.acos(0) == 1.570796326794897 - 4.440892098500626e-16 +assert math.acos(1) == 0.0 +assert math.asin(0) == 0.0 +assert math.asin(1) == 1.570796326794897 - 4.440892098500626e-16 +assert math.atan(0) == 0.0 +assert math.atan(1) == 0.7853981633974483 + +assert math.atan2(math.pi/4, math.pi/4) == 0.7853981633974483 +assert math.atan2(-math.pi/4, math.pi/4) == -0.7853981633974483 +assert math.atan2(-math.pi/4, -math.pi/4) == -2.356194490192345 +assert math.atan2(math.pi/4, -math.pi/4) == 2.356194490192345 + +assert math.fmod(-2.0, 3.0) == -2.0 +assert math.fmod(2.0, 3.0) == 2.0 +assert math.fmod(4.0, 3.0) == 1.0 +assert math.fmod(-4.0, 3.0) == -1.0 + +x, y = math.modf(math.pi) +assert x == 0.14159265358979323846 - 1.110223024625157e-16 +assert y == 3.0 + +x, y = math.modf(-math.e) +assert x == -0.7182818284590451 +assert y == -2.0 + +assert math.factorial(0) == 1 +assert math.factorial(1) == 1 +assert math.factorial(2) == 2 +assert math.factorial(3) == 6 +assert math.factorial(4) == 24 +assert math.factorial(5) == 120 diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 43af92a5..9d44b3f5 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -41,7 +41,7 @@ #define PK_MAX_MODULE_PATH_LEN 63 // This is some math constants -#define PK_M_PI 3.1415926535897932384 +#define PK_M_PI 3.14159265358979323846 #define PK_M_E 2.7182818284590452354 #define PK_M_DEG2RAD 0.017453292519943295 #define PK_M_RAD2DEG 57.29577951308232 diff --git a/src/modules/vmath.c b/src/modules/vmath.c index acd5723d..647caeda 100644 --- a/src/modules/vmath.c +++ b/src/modules/vmath.c @@ -196,7 +196,7 @@ static py_Ref _const(py_Type type, const char* name) { float sum = 0; \ for(int i = 0; i < D; i++) \ sum += v.data[i] * v.data[i]; \ - py_newfloat(py_retval(), sqrtf(sum)); \ + py_newfloat(py_retval(), sqrt(sum)); \ return true; \ } \ static bool vec##D##_length_squared(int argc, py_Ref argv) { \ @@ -226,7 +226,7 @@ static py_Ref _const(py_Type type, const char* name) { for(int i = 0; i < D; i++) \ len += self.data[i] * self.data[i]; \ if(isclose(len, 0)) return ZeroDivisionError("cannot normalize zero vector"); \ - len = sqrtf(len); \ + len = sqrt(len); \ c11_vec##D res; \ for(int i = 0; i < D; i++) \ res.data[i] = self.data[i] / len; \ @@ -347,8 +347,8 @@ static bool vec2_rotate(int argc, py_Ref argv) { PY_CHECK_ARGC(2); py_f64 radians; if(!py_castfloat(&argv[1], &radians)) return false; - float cr = cosf(radians); - float sr = sinf(radians); + float cr = cos(radians); + float sr = sin(radians); c11_vec2 res; res.x = argv[0]._vec2.x * cr - argv[0]._vec2.y * sr; res.y = argv[0]._vec2.x * sr + argv[0]._vec2.y * cr; @@ -360,7 +360,7 @@ static bool vec2_angle_STATIC(int argc, py_Ref argv) { PY_CHECK_ARGC(2); PY_CHECK_ARG_TYPE(0, tp_vec2); PY_CHECK_ARG_TYPE(1, tp_vec2); - float val = atan2f(argv[1]._vec2.y, argv[1]._vec2.x) - atan2f(argv[0]._vec2.y, argv[0]._vec2.x); + float val = atan2(argv[1]._vec2.y, argv[1]._vec2.x) - atan2(argv[0]._vec2.y, argv[0]._vec2.x); if(val > PK_M_PI) val -= 2 * (float)PK_M_PI; if(val < -PK_M_PI) val += 2 * (float)PK_M_PI; py_newfloat(py_retval(), val); @@ -401,7 +401,7 @@ static bool vec2_smoothdamp_STATIC(int argc, py_Ref argv) { float maxChangeSq = maxChange * maxChange; float sqDist = change_x * change_x + change_y * change_y; if(sqDist > maxChangeSq) { - float mag = sqrtf(sqDist); + float mag = sqrt(sqDist); change_x = change_x / mag * maxChange; change_y = change_y / mag * maxChange; } @@ -579,8 +579,8 @@ static bool inverse(const c11_mat3x3* m, c11_mat3x3* out) { } static void trs(c11_vec2 t, float r, c11_vec2 s, c11_mat3x3* out) { - float cr = cosf(r); - float sr = sinf(r); + float cr = cos(r); + float sr = sin(r); // clang-format off *out = (c11_mat3x3){ ._11 = s.x * cr, ._12 = -s.y * sr, ._13 = t.x, @@ -736,7 +736,7 @@ static bool mat3x3_t(int argc, py_Ref argv) { static bool mat3x3_r(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_mat3x3* ud = py_tomat3x3(argv); - float r = atan2f(ud->_21, ud->_11); + float r = atan2(ud->_21, ud->_11); py_newfloat(py_retval(), r); return true; } @@ -745,8 +745,8 @@ static bool mat3x3_s(int argc, py_Ref argv) { PY_CHECK_ARGC(1); c11_mat3x3* ud = py_tomat3x3(argv); c11_vec2 res; - res.x = sqrtf(ud->_11 * ud->_11 + ud->_21 * ud->_21); - res.y = sqrtf(ud->_12 * ud->_12 + ud->_22 * ud->_22); + res.x = sqrt(ud->_11 * ud->_11 + ud->_21 * ud->_21); + res.y = sqrt(ud->_12 * ud->_12 + ud->_22 * ud->_22); py_newvec2(py_retval(), res); return true; }