From 32ae702c3195ff7d700b8253f79cadeb1ec853c3 Mon Sep 17 00:00:00 2001 From: David Tschumperle Date: Fri, 13 Sep 2024 10:10:17 +0200 Subject: [PATCH] cimg_math_parser(): Extend random int generator to 64bits integers. --- CImg.h | 27 ++++++++++++++------------- html/header.html | 2 +- html/header_doxygen.html | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/CImg.h b/CImg.h index e9684358..7bebd95a 100644 --- a/CImg.h +++ b/CImg.h @@ -28593,11 +28593,12 @@ namespace cimg_library { return cimg::rand(m,M,&mp.rng); } - static double _mp_rand_int(_cimg_math_parser& mp, unsigned int delta) { - if (!delta) return 0; - if (delta!=cimg::type::max()) ++delta; - unsigned int val = 0; - do { val = (unsigned int)std::floor(cimg::rand(delta,&mp.rng)); } while (val>delta); + static double _mp_rand_int(_cimg_math_parser& mp, const double delta) { + if (delta>=cimg::type::max()) return cimg::round(cimg::rand(0,delta,&mp.rng)); + const cimg_uint64 _delta = (cimg_uint64)delta; + if (!_delta) return 0; + cimg_uint64 val = 0; + do { val = (cimg_uint64)std::floor(cimg::rand(_delta + 1,&mp.rng)); } while (val>_delta); return val; } @@ -28607,9 +28608,9 @@ namespace cimg_library { _M = _mp_arg(3); if (_m>_M) cimg::swap(_m,_M); const double - m = cimg::type::cut(std::ceil(_m)), - M = cimg::type::cut(std::floor(_M)); - return (double)m + _mp_rand_int(mp,(unsigned int)(M - m)); + m = cimg::type::cut(std::ceil(_m)), + M = cimg::type::cut(std::floor(_M)); + return (double)m + _mp_rand_int(mp,M - m); } static double mp_rand_int_0_1(_cimg_math_parser& mp) { @@ -28618,8 +28619,8 @@ namespace cimg_library { static double mp_rand_int_0_N(_cimg_math_parser& mp) { const double _M = _mp_arg(2); - const bool sgn = _M>=0; - return (sgn?1.0:-1.0)*_mp_rand_int(mp,cimg::type::cut(std::floor(sgn?_M:-_M))); + const int sgn = _M>=0?1:-1; + return sgn*_mp_rand_int(mp,_M*sgn); } static double mp_rand_int_m1_1(_cimg_math_parser& mp) { @@ -28638,9 +28639,9 @@ namespace cimg_library { _M = _mp_arg(3); if (_m>_M) cimg::swap(_m,_M); const int - m = cimg::type::cut(std::ceil(_m)) + (include_min?0:1), - M = cimg::type::cut(std::floor(_M)) - (include_max?0:1); - return (double)m + _mp_rand_int(mp,(unsigned int)(M - m)); + m = cimg::type::cut(std::ceil(_m)) + (include_min?0:1), + M = cimg::type::cut(std::floor(_M)) - (include_max?0:1); + return (double)m + _mp_rand_int(mp,M - m); } static double mp_ui2f(_cimg_math_parser& mp) { diff --git a/html/header.html b/html/header.html index c8743710..266a764e 100644 --- a/html/header.html +++ b/html/header.html @@ -23,7 +23,7 @@
Logo

- Latest stable version: 3.4.2        Current pre-release: 3.4.3 (2024/09/11) + Latest stable version: 3.4.2        Current pre-release: 3.4.3 (2024/09/12)


diff --git a/html/header_doxygen.html b/html/header_doxygen.html index f781e53a..ef8dd74e 100644 --- a/html/header_doxygen.html +++ b/html/header_doxygen.html @@ -26,7 +26,7 @@
Logo

- Latest stable version: 3.4.2        Current pre-release: 3.4.3 (2024/09/11) + Latest stable version: 3.4.2        Current pre-release: 3.4.3 (2024/09/12)