Jessica Posted April 4, 2006 Share Posted April 4, 2006 **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! Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/ Share on other sites More sharing options...
redbullmarky Posted April 4, 2006 Share Posted April 4, 2006 Hii'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?CheersMark Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23875 Share on other sites More sharing options...
Jessica Posted April 4, 2006 Author Share Posted April 4, 2006 [!--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--]Hii'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?CheersMark[/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..............B0..............20 Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23878 Share on other sites More sharing options...
redbullmarky Posted April 4, 2006 Share Posted April 4, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23879 Share on other sites More sharing options...
Jessica Posted April 4, 2006 Author Share Posted April 4, 2006 [!--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? Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23881 Share on other sites More sharing options...
redbullmarky Posted April 4, 2006 Share Posted April 4, 2006 [!--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] Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23882 Share on other sites More sharing options...
Jessica Posted April 4, 2006 Author Share Posted April 4, 2006 [!--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 :) Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23884 Share on other sites More sharing options...
djnrempel Posted April 4, 2006 Share Posted April 4, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23890 Share on other sites More sharing options...
Jessica Posted April 4, 2006 Author Share Posted April 4, 2006 Actually the way redbullmarky posted worked perfectly.Here's what I get now when B = 70/7: 151/7: 172/7: 203/7: 234/7: 265/7: 296/7: 327/7: 35 Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23894 Share on other sites More sharing options...
jvrothjr Posted April 4, 2006 Share Posted April 4, 2006 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 = FPERCENT PER STEP G 2.2222 F / (C -B) = GPERCENT AT NUMBER H 23.8889 ( G * ( A - B ) ) = HThis was a little odd had to think on this one lol....... bad I knowbut I think we got it1) 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.8889so 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 Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23904 Share on other sites More sharing options...
djnrempel Posted April 4, 2006 Share Posted April 4, 2006 [!--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 = 70/7: 151/7: 172/7: 203/7: 234/7: 265/7: 296/7: 327/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 ;) Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23955 Share on other sites More sharing options...
Jessica Posted April 4, 2006 Author Share Posted April 4, 2006 [!--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! Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23957 Share on other sites More sharing options...
redbullmarky Posted April 4, 2006 Share Posted April 4, 2006 [!--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? Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23976 Share on other sites More sharing options...
Jessica Posted April 4, 2006 Author Share Posted April 4, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/6575-mathlogic-help/#findComment-23985 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.