3 Replies - 290 Views - Last Post: 10 November 2012 - 02:16 PM Rate Topic: -----

#1 GraphicSpike  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 27-October 12

Macro that finds the minimum of its five arguments.

Posted 10 November 2012 - 07:46 AM

I've been trying to figure out the right way to write a macro for 5 arguments.
I know to find the min of 2 arguments you use:

#define ( a , b ) / > a > b ? a : b

But to make 5 do I have to write it the same way I would write a function?

  smallest = num1;
                   if (num2 < num1)
                       smallest = num2;
                   if (num3 < num2)
                       smallest = num3;
                   if (num4 < num3)
                       smallest = num4;
                   if (num5 < num4)
                       smallest = num5;


Or do you write it something like this:

#define MIN5(1,2,3,4,5) (MIN2 (1 , MIN2 (2 , MIN2 ( 3 , MIN2 ( 4 , 5 ) ) ) ) )



Any help is greatly appreciated!

This post has been edited by jimblumberg: 10 November 2012 - 08:50 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Macro that finds the minimum of its five arguments.

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Macro that finds the minimum of its five arguments.

Posted 10 November 2012 - 08:27 AM

I myself would use an array and a loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Macro that finds the minimum of its five arguments.

Posted 10 November 2012 - 10:19 AM

I would say breaking it down in to separate macros would be easiest, if you're allowed.

So you would need MIN5, MIN4 and MIN2.

You could do it with one macro using MIN2, try creating MIN4 or MIN3 first to give you an idea.
Was This Post Helpful? 1
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Macro that finds the minimum of its five arguments.

Posted 10 November 2012 - 02:16 PM

I appreciate the intellectual exercise involved, but you know, you're heading hard and fast, straight for MACRO HELL, don't you?

Just checking < smile >

This post has been edited by Adak: 10 November 2012 - 02:17 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1