Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Give @EVAL max(x,y) and min(x,y)

May
12,846
164
It's cumbersome putting @MAX inside @EVAL (because you almost certainly have to put @EVAL inside @MAX).
 
Another reason is the limitation on @MIN's parameters.
Code:
v:\> echo %@min[7701322015,9999999000]
-888612577
 
FWIW, here's basic code to do max(x1 x2 ... xN) with the MAPM API. With a single change, it'll do min(x1 x2 ... xN). I haven't a clue how it might fit in @EVAL.
I'm working on log2 now. I have a working version, but I think it's quite wasteful ... must consult with a numerical analyst about achieving then desired precision.

Code:
DELETED

There's a better one in the next post.
 
Last edited:
Here's a better one. I was using m_apm_copy() which is non-trivial.

Code:
#include <windows.h>
#include <stdio.h>
#include "..\m_apm.h"

// Syntax:  MAX x1 x2 ... xk
// Change -1 to 1 for MIN (see below)
INT main ( INT argc, LPSTR *argv )
{
   INT index_of_max = 1;

   M_APM n[2] = { m_apm_init(), m_apm_init() };

   INT imax = 0, itest = 1;

   m_apm_set_string(n[imax], argv[1]);

   for ( INT i=2; i<argc; i++ )
   {
       m_apm_set_string(n[itest], argv[i]);
       if ( m_apm_compare(n[imax], n[itest]) == -1 ) // change to 1 for MIN
       {
           index_of_max = i;
           imax = itest;
           itest = 1 - itest;
       }
   }

   m_apm_free(n[0]);
   m_apm_free(n[1]);

   printf("%s\r\n", argv[index_of_max]);
}
 
Last edited:
Here's the same thing using MAPM's C++ wrapper (which is pretty nice).
Code:
// Syntax:  MAX x1 x2 ... xk
INT main ( INT argc, LPSTR *argv )
{
   if ( argc < 2 ) return 0;

   INT index_of_max = 1, imax = 0, itest = 1, i;

   MAPM m[2] = {argv[1], 0};

   for ( i=2; i<argc; i++ )
   {
       m[itest] = argv[i];
       if ( m[itest] > m[imax] ) // use < for MIN
       {
           index_of_max = i;
           imax = itest;       // this flip-flopping avoids the somewhat costly
           itest = 1 - itest;   // assignment of the test MAPM to the max MAPM
       }
   }

   printf("%s\r\n", argv[index_of_max]);
   return 0;
}
 

Similar threads

Back
Top