Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,
I have a list of numbers 0.25,0.50,0.75,1,1.25,2,2.5,3.5, ... and then I need to find out the range minimum and maximum of a number. For example my number is 0.30 then the min range is 0.25 and maximum is 0.50.
I know there are so many ways to find out this but I want to know that if there is any pre-defined functions like Icompare.(The reason is I want to shorten my code length.)
Thanks in Advance.
Posted
Comments
DamithSL 7-Nov-14 5:27am    
What is the datatype of the list?
Manu Prasad 7-Nov-14 5:49am    
Arraylist of double values
DamithSL 7-Nov-14 5:54am    
double givenNumber =0.30 ;
List<double> numbers = new List<double>{0.25,0.50,0.75,1,1.25,2,2.5,3.5};
var min = numbers.Where(n=>n<=givenNumber).Min();
var max = numbers.Where(n=>n>=givenNumber).Min();
Manu Prasad 7-Nov-14 6:29am    
Thank you Damith.. Now my code is this much only
private double GetLength(double myvalue)
{
return lengths.Where(n => n >= myvalue).Min();

}
Tomas Takac 7-Nov-14 5:40am    
If you would use array instead of list you can do binary search on sorted array that finds you the range. I can post the code if you are interested.

AFAIK, there is no "find in range" method: You will have to "do it yourself".
But it's not massive code: depending on the number of numbers in your set (and I notice it's sorted already) it shouldn't take long to find the "first number above" your value and then that and the previous index are the range.

For a small number of ranges I'd just scan up: for a larger number then maybe a binary chop would be more efficient? Depends on the count, and what your code is doing at the moment.
 
Share this answer
 
As I mentioned in my comment, you can do this easily with an array. Please note that the array must be sorted in ascending order.

C#
double[] numbers = new[] {0.25,0.50,0.75,1,1.25,2,2.5,3.5};
int index = Array.BinarySearch(numbers, 0.3);

if(index >= 0)
{
    Console.WriteLine("is exact match");
}
else
{
    int lowerBound = -index-2;
    int upperBound = -index-1;
	
    if(lowerBound < 0)
        Console.WriteLine("is minimum");
    else if(upperBound >= numbers.Length)
        Console.WriteLine("is maximum");
    else
        Console.WriteLine("is between {0} and {1}", numbers[lowerBound], numbers[upperBound]);
}


Please see MSDN for more details on Array.BinarySearch[^] method.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 8-Nov-14 5:27am    
Yes +5!
You can achieve it using Linq[^]:
C#
double find = .80;
double[] darr = new double[]{0.25,0.50,0.75,1,1.25,2,2.5,3.5};
double up = (from maxv in darr
		where maxv > find
		select maxv).First();
double down = (from minv in darr
		where minv < find
		select minv).Last();
Console.WriteLine("Range for {0} is: {1}-{2}", find, down, up);

Result:
Range for 0,8 is: 0,75-1
 
Share this answer
 
Comments
Manas Bhardwaj 8-Nov-14 5:27am    
Yes +5!
Maciej Los 8-Nov-14 8:27am    
Thank you, Manas ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900