Jump to content

Recommended Posts

I have a mathematical calculation, and for some reason if i put "1" into the input this is the result i get :

 

1 Muscle Building sessions giving you a strength gain of 4.99999999999E-5.

 

I have put a script in to cut the decimals so that it'll only go as far as 5 decimals but it still keeps coming up...

 

$DecimalRemovalStrengthresult = number_format($GainTotalStrength,5);
$DecimalRemovalStrength = number_format($NewStrength,5);

 

That is what im using to strip the number down so it should only show "4.99999"

 

If i don't put "1" and I put say anything above 1 it'll work fine and only show a figure of 5 decimal places such as 4.99000.

 

So what is actually happening to the number to come up with 4.99999999999E-5 ? Does the value mean infinite number or something else?

Link to comment
https://forums.phpfreaks.com/topic/71698-maths-result/
Share on other sites

 

Values that im using:

$Percentage1 in this situation is = 0.0001
$StrengthInput = 5
$Happy =  1250

 

 

 

$Strength = $row['Strength'];
$StrengthInput = mysql_real_escape_string($_POST['strength']);



$Happy = $row['CurrentHappy'];

$percentage2 = $Happy * (1 + $percentage1/100);

$PercentageAddOn = array();
			$PercentageAddOn[] = 1.3232;
			$PercentageAddOn[] = 3.4221;
			$PercentageAddOn[] = 1.4221;
			$PercentageAddOn[] = 1.5;
			$PercentageAddOn[] = 1.0;

		$PercentageKey = array_rand($PercentageAddOn);
		$PercentageAddOn = $PercentageResults[$PercentageKey];

$NewPercentageStrength = $StrengthInput*($PercentageAddOn + $percentage1 + $percentage2);


If ($StrengthInput > 0){

				$NewStrength = ($Strength) * (1 + $NewPercentageStrength/100);
$GainTotalStrength = $NewStrength - $Strength;
$DecimalRemovalStrengthresult = number_format($GainTotalStrength,5);
$DecimalRemovalStrength = number_format($NewStrength,5);
				$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!');
				}

			}

Link to comment
https://forums.phpfreaks.com/topic/71698-maths-result/#findComment-360940
Share on other sites

Unfortunately, since I don't know what the goal of your program is, I can't really tell which calculations are faulty.  There are also some variables in there that I can't tell what they do.

 

I would suggest adding this line of code for debugging

 

echo "<p>Line " . __LINE__ . ": $variable_you_want_to_know_the_value_of</p>";

 

Adding a bunch of these along the way will give you a lot of intermediate values, which will help you narrow down which calculation is going funky.

Link to comment
https://forums.phpfreaks.com/topic/71698-maths-result/#findComment-360947
Share on other sites

Hey thanks for the advice... jsut to add its basically a simple increment of a total via a % + what the user input... example:

 

User inputs 5.

 

This is multiplied by 10

Then that 10 + the example "5%" added on to that.... equals for this example 15%

 

Then the total from the field in the database should increase by 15% of what it is.

 

Thats what happens in its simplistic nature but there its meant to be slightly random to avoid user's working out the pattern.

Link to comment
https://forums.phpfreaks.com/topic/71698-maths-result/#findComment-360978
Share on other sites

Hey thanks for the advice... jsut to add its basically a simple increment of a total via a % + what the user input... example:

 

User inputs 5.

 

This is multiplied by 10

Okay, you get 50.

 

Then that 10 + the example "5%" added on to that.... equals for this example 15%

What happened to the 50?

 

Thats what happens in its simplistic nature but there its meant to be slightly random to avoid user's working out the pattern.

 

I'm guessing that this part:

 

				$PercentageAddOn = array();
			$PercentageAddOn[] = 1.3232;
			$PercentageAddOn[] = 3.4221;
			$PercentageAddOn[] = 1.4221;
			$PercentageAddOn[] = 1.5;
			$PercentageAddOn[] = 1.0;

		$PercentageKey = array_rand($PercentageAddOn);
		$PercentageAddOn = $PercentageResults[$PercentageKey];

 

is where you are introducing randomness.  I think there are better ways to do this.

 

Also, some of your percentage values look funky.

 

$percentage2 = $Happy * (1 + $percentage1/100);
/*
You wrote

$Percentage1 in this situation is = 0.0001
$StrengthInput = 5
$Happy =  1250

Which would mean...

$percentage2 = 1250 * (1 + (0.0001 / 100))
$percentage2 = 1250 * (1 + (0.000001))
$percentage2 = 1250 * (1.000001)
$percentage2 = 1250.00125

*/

 

I don't know exactly how you're handling percentages in your code, but this doesn't seem right any way I look at it.

Link to comment
https://forums.phpfreaks.com/topic/71698-maths-result/#findComment-361001
Share on other sites

Crap... did a typo mistake:

 

This is multiplied by 2 to make 10.

 

 

Yeah i had that exactly written down yet the code is increase by no more than values of 124 to lowest which was 13.42

 

I don't know how lol It's not the first time I have had code do it's own thing from what i expected it to do.. im sure my logical order of math's is wrong than anything else... ill look into more.

 

Thanks for the help though.

Link to comment
https://forums.phpfreaks.com/topic/71698-maths-result/#findComment-361020
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.