Convert math module to static Math class

This commit is contained in:
2026-11-23 10:22:11 +01:00
parent c981572bae
commit 2243557bf5
6 changed files with 865 additions and 861 deletions
-53
View File
@@ -202,58 +202,6 @@ static int PH7_builtin_is_resource(ph7_context *pCtx, int nArg, ph7_value **apAr
ph7_result_bool(pCtx, res);
return PH7_OK;
}
/*
* float round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
* Exponential expression.
* Parameter
* $val
* The value to round.
* $precision
* The optional number of decimal digits to round to.
* $mode
* One of PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, or PHP_ROUND_HALF_ODD.
* (not supported).
* Return
* The rounded value.
*/
static int PH7_builtin_round(ph7_context *pCtx, int nArg, ph7_value **apArg) {
int n = 0;
double r;
if(nArg < 1) {
/* Missing argument, return 0 */
ph7_result_int(pCtx, 0);
return PH7_OK;
}
/* Extract the precision if available */
if(nArg > 1) {
n = ph7_value_to_int(apArg[1]);
if(n > 30) {
n = 30;
}
if(n < 0) {
n = 0;
}
}
r = ph7_value_to_double(apArg[0]);
/* If Y==0 and X will fit in a 64-bit int,
* handle the rounding directly.Otherwise
* use our own custom printf [i.e:SyBufferFormat()].
*/
if(n == 0 && r >= 0 && r < LARGEST_INT64 - 1) {
r = (double)((ph7_int64)(r + 0.5));
} else if(n == 0 && r < 0 && (-r) < LARGEST_INT64 - 1) {
r = -(double)((ph7_int64)((-r) + 0.5));
} else {
char zBuf[256];
sxu32 nLen;
nLen = SyBufferFormat(zBuf, sizeof(zBuf), "%.*f", n, r);
/* Convert the string to real number */
SyStrToReal(zBuf, nLen, (void *)&r, 0);
}
/* Return thr rounded value */
ph7_result_double(pCtx, r);
return PH7_OK;
}
/*
* string dechex(int $number)
* Decimal to hexadecimal.
@@ -7456,7 +7404,6 @@ static const ph7_builtin_func aBuiltInFunc[] = {
{ "is_string", PH7_builtin_is_string },
{ "is_void", PH7_builtin_is_void },
{ "is_numeric", PH7_builtin_is_numeric },
{ "round", PH7_builtin_round },
{ "dechex", PH7_builtin_dechex },
{ "decoct", PH7_builtin_decoct },
{ "decbin", PH7_builtin_decbin },
+849 -711
View File
File diff suppressed because it is too large Load Diff
+8 -91
View File
@@ -2,7 +2,7 @@
* @PROJECT AerScript Interpreter
* @COPYRIGHT See COPYING in the top level directory
* @FILE modules/math/math.h
* @DESCRIPTION Mathematical functions module for AerScript Interpreter
* @DESCRIPTION Mathematical class module for AerScript Interpreter
* @DEVELOPERS Symisc Systems <devel@symisc.net>
* Rafal Kupiec <belliash@codingworkshop.eu.org>
*/
@@ -18,97 +18,14 @@
#define MODULE_DESC "Math Module"
#define MODULE_VER 1.0
#define PH7_PI 3.1415926535898
/* MathException error codes. */
#define MATH_ERROR_NONE 0
#define MATH_ERROR_ARGUMENT_OUT_OF_RANGE 1
#define MATH_ERROR_ARGUMENT 2
#define MATH_ERROR_OVERFLOW 3
#define MATH_ERROR_ARITHMETIC 4
#define MATH_ERROR_DIVIDE_BY_ZERO 5
/* Forward reference & declaration */
static void PH7_M_PI_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_TAU_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_E_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_LOG2E_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_LOG10E_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_LN2_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_LN10_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_PI_2_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_PI_4_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_1_PI_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_2_PI_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_SQRTPI_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_2_SQRTPI_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_SQRT2_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_SQRT3_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_SQRT1_2_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_LNPI_Const(ph7_value *pVal, void *pUserData);
static void PH7_M_EULER_Const(ph7_value *pVal, void *pUserData);
static int PH7_builtin_sqrt(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_exp(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_floor(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_cos(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_acos(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_cosh(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_sin(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_asin(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_sinh(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_ceil(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_tan(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_atan(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_tanh(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_atan2(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_abs(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_log(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_log10(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_pow(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_pi(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_fmod(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_hypot(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_max(ph7_context *pCtx, int nArg, ph7_value **apArg);
static int PH7_builtin_min(ph7_context *pCtx, int nArg, ph7_value **apArg);
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc);
static const ph7_builtin_constant mathConstList[] = {
{"M_PI", PH7_M_PI_Const },
{"M_TAU", PH7_M_TAU_Const },
{"M_E", PH7_M_E_Const },
{"M_LOG2E", PH7_M_LOG2E_Const },
{"M_LOG10E", PH7_M_LOG10E_Const },
{"M_LN2", PH7_M_LN2_Const },
{"M_LN10", PH7_M_LN10_Const },
{"M_PI_2", PH7_M_PI_2_Const },
{"M_PI_4", PH7_M_PI_4_Const },
{"M_1_PI", PH7_M_1_PI_Const },
{"M_2_PI", PH7_M_2_PI_Const },
{"M_SQRTPI", PH7_M_SQRTPI_Const },
{"M_2_SQRTPI", PH7_M_2_SQRTPI_Const },
{"M_SQRT2", PH7_M_SQRT2_Const },
{"M_SQRT3", PH7_M_SQRT3_Const },
{"M_SQRT1_2", PH7_M_SQRT1_2_Const },
{"M_LNPI", PH7_M_LNPI_Const },
{"M_EULER", PH7_M_EULER_Const }
};
static const ph7_builtin_func mathFuncList[] = {
{ "abs", PH7_builtin_abs },
{ "sqrt", PH7_builtin_sqrt },
{ "exp", PH7_builtin_exp },
{ "floor", PH7_builtin_floor },
{ "cos", PH7_builtin_cos },
{ "sin", PH7_builtin_sin },
{ "acos", PH7_builtin_acos },
{ "asin", PH7_builtin_asin },
{ "cosh", PH7_builtin_cosh },
{ "sinh", PH7_builtin_sinh },
{ "ceil", PH7_builtin_ceil },
{ "tan", PH7_builtin_tan },
{ "tanh", PH7_builtin_tanh },
{ "atan", PH7_builtin_atan },
{ "atan2", PH7_builtin_atan2 },
{ "log", PH7_builtin_log },
{ "log10", PH7_builtin_log10 },
{ "pow", PH7_builtin_pow },
{ "pi", PH7_builtin_pi },
{ "fmod", PH7_builtin_fmod },
{ "hypot", PH7_builtin_hypot },
{ "max", PH7_builtin_max },
{ "min", PH7_builtin_min }
};
#endif
+4 -2
View File
@@ -1,11 +1,13 @@
import 'math';
class Program {
private string center_text(string $word){
int $tot_width = 30;
char $symbol = '-';
int $middle = (int) round($tot_width/2);
int $middle = (int) Math::Round($tot_width/2);
int $length_word = strlen($word);
int $middle_word = (int) round($length_word / 2);
int $middle_word = (int) Math::Round($length_word / 2);
int $last_position = $middle + $middle_word;
int $number_of_spaces = $middle - $middle_word;
string $result = sprintf("%'{$symbol}{$last_position}s", $word);
+3 -3
View File
@@ -5,8 +5,8 @@ class Program {
private string num2alpha(int $n) {
string $r = '';
for(int $i = 1; $n >= 0 && $i < 10; $i++) {
$r = chr(0x41 + ($n % pow(26, $i) / pow(26, $i - 1))) + $r;
$n -= (int)pow(26, $i);
$r = chr(0x41 + ($n % Math::Pow(26, $i) / Math::Pow(26, $i - 1))) + $r;
$n -= (int)Math::Pow(26, $i);
}
return $r;
}
@@ -15,7 +15,7 @@ class Program {
int $r = 0;
int $l = strlen($a);
for(int $i = 0; $i < $l; $i++) {
$r += (int)pow(26, $i) * (ord($a[$l - $i - 1]) - 0x40);
$r += (int)Math::Pow(26, $i) * (ord($a[$l - $i - 1]) - 0x40);
}
return (int) $r - 1;
}
+1 -1
View File
@@ -20,7 +20,7 @@ class Sudoku {
return false;
}
}
int $sqrt = (int) sqrt($this->size);
int $sqrt = (int) Math::Sqrt($this->size);
int $boxRowStart = $row - $row % $sqrt;
int $boxColStart = $col - $col % $sqrt;
for(int $w = $boxRowStart; $w < $boxRowStart + $sqrt; $w++) {