From 9be5644713219365e7683fd3052505db661275f0 Mon Sep 17 00:00:00 2001 From: David Tschumperle Date: Sun, 11 Feb 2024 20:50:31 +0100 Subject: [PATCH] math_parser(): Make 'gcd()' work for multiple numbers. --- CImg.h | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/CImg.h b/CImg.h index 6438cc02..fafa6aab 100644 --- a/CImg.h +++ b/CImg.h @@ -6908,8 +6908,11 @@ namespace cimg_library { } //! Calculate greatest common divisor of two integers. - inline cimg_long gcd(cimg_long a, cimg_long b) { - while (a) { const cimg_long c = a; a = b%a; b = c; } + template + inline T gcd(T a, T b) { + if (a<0) a = -a; + if (b<0) b = -b; + while (a) { const T c = a; a = b%a; b = c; } return b; } @@ -20927,18 +20930,6 @@ namespace cimg_library { _cimg_mp_scalar3(mp_gauss,arg1,arg2,arg3); } - if (!std::strncmp(ss,"gcd(",4)) { // Gcd - _cimg_mp_op("Function 'gcd()'"); - s1 = ss4; while (s1 values; + if (i_end==5) { // Only a single argument + if ((unsigned)mp.opcode[4]==1) return _mp_arg(3); // Real value + else values.assign(&_mp_arg(3),(unsigned int)mp.opcode[4],1,1,1,true); // Vector value + } else { + unsigned int siz = 0; + for (unsigned int i = 4; i1) std::memcpy(ptr,&_mp_arg(i),len*sizeof(double)); + else *ptr = _mp_arg(i); + ptr+=len; + } + } + return values.gcd(); } #ifdef cimg_mp_func_name @@ -30525,11 +30535,11 @@ namespace cimg_library { } //! Return greatest common diviser of all image values. - cimg_long gcd() const { + T gcd() const { if (is_empty()) return 0; const ulongT siz = size(); - cimg_long res = (cimg_long)*data; - for (ulongT k = 1; k