GreycLab / CImg

The CImg Library is a small and open-source C++ toolkit for image processing
http://cimg.eu
Other
1.49k stars 285 forks source link

Making function next(), and back(). Basically, they use nexttoward. #407

Closed Reptorian1125 closed 8 months ago

Reptorian1125 commented 8 months ago

Hello, I have decided to try to make two new function, but before that, I want to get next() to work.

This is what I have at the moment:

diff --git a/CImg.h b/CImg.h
index bb95ede..d069009 100644
--- a/CImg.h
+++ b/CImg.h
@@ -6862,6 +6862,12 @@ namespace cimg_library {
       return res;
     }

+    //! Return the next value after n
+    template<typename T>
+    inline double next(const T n) {
+        return std::nexttoward(n, cimg::type<double>::inf());
+    }
+
     //! Return the number of permutations of k objects in a set of n objects.
     inline double permutations(const int k, const int n, const bool with_order) {
       if (n<0 || k<0) return cimg::type<double>::nan();
@@ -20731,6 +20737,14 @@ namespace cimg_library {
               _cimg_mp_scalar1(mp_factorial,arg1);
             }

+            if (!std::strncmp(ss,"next(",5)) { // Next toward Infinity
+                _cimg_mp_op("Function 'next()'");
+                arg1 = compile(ss5, se1, depth1, 0, block_flags);
+                if (is_vector(arg1)) _cimg_mp_vector1_v(mp_next, arg1);
+                if (is_const_scalar(arg1)) _cimg_mp_const_scalar(cimg::next((int)mem[arg1]));
+                _cimg_mp_scalar1(mp_next, arg1);
+            }
+
             if (!std::strncmp(ss,"fibo(",5)) { // Fibonacci
               _cimg_mp_op("Function 'fibo()'");
               arg1 = compile(ss5,se1,depth1,0,block_flags);
@@ -25547,6 +25561,10 @@ namespace cimg_library {
         return cimg::factorial((int)_mp_arg(2));
       }

+      static double mp_next(_cimg_math_parser& mp) {
+          return cimg::next(_mp_arg(2));
+      }
+
       static double mp_fibonacci(_cimg_math_parser& mp) {
         return cimg::fibonacci((int)_mp_arg(2));
       }

When I try to test it with this:

$ echo {5%next(5)}

There seems to be a error.

Reptorian1125 commented 8 months ago

Ah, sorry, I got it now. It seems that I had to move it to case 'n'. Now, I"ll find negative inf within Cimg::type and do the back() version.

Also, made a thread on this to see if any one else wants it in G'MIC.