Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test to see if number is in range using Objective-C?

Tags:

c

objective-c

I have a very basic question. I would like to know if here is a built-in function in Objective-C or C to help me find if a specific number it's in a certain range. I know that this is probably easy question but still I didn't found an answer. On short terms, would like to avoid using multiple "if"s and "else"s for this test.

like image 705
Rad'Val Avatar asked Mar 07 '11 17:03

Rad'Val


People also ask

How to check if a number is in a range?

If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0. And must be smaller than or equal to high i.e., (high – x) <= 0. So if result of the multiplication is less than or equal to 0, then x is in range.

How to check if an integer is in a given range?

ValueRange. of(minValue, maxValue); range. isValidIntValue(x); it returns true if minValue <= x <= MaxValue - i.e. within the range.


1 Answers

NSLocationInRange(c, NSMakeRange(a, (b - a)))

This returns a BOOL if c lies within a and b. However a,b and c must be unsigned int. And this is really not very good looking. So I guess it is far better to compare myself.

c >= a && c <= b
like image 125
taskinoor Avatar answered Nov 16 '22 01:11

taskinoor