## Check which range a number is within

check if number is in range python
c++ check if number is in range
c check if number is in range
c# check if decimal is in range
check number between range c#
check if number is in range java
python check if float is between two values
check if number is in range javascript

I need to find which range a number, in this case `value`, is within. I cannot find any other way to set `i` to what I need other than this:

```if value < -64:
i = 0
elif value < -32:
i = 1
elif value < -16:
i = 2
elif value < -8:
i = 3
elif value < -4:
i = 4
elif value < -2:
i = 5
elif value < -1:
i = 6
elif value < -0.5:
i = 7
elif value < 0:
i = 8
elif value < 0.5:
i = 9
elif value < 1:
i = 10
elif value < 2:
i = 11
elif value < 4:
i = 12
elif value < 8:
i = 13
elif value < 16:
i = 14
elif value < 32:
i = 15
elif value < 64:
i = 16
else:
i = 17
```

This is terrible code and I hate it. Is there any way I can do something like this?

```ranges = [-64, -32, -16 ... 32, 64]
i = find_which_range(value, ranges)
```

Thanks!

Use bisect:

```import bisect

ranges = [-64, -32, -16, -8, -4, -2, -1, -0.5, 0, 0.5, 1, 2, 4, 8, 16, 32, 64]

print(bisect.bisect(ranges, -65))
# 0

print(bisect.bisect(ranges, -64))
# 1

print(bisect.bisect(ranges, 63))
#16

print(bisect.bisect(ranges, 64))
# 17
```

`bisect.bisect(l, value)` returns the index at which `value` would have to be inserted in `l`, such that all values to the left are less than `value`. It will also be fast to search a large list, as it uses a bisection algorithm.

How to check whether a number is in the range[low, high] using one , How to check if x lies in range [low, high] or not using single comparison. For example, if range is [10, 100] and number is 30, then output is true and if the  1..100 (inclusive) and uses x as the number to check followed by an optional range defined by min and max. Adding Examples For Good Measure. Example 1:

Based off the way you described you could do something like this:

```ranges = [-64, -32, -16, -8, -4, -2, -1, -0.5, 0, 0.5, 1, 2, 4, 8, 16, 32, 64]

def build_ranges(power):
ranges = [-0.5, 0, 0.5]
for i in range(power):
ranges.append(2**i)
ranges.append(-2**i)
return sorted(ranges)

def find_which_range(value, ranges):
for i, range in enumerate(sorted(ranges)):
if value < range:
return i
return None

output = find_which_range(value, ranges)
```

Python Check Integer Number in Range Using Multiple Ways, This tutorial provides you multiple methods to check if an integer number lies is in the given range. It includes examples for clarity. To check whether some number n is in the inclusive range denoted by the two number a and b you do either. if a <= n <= b: print "yes" else: print "no". use the replace >= and <= with > and < to check whether n is in the exclusive range denoted by a and b (i.e. a and b are not themselves members of the range).

Mathew provided a looping solution that works fine. But also observe that you are checking basically, which powers of 2 your number is between. Thus, you can take the log.

```import math
a_value = abs(value)
sign = 1 if value > 0 else -1
lg_value = math.log2(a_value)
range_ = (2**(sign*math.floor(lg_value)), 2**(sign*math.ceil(lg_value)))
```

note that slight modifications are required for ranges < 1.

Python Exercise: Check whether a number is in a given range , Write a Python function to check whether a number is in a given range. Sample Solution:- Python Code: def test_range(n): if n in range(3,9): print(  In the Select Specific Cells dialog box, select the Cell option in the Selection type section, select Equals in the Specific type drop-down list, then enter the number or value you want to find in range, and finally click the OK button.

People already provided much shorter (and efficient) solutions. I am just posting here my attempt just to show one more way and logic to approach the problem using list comprehension.

```def find_which_range(value, ranges):
if value <= min(ranges):
c = 0
elif value >= max(ranges):
c = len(ranges)
else:
c = [i+1 for i in range(len(ranges)-1) if ranges[i] <= value <= ranges[i+1]]
return c

value = -9
i = find_which_range(value, ranges)
```

Checking for the Presence of Specified Values Within a Range in , COUNTIF: This function is used to count the number of cells within a range which meet a given condition. countif. For Example:-We have a data table in range A2:  I have a dataset in a data.table format that looks as such:. ID time.s time.e 1 1 2 2 1 4 3 2 3 4 2 4 I want to check to see if the value 1 is within time.s and time

To find the right range, you can iterate over each range using `zip` function, like this:

```ranges = [-64, -32, -16, -8, -4, -2, -1, -0.5, 0, 0.5, 1, 2, 4, 8, 16, 32, 64]

i = 9

for p, n in zip(ranges[:-1], ranges[1:]):
if p <= i < n:
r = p, n
break
else:
raise ValueError(i)

print(r)
```

You get: (8, 16)

Then you can use a mapping between each range and target values, something like this:

```mapping = {
(-64, -32): 1,
(-32, -16): 2,
(-16, -8): 3
}

print(mapping[(-32, -16)])
2
```

How to determine if a number or value is in a range in Excel?, Determine if a number or value is in a range with Kutools for Excel. Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier. To determine if a value exists in a range of cells, you can use a simple formula based on the COUNTIF function. In the example shown, the formula in D5 is: where "rng" is the named range F4:F10. The COUNTIF function counts cells that meet supplied criteria, returning the number of occurrences found. If no cells meet criteria, COUNTIF returns zero.

How to return a value if lookup value is in a range, Check out the LOOKUP category to find more interesting articles. can also do lookups in date ranges, dates in Excel are actually numbers. You will see updates in your activity feed. You may receive emails, depending on your notification preferences. Hello, just trying to figure out a way to find a number in a vector within range. I'm trying practice for loops but still bad at it. but what if 0 was part of it? but not sure how to get that last part. Sign in to answer this question.

Shortest code to check if a number is in a range in JavaScript, 13 chars, checks both variants a<b and b<a (x-a)*(x-b)<0. In C may be used expression (may be also in JavaScript). 11 chars, No multiplications (fast) If one number is 12 and one is 10 and they are in A1 and A2, then =ABS(A1-A2)/A1 will give you the percent difference relative to A1 =ABS(A1-A2)/A2 will give you the percent difference relative to A2. They are not the same!

Check if \$REPLY is in a range of numbers, The [ command/shell builtin has comparison tests, so you can just do if [ "\$REPLY​" -ge 1 ] && [ "\$REPLY" -le 32 ]; then REPLY=-2; elif [ "\$REPLY" -ge 33 ]  Write a Python function to check whether a number is in a given range. The following tool visualize what the computer is doing step-by-step as it executes the said program: Customize visualization ( NEW!) There was a problem connecting to the server. Please check your connection and try running the trinket again.

##### Comments
• Sure, check out this tutorial on `for` loops.
• Why do you need to do this in the wider context of your program. This feels like an anti-pattern / code smell to begin with.
• I need to create a one-hot label for tensorflow. The value is a percentage, and I need to find which index of an array of zeros to set to one.
• Exactly. This is not only nicer than the original solution - it is also faster ;)
• You should include the default case for the scenario in which `value > 64`
• Could expand this to have a helper function that generates `ranges` between any 2 powers of 2
• The array look like `[... -2, -1, -0.5, 0, 0.5, 1, 2 ...]`, but other than that, thanks! This works perfectly!
• That is much cleaner, but because I have no clue as to whats actually happening, I'm going to use the looping method. Thanks tho.
• being easy to understand is important @NeilMacneale! No problems with that :). I did edit the answer though to be more clear