I have two numbers, and need to get returned the lower one. Is there any function I could use? Sure it's a easy task, I could do an if-statement. I just want to know.
views:
1639answers:
2
+8
A:
If you're using ints, use the MIN()
macro:
MIN(25, 50); //Returns 25
If you're comparing two NSNumber
s, then use the compare:
method:
NSNumber *number, *secondNumber; //Assume 'number'=25, 'secondNumber'=50
NSComparisonResult result = [number compare:secondNumber];
return (result==NSOrderedDescending)?secondNumber:number; //Returns the 'number' NSNumber
Perspx
2009-05-10 09:19:40
did you forget to include *result?
griotspeak
2010-10-11 14:53:01
Good catch – thanks!
Perspx
2010-10-11 21:09:44
+5
A:
The C standard library includes several min()
functions that, given two numbers, will return the lower of the two:
double fmin(double x, double y);
long double fminl(long double x, long double y);
float fminf(float x, float y);
To use these, just #include <math.h>
.
htw
2009-05-10 09:19:46