Does this vary depeneding if the min value is a negative?

Maybe you just need to add some logic to check if the minimum value is less than 0 and change the operator from a subtraction to an addition

Solved

Posted on 2011-04-30

Hello,

Very sorry - rubbish title I know, can't say I get much better at them either.

I've got two, a minimum and a maximum now all I know is that the minimum will always be less than or equal to the maximum, there's no restrictions of having negative numbers etc (which is where I run into problems).

Both of those numbers are inclusive, so if I had:

min: 0

max: 10

The available number of numbers is:

result = (max-min)+1 =

result = (10-0)+1 =

result = 11

What I then want to do is find a particular percentage of that range, so taking the result above (11), and I wanted 30% of this I could easily do:

result = result * 0.3

result = 3

This is all okay, but as soon as negative numbers for minimum and/or maximum are thrown in, then things start to collapse quite quickly. Consider:

max = 5

min = -5

result = (max-min)+1 =

result = (5-(-5))+1 =

result = 11

result = result * 0.3

result = 11 * 0.3

result = 3

What I'm struggling with is that while 3 is correct for a range of 0-11, the actual range is -5 to 5. Yet I can't just subtract the minimum (-5) from the result 3 as that just gives me 8.

So, is there a way given an inclusive range (min to max) to get the percentage along that number line, perhaps without all the messy calculations above?

Thanks,

Uni

Very sorry - rubbish title I know, can't say I get much better at them either.

I've got two, a minimum and a maximum now all I know is that the minimum will always be less than or equal to the maximum, there's no restrictions of having negative numbers etc (which is where I run into problems).

Both of those numbers are inclusive, so if I had:

min: 0

max: 10

The available number of numbers is:

result = (max-min)+1 =

result = (10-0)+1 =

result = 11

What I then want to do is find a particular percentage of that range, so taking the result above (11), and I wanted 30% of this I could easily do:

result = result * 0.3

result = 3

This is all okay, but as soon as negative numbers for minimum and/or maximum are thrown in, then things start to collapse quite quickly. Consider:

max = 5

min = -5

result = (max-min)+1 =

result = (5-(-5))+1 =

result = 11

result = result * 0.3

result = 11 * 0.3

result = 3

So, is there a way given an inclusive range (min to max) to get the percentage along that number line, perhaps without all the messy calculations above?

Thanks,

Uni

12 Comments

Does this vary depeneding if the min value is a negative?

Maybe you just need to add some logic to check if the minimum value is less than 0 and change the operator from a subtraction to an addition

Should you subtract from the minimum value or add to the minimum value?Don't have a clue...

Does this vary depeneding if the min value is a negative?

Maybe you just need to add some logic to check if the minimum value is less than 0 and change the operator from a subtraction to an additionThat is one solution, but it seems I'm over complicating something which seems very simple...

So in the first instance this would be 0+3=3

And in the second I instance this would be -5+3=-2

Is this what you are after? If so you can always use an addition and this will return what you require. You could also subtract the inverse of the percentage from the maximum which would give the same result.

Yes. Here's another example (Sorry I didn't quite grasp yours):

min: 10

max: 25

range: (25-10)+1 : 16

35% of 16 is: 5.6

min is 10, so actual result is: 5.6+10 = 15.6 ~~ 15

Given:

min

max

AvailableNumbersInRange = (max - min) + 1

30 percent is therefore:

Place = 0.3 * AvailableNumbersInRange

Value = min + Place

I'm getting some off by one error here, and I'm not sure why:

min: 0

max: 99

AvailableNumbersInRange = (99-0)+1

AvailableNumbersInRange = 100

If I want 100%:

Place = 1 * AvailableNumbersInRange

Place = 100

Value = 0 + 100

Value = 100

Value should not be greater than max...

Thanks,

Uni

place = (1-x%) * AvailableNumbersInRange

Value = max - place

So for your example above:

Place = (1 - 1) * 100

Place = 0

Value = 99 - 0

Value = 99

min: 0

max: 99

AvailableNumbersInRange = (99-0)+1

AvailableNumbersInRange = 100

If I want 0%:

Place = (1-%) * AvailableNumbersInRange

Place = (1-0) * 100

Place = 1 * 100

Value = max - place

Value = 99 - 100

Value = -1

>>I don't think you should be adding 1 in the first place.

If I don't add 1 then the top value cannot be selected.

>>in which case 100 is correct.

No, the final value must be >= min and <= max, otherwise min and max are useless.

Thanks,

Uni

By clicking you are agreeing to Experts Exchange's Terms of Use.

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**11** Experts available now in Live!