SirChick Posted October 8, 2007 Share Posted October 8, 2007 Hey guys, I been trying to create a mathematical thing to increase a total via a % but the % is worked out in a rather complex way ill step you through so it makes sense. The input from the user is firstly multiplied by 5 as it costs 5 per 1 input. $StrengthInput = mysql_real_escape_string($_POST['strength']); $StrengthMultiply = $StrengthInput * 5; A random is then created to create a small % number which is used against the total "happy" that the user has from the database: //Happy is a field in the database in this situation it is current set at 10 $Happy = $row['CurrentHappy']; $PercentageHappyAddOn = array(); $PercentageHappyAddOn[] = 0.1; $PercentageHappyAddOn[] = 0.3; $PercentageHappyAddOn[] = 0.02; $PercentageHappyAddOn[] = 0.01; $PercentageHappyAddOn[] = 0.03; $PercentageKey = array_rand($PercentageHappyAddOn); $PercentageHappyAddOn = $PercentageResults[$PercentageKey]; //a calculation including the random and the the happy value of the user to make a new % $percentagehappy = $Happy * (1 + $PercentageHappyAddOn/100); Followed by this another random is created to generate another number which is then added on to the percentage created from above ^ // STRENTH $PercentageAddOn = array(); $PercentageAddOn[] = 0.26462; $PercentageAddOn[] = 0.52421; $PercentageAddOn[] = 0.56831; $PercentageAddOn[] = 0.11672; $PercentageKey = array_rand($PercentageAddOn); $PercentageAddOn = $PercentageResults[$PercentageKey]; //code line below now creates has a value of the numbers put together $NewPercentageStrength = $StrengthMultiply*($PercentageAddOn + $percentagehappy); After this i have a process which gets the Total strength of the user and the % gathered up from above... and it then gets the % of the total from the database and adds the % of that total on to the new total... Steps: 1)% 2)Strength 3) % of Strength = $value 4)Newtotal = $value + Strength But i believe this below section is what i have wrong from what i am actually trying to do as described in bold above... it was getting a bit beyond my skill level to figure it out...and i kinda got my own self confused! So was hoping could have some one else's view on it to see if you could help get me back on track ... //if statement to make sure the user did input something for strength If ($StrengthInput > 0){ $NewStrength = ($Strength) * (1 + $NewPercentageStrength); $GainTotalStrength = $NewStrength - $Strength; //make it only 5 decimal places $ValidateStrength = number_format($GainTotalStrength,5); //add on the % of the total on to the total again $StrengthSum = $GainTotalStrength + $Strength; //make it only 5 decimal places $DecimalRemovalStrength = number_format($StrengthSum,5); //add the new value to the field in the database by using UPDATE $addstrength = "UPDATE userregistration SET Strength=Strength+'$DecimalRemovalStrength' WHERE UserID='{$_SESSION['Current_User']}'"; $resultresult = mysql_query($addstrength) or die(mysql_error()); If (mysql_affected_rows() == 0) { die('Error ID 0001, contact admin on the Civilian forums immediatly about this error!'); } } Hope this makes sense.. if not just ask questions and ill try to answer... Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/ Share on other sites More sharing options...
btherl Posted October 8, 2007 Share Posted October 8, 2007 That's code is too complex to look through.. what you can do is add print statements to show each value throughout your script. You can remove the prints once the script works. For example: //make it only 5 decimal places $ValidateStrength = number_format($GainTotalStrength,5); print "Converted $GainTotalStrength to $ValidateStrength (5 decimal places)<br>"; If there's a problem, then you will see it as the print statement will not match what you expected. Then you fix the bug and continue debugging I'm suspicious that you divide $PercentageHappyAddOn by 100 when it's already so small. Is that intended? Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/#findComment-364256 Share on other sites More sharing options...
Rithiur Posted October 8, 2007 Share Posted October 8, 2007 What the code actually does is not hard to understand. What's much harder is understanding what it's supposed to do. There are no "obvious" errors in your mathematical calulations and all of it could make sense, depending on how it's actually suppose to work. What I'm saying, is that it would be considerably more helpful, if you gave example inputs and expected outputs. Without these, it's kind of hard to tell. But as btherl said, the easiest way for you to debug it yourself, is to keep printing out the values through the calculation to tell where the resulted values differ from expected values. Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/#findComment-364441 Share on other sites More sharing options...
SirChick Posted October 8, 2007 Author Share Posted October 8, 2007 Ok, User Inputs say 2. Which is multiplied. It is multiplied because it cost's the user 5 "energy". So $StrengthMultiply = 10 Lets say User happy = 1000 $PercentageHappyAddOn[] = 0.1; $percentagehappy = $Happy * (1 + $PercentageHappyAddOn/100); So we got: $percentagehappy = 0.011 Then another random to add to that value: $PercentageAddOn[] = 0.26462; $NewPercentageStrength = $StrengthMultiply*($PercentageAddOn + $percentagehappy); $NewPercentageStrength = 10 * (0.26462 + 0.011) Which is 3.7462. User's total strength = 1000 Then this code below does this: Find 3.7462% of 1000 and what ever that value is add that onto 1000. So in conclusion, the user input's a number which increases their strength but i made it complex so they cannot work out an exact pattern of increase which would reduce them being able to work out other player's strength via days old. Which adds more of a challenge to the user's gameplay. The result was the increase was going up by 2 every time ... so say the user strength was: 13.4553 then the user inputs 3 the result was 15.4553 then if the user inputs 2 the result was 17.4553 which proves that the random isn't working and it isn't increasing by the total % that it's ment to. It should increase per train in a curve type effect.. the higher the user's total the more gain they get. Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/#findComment-364607 Share on other sites More sharing options...
Rithiur Posted October 8, 2007 Share Posted October 8, 2007 Are you aware of the fact that PHP does actually take into account the operator precedence? In other words, the / operator is counted before the + operator. Thus your: (1 + $PercentageHappyAddOn/100) Will end up being 1.001 (if the variable is 0.1) because it will first calculate the $var/100 and then add the +1 to it . Perhaps what you meant to use was: ((1 + $PercentageHappyAddOn) / 100) Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/#findComment-364617 Share on other sites More sharing options...
d22552000 Posted October 11, 2007 Share Posted October 11, 2007 always remember BEDMAS everything in life follow bedmass, devide is before addition, so it iwll do devide first unless you put it in brackets (B) woot, I guess it helps that im in grade 9 Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/#findComment-366751 Share on other sites More sharing options...
wsantos Posted October 11, 2007 Share Posted October 11, 2007 Im using PEMDAS rather than BEDMAS anyway If that doesn't work try checking this. I don't see any value of $Strength prior to this line. $NewStrength = ($Strength) * (1 + $NewPercentageStrength); Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/#findComment-366802 Share on other sites More sharing options...
SirChick Posted October 11, 2007 Author Share Posted October 11, 2007 I think its working... ! Thanks for the maths help guys Quote Link to comment https://forums.phpfreaks.com/topic/72232-solved-my-maths-logic/#findComment-367041 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.