Re: How to implement float min(float x, ...) ?
Re: How to implement float min(float x, ...) ?
- Subject: Re: How to implement float min(float x, ...) ?
- From: "Chris Holloway" <email@hidden>
- Date: Mon, 17 Nov 2008 13:45:59 +0000
Hi,
It's not possible in C89, but in C99 you can do this (apologies if the
formatting is messed up):
#include <math.h>
#include <stddef.h>
#define ARRAY_NELEMS(A) (sizeof(A) / sizeof((A)[0]))
#define FA_MIN(...) fa_min_func((float []){
__VA_ARGS__ }, ARRAY_NELEMS(((float []){ __VA_ARGS__ })))
float fa_min_func(float f[], size_t n)
{
float ret;
if(n < 1)
return NAN;
ret = f[0];
while(n-- > 1) {
if(f[n] < ret)
ret = f[n];
}
return ret;
}
Credit where it's due, I originally saw this 'trick' at
http://nikitadanilov.blogspot.com/2005/07/vaargs-c99-compound-literals-safe.html.
-Chris
2008/11/17 Oleg Krupnov <email@hidden>:
> I apologize, this is plain old C, not Cocoa-specific question, but the
> fastest way to get the answer.
>
> I want to create a function that finds the minimum out of a
> variable-length list of simple float arguments. In other words, I want
> this function:
>
> float min(float x, ...);
>
> Can anyone suggest the implementation?
>
> I have read this: http://developer.apple.com/qa/qa2005/qa1405.html
> but it seems to address arguments of type "id", not sure it will work
> for simple integral types like float.
>
> Thanks!
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden