#### Archived

This topic is now archived and is closed to further replies.

# Math/Logic Help?

## Recommended Posts

**SOLVED**

Sorry if this isn't appropriate for here, but I can't figure out how to do this.
I have two variables, a&b. I want to establish a percentage between 15-35 based on a&b. So if:

\$a = 0;
\$b = 10;
\$percent = 15%

\$a = 5;
\$b = 10;
\$percent = 25%

\$a = 10;
\$b = 10;
\$percent = 35%

To summarize: the closer A gets to B, the higher the percentage is.

So stuck on how to do this? Any help would be GREATLY appreciated, thank you SO much!

##### Share on other sites
Hi
i'd hazard a guess that this is purely a math question rather than php at all, but i can't see any way that \$a=1 and \$b=10 could ever create 15%, even by using 35 instead of 100 to work out the percentage?...

maybe you could elaborate a bit more?
Cheers
Mark

##### Share on other sites
[!--quoteo(post=361626:date=Apr 4 2006, 12:48 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 12:48 PM) [snapback]361626[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Hi
i'd hazard a guess that this is purely a math question rather than php at all, but i can't see any way that \$a=1 and \$b=10 could ever create 15%, even by using 35 instead of 100 to work out the percentage?...

maybe you could elaborate a bit more?
Cheers
Mark
[/quote]

I know, but I couldn't find anywhere else to ask, and I am trying to do it in PHP ;)

Let me try to explain better. Depending on how close A is to B, I want to pick a number between 15-35. We could make it just 0-20 and add on 15 later.

So if B is 20, it's easy:
A=1, number = 0, so +15 = 15%
when A = 10, number = 10, 25%.
When A is 20, number=20 +15 = 35%

Make sense?

It's easy when B is 20, but if B is 7 and A is 4...
How do I find the number on the scale?

A..............B
0..............20

##### Share on other sites
assuming that your examples are not shown with the results you expect, then:

[code]
\$percent = (\$a/\$b)*20;
[/code]
would give you a "percentage" with 20 as the top value, as the normal way to work out percent is:

[code]
\$percent = (\$pizza_slice_size/\$pizza_full_size)*100;
[/code]

##### Share on other sites
[!--quoteo(post=361630:date=Apr 4 2006, 12:57 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 12:57 PM) [snapback]361630[/snapback][/div][div class=\'quotemain\'][!--quotec--]
assuming that your examples are not shown with the results you expect, then:

[code]
\$percent = (\$a/\$b)*35;
[/code]
would give you a "percentage" with 35 as the top value, as the normal way to work out percent is:

[code]
\$percent = (\$pizza_slice_size/\$pizza_full_size)*100;
[/code]
[/quote]

So if I want only between 15-35, I could do:
\$percent = (\$a/\$b)*20; and add 15% right?

##### Share on other sites
[!--quoteo(post=361632:date=Apr 4 2006, 06:59 PM:name=jesirose)--][div class=\'quotetop\']QUOTE(jesirose @ Apr 4 2006, 06:59 PM) [snapback]361632[/snapback][/div][div class=\'quotemain\'][!--quotec--]
So if I want only between 15-35, I could do:
\$percent = (\$a/\$b)*20; and add 15% right?
[/quote]

yes. just watch the order things are calculated in though:

[code]
\$percent = (( \$a / \$b ) * 20) + 15;
[/code]

##### Share on other sites
[!--quoteo(post=361633:date=Apr 4 2006, 01:01 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 01:01 PM) [snapback]361633[/snapback][/div][div class=\'quotemain\'][!--quotec--]
yes.
[/quote]

Thaaank you :)

##### Share on other sites
I think you have some unstated constants in your problem, namely a \$floor of 1 and a \$ceiling of 10 (although \$b seems to contain that constant)

Your problem is to convert a value between \$floor and \$ceiling to a value between 15 and 35. Basically, this is the same as converting celsius to fahrenheit - the units are of different size, and the scale starts at different points.

[code]// First, find the scales, and the ratio between them:

\$scale1= \$ceiling-\$floor; // will evaluate to 9 if \$floor is 1, 10 if \$floor is 0
\$scale2= 35-15; // equals 20

\$factor= \$scale2 / \$scale1; // if \$scale is 9 this will be some ugly decimal, if it is 10 it will be a nice tidy 2

// now convert \$a to value on \$scale2:

\$percentage = (\$a - \$floor) * \$factor; // if \$floor is 1 then if \$a==1 \$percentage will be 0
\$percentage  += 15; // add the base of 15 to the percentage value[/code]

if you really have to have a floor of 1, I suggest using round() on your result.

##### Share on other sites
Actually the way redbullmarky posted worked perfectly.

Here's what I get now when B = 7

0/7: 15
1/7: 17
2/7: 20
3/7: 23
4/7: 26
5/7: 29
6/7: 32
7/7: 35

##### Share on other sites
SELECTED NUMBER A 5.0000
STARTING NUMBER B 1.0000
ENDING NUMBER C 10.0000
STARTING PERCENT D 15.0000
ENDING PERCENT E 35.0000
PERCENT DIFFERENCE F 20.0000 E - D = F
PERCENT PER STEP G 2.2222 F / (C -B) = G
PERCENT AT NUMBER H 23.8889 ( G * ( A - B ) ) = H

This was a little odd had to think on this one lol....... bad I know

but I think we got it

1) You need to define a min / max number [C - B = steps in range]
2) You need to define a min / max percent [E - D = F {Percent per range}]
3) [F / (C -B) = G {percent per step}]
4) [( G * ( A - B ) ) = H {to get you percent per given number}]

if 1 was 15 and 10 was 35 then 5 would have been 23.8889
so if you define start and finish points then you could get the percent with in that range and the formula will get you your percentage via number given

##### Share on other sites
[!--quoteo(post=361645:date=Apr 4 2006, 01:25 PM:name=jesirose)--][div class=\'quotetop\']QUOTE(jesirose @ Apr 4 2006, 01:25 PM) [snapback]361645[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Actually the way redbullmarky posted worked perfectly.

Here's what I get now when B = 7

0/7: 15
1/7: 17
2/7: 20
3/7: 23
4/7: 26
5/7: 29
6/7: 32
7/7: 35
[/quote]

But it would not work if you wanted 1/10 to be 15%, it would have made 1/10 equal to 17% (15 + (1/10 *20)) But I see now that you wanted 0 to be 15% - did you edit that or was it just Mark who started all this 1/10 = 15% nonsense ;)

##### Share on other sites
[!--quoteo(post=361708:date=Apr 4 2006, 03:17 PM:name=Dan R.)--][div class=\'quotetop\']QUOTE(Dan R. @ Apr 4 2006, 03:17 PM) [snapback]361708[/snapback][/div][div class=\'quotemain\'][!--quotec--]
But it would not work if you wanted 1/10 to be 15%, it would have made 1/10 equal to 17% (15 + (1/10 *20)) But I see now that you wanted 0 to be 15% - did you edit that or was it just Mark who started all this 1/10 = 15% nonsense ;)
[/quote]

That was my fault, I fixed it to reflect it. I've got it working perfectly now. I meant when A=0, not a=1 :) Sorry!

##### Share on other sites
[!--quoteo(post=361708:date=Apr 4 2006, 09:17 PM:name=Dan R.)--][div class=\'quotetop\']QUOTE(Dan R. @ Apr 4 2006, 09:17 PM) [snapback]361708[/snapback][/div][div class=\'quotemain\'][!--quotec--]
...did you edit that or was it just Mark who started all this 1/10 = 15% nonsense ;)
[/quote]

lol well i would have loved to have used pythagoras theorem or some celsius/farenheight mathematical black hole to solve the problem, but i thought i'd just stick to the question that was asked in the first place ;) hey, if someone wants a percentage to start at 15 and end at 35, who am i to argue?

##### Share on other sites
[!--quoteo(post=361729:date=Apr 4 2006, 04:03 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 04:03 PM) [snapback]361729[/snapback][/div][div class=\'quotemain\'][!--quotec--]
hey, if someone wants a percentage to start at 15 and end at 35, who am i to argue?
[/quote]

I do have a reason ;) I promise, I'm not totally insane.