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

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#3
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:
#4
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:
#5
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;
}