From e196733e880792ca75fce72e642383c777280aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Tue, 4 May 2021 12:51:03 +0200 Subject: [PATCH] Re-bind posmod, use int64_t instead of int Fixes #48420, fixes #48421. The binding was missed when moving GDScript built-in to Global Scope it seems. Co-authored-by: kleonc <9283098+kleonc@users.noreply.github.com> --- core/math/math_funcs.h | 4 +-- core/variant/variant_utility.cpp | 5 ++++ doc/classes/@GlobalScope.xml | 25 +++++++++++++++++++ .../visual_script_builtin_funcs.cpp | 2 +- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 8cf13efdb61..c0d7649b656 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -218,8 +218,8 @@ public: return value; } - static _ALWAYS_INLINE_ int posmod(int p_x, int p_y) { - int value = p_x % p_y; + static _ALWAYS_INLINE_ int64_t posmod(int64_t p_x, int64_t p_y) { + int64_t value = p_x % p_y; if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) { value += p_y; } diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index f154ab1ed6c..553f2b23a2a 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -93,6 +93,10 @@ struct VariantUtilityFunctions { return Math::fposmod(b, r); } + static inline int64_t posmod(int64_t b, int64_t r) { + return Math::posmod(b, r); + } + static inline double floor(double x) { return Math::floor(x); } @@ -1154,6 +1158,7 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(sqrt, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(posmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index 95108f16137..af66a11fe5b 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -652,6 +652,31 @@ Converts a 2D point expressed in the polar coordinate system (a distance from the origin [code]r[/code] and an angle [code]th[/code]) to the cartesian coordinate system (X and Y axis). + + + + + + + + + Returns the integer modulus of [code]x/y[/code] that wraps equally in positive and negative. + [codeblock] + for i in range(-3, 4): + print("%2d %2d %2d" % [i, i % 3, posmod(i, 3)]) + [/codeblock] + Produces: + [codeblock] + -3 0 0 + -2 -2 1 + -1 -1 2 + 0 0 0 + 1 1 1 + 2 2 2 + 3 0 0 + [/codeblock] + + diff --git a/modules/visual_script/visual_script_builtin_funcs.cpp b/modules/visual_script/visual_script_builtin_funcs.cpp index 7ca14fbca81..3b24de433cd 100644 --- a/modules/visual_script/visual_script_builtin_funcs.cpp +++ b/modules/visual_script/visual_script_builtin_funcs.cpp @@ -723,7 +723,7 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in case VisualScriptBuiltinFunc::MATH_POSMOD: { VALIDATE_ARG_NUM(0); VALIDATE_ARG_NUM(1); - *r_return = Math::posmod((int)*p_inputs[0], (int)*p_inputs[1]); + *r_return = Math::posmod((int64_t)*p_inputs[0], (int64_t)*p_inputs[1]); } break; case VisualScriptBuiltinFunc::MATH_FLOOR: { VALIDATE_ARG_NUM(0);