Reputation: 4731
I created the following method to use rate safely.
(Sometimes rate can become invalid value like INFINITY, NAN, or out of 0-1)
-(double)XXXX:(double)rate
if (rate >= 1) {
return 1;
} else if (rate <= 0) {
return 0;
} else if (0 <= rate && rate <= 1) {
return rate;
} else {
return 0;
}
}
What should I name this method?
EDIT:
I use rate to display progress of time with UISlider, UIProgress, or just NString(XX %).
Usage of the method are:
rate = [objectOrClass XXXX: currentTime / totalTime];
rate = [objectOrClass XXXX:(currentTime + additionalTime) / totalTime];
I also use it to calculate currentTime from rate:
currentTime = [objectOrClass XXXX:rate] * totalTime;
Upvotes: 0
Views: 134
Reputation: 6718
When totalTime is zero you will get INFINITY.
When totalTime and currentTime both are zeros you will get NAN.
When (currentTime + additionalTime) and currentTime both are zeros you will get NAN.
I think it will be helpful to you.
Upvotes: 0
Reputation: 1027
static inline float RateAligned(float rate) {
return MAX(.0f, MIN(1.0f, rate));
}
Upvotes: 1