I modified the test program as below and made checks using as standard C runtime library the Pelles POCRT.lib and MSVCRT.lib from M$.
If you have coincident values using MingW maybe you are linking with M$ runtime library, so de facto you are using the same routine.
The compiler used is PellesC V6.00 RC#3.
#include <stdio.h>
#include <stdlib.h>
#define MIN(x,y) ( (x<y) ? (x) : (y) )
#define MAX(x,y) ( (x>y) ? (x) : (y) )
#define NELEMS 50
double vector[NELEMS];
int main(void)
{
double min1,min2,min3, max1,max2,max3, minx=0.0, maxx=0.0;
for(int i=0;i<NELEMS;i++)
vector[i]=(double) (rand())/RAND_MAX*100;
//vector[i] = (double) i * 3.55;
min1=min2=min3=vector[0];
max1=max2=max3=vector[0];
double x = 0.0;
for(int i=1;i<NELEMS;i++) {
x = vector[i];
// CASE 1: this code doesn´t work *********************
min1=MIN(min1, x);
max1=MAX(max1, x);
// Case 2: neither this *******************************
min2 = (min2 > x ? x : min2);
max2 = (max2 < x ? x : max2);
// Case 3: but this works *****************************
if( min3 > x ) min3 = x;
if( max3 < x ) max3 = x;
}
printf("\n\n");
printf("Case 1 -> MIN =%8.4f MAX =%8.4f\n",min1,max1);
printf("Case 2 -> MIN =%8.4f MAX =%8.4f\n",min2,max2);
printf("Case 3 -> MIN =%8.4f MAX =%8.4f\n",min3,max3);
printf("\n\n");
return EXIT_SUCCESS;
}
But I haven't seen any bug.
The values changes depending on the pseudodistribution of the random function, which of course have not the same implementation for all libraries.
You can see it enlarging the array, i.e NELEMS=5000. The larger you make it, higher is the probability that you will cover the whole range of pseudorandom values.
If you use the linear filling proposed by CommonTater you will get the correct answer:
MIN=0.00 (i=0)*3.55
MAX=173.95 (i=49)*3.55
The bracketing have no influence because the expression is solved based on operators priorities.
John I don't understand why you get that strange result. Please try my code.
Eheredia2511 what you exactly mean for cases 1 & 2 not working? compilation error, zero values or simply not coincident with M$? In last case I already explained the reason.