Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My code is:
$a = $_GET["valA"];
$b = $_GET["valB"];
$c = $_GET["valC"];
$d = pow($b,2) - 4 * $a * $c;
$e = sqrt($d);
$f = $e - $b;
$g = 2 * $a;
$h = $f / $g;
echo "x = $h";
And it is returning:
x = NAN
Please point out my mistake.
Issue is with $e = sqrt($d);
Whenever $d gives a NEGATIVE value sqrt($d) returns NAN.
EXAMPLE
echo $e = sqrt(-4);
returns NAN
AND
echo $e = sqrt(4);
returns 2
NAN simply means Not A Number. The values you are pulling from the URL may be strings, which would cause this issue.
Also I think the problem may occur if you are taking the square root of a negative number.
You could use something like this to solve it
<?php
$a = 10;
$b = 20;
$c = 30;
$d = pow($b,2) - 4 * $a * $c;
if($d < 0){
$d *= -1;
}
$e = sqrt($d);
$f = $e - $b;
$g = 2 * $a;
$h = $f / $g;
?>
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
how to make multiplication by 5%, for example like this.
200000 x 5% = 10000
So that's how it is?
<?php
$a = 200000;
$b = 5;
$x = $a % $b;
echo $x;
?>
Why not convert the percentage value to a decimal and multiple?
$a = 200000;
$b = 5;
$x = $a * ($b / 100);
echo $x;
The return value will be a float.
Although, I'm still somewhat unclear as to what you are actually asking.
Use normal calculation to multiplication by %
Code:
<?php
$a = 1000;
$b = 2000;
$x = $a * 100 / 100 * $b;
echo $x;
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
function standard_deviation_population ($a)
{
//variable and initializations
$the_standard_deviation = 0.0;
$the_variance = 0.0;
$the_mean = 0.0;
$the_array_sum = array_sum($a); //sum the elements
$number_elements = count($a); //count the number of elements
//calculate the mean
$the_mean = $the_array_sum / $number_elements;
//calculate the variance
for ($i = 0; $i < $number_elements; $i++)
{
//sum the array
$the_variance = $the_variance + ($a[$i] - $the_mean) * ($a[$i] - $the_mean);
}
$the_variance = $the_variance / $number_elements;
//calculate the standard deviation
$the_standard_deviation = pow( $the_variance, 0.5);
//return the variance
return $the_standard_deviation;
}
$query_question = mysqli_query($con,"SELECT * FROM question order by s_id");
$QuestionDimArray = array();
while ($data_question = mysqli_fetch_array($query_question))
{
$QuestionDimArray []= $data_question['q_id'];
}
$a = array($QuestionDimArray());
$standard_deviation = standard_deviation_population ($a);
echo "standard_deviation =" .$standard_deviation;
How to store database value into
$variable = array([database values here]);
for example, with defined values (1,2,3)
$a = array(1,2,3);
$standard_deviation = standard_deviation_population ($a);
echo "standard_deviation =" .$standard_deviation;
that returns,
standard_deviation = 0.81649658092773
It is not good idea to calculate the standard deviation this way. Database systems has a built-in functions for standard deviation. You can get it in one query, something like this: SELECT STDDEV_POP(q_id) FROM question.
In your code the $QuestionDimArray is already an array. You could just $standard_deviation = standard_deviation_population ($QuestionDimArray);
Your question is not clear but if I understand well you want put all your database result in $variable ?
$query_question = mysqli_query($con,"SELECT * FROM question order by s_id");
$QuestionDimArray = array();
while ($data_question = mysqli_fetch_array($query_question)) {
$QuestionDimArray []= $data_question['q_id'];
/* if you want the full data line from your query use. It depends your implementation.
You will have an 2d array
[['col1' => 'val_line_1_1', 'col2'=> 'val_line_1_2',...],...,['col1' => 'val_line_n_1', 'col2'=> 'val_line_n_2',...]]
$QuestionDimArray []= $data_question;
*/
}
// $a = array($QuestionDimArray()); unless you want copy the array this line is useless. Event worst its wrong
$standard_deviation = standard_deviation_population ($questionDimArray);
echo "standard_deviation =" .$standard_deviation;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would i go about writing the php code that would take the inputs of three form fields and calculate whether or not they result in a triangle?
Would the best way to go about be an if/else statement?
That is right, you would have to use if/ else, or at last I can not think of something else at the moment.
Here is a sample PHP code:
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
if (($a + $b > $c) && ($a + $c > $b) && ($b + $c > $a)) {
echo "Yes, this is a triangle!";
} else {
echo "No, this is not a triangle!";
}
To test use something like: http://localhost/TriangleChecker.php?a=4&b=6&c=8
3 side lengths a, b, and c are a triangle if a+b>c, a+c>b, and b+c>a. Here's the function you need:
/**
* Determine If Three Side Lengths Are a Triangle
* #param integer $a first length
* #param integer $b second length
* #param integer $c third lenght
* #return boolean true|false
*/
function isTriangle($a, $b, $c)
{
return (($a + $b > $c) && ($a + $c > $b) && ($c + $b > $a));
}
Example:
$a = 10;
$b = 12;
$c = 34;
$ret = isTriangle($a, $b, $c);
//var_dump(isTriangle($a, $b, $c));
if ($ret)
echo 'That\'s definitely a triangle';
else
echo 'That is not a triangle';
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone try to explain me why the output of this code is 2 , 12 ; 5 , 25 ?
<?php
function swap($x, $y){
$x = $x + 1;
$y = $y + 2;
return $x * $y;
}
$a = 2;
$b = swap($a, $a);
print "$a, $b";
$b = swap(&$a, &$a);
print "$a, $b";
?>
Output:
2, 12
5, 25
Here is some explanation. The first call I think you have understood. I will explain the result of the second call in the code. Here you are passing the variable by reference.
when you call by reference the reference to the variable is passed so any change gets reflected in the original variable.
<?php
function swap($x, $y){
//here your variable $a is 2
$x = $x + 1;
// added 1 to $a so $a is now 3
$y = $y + 2;
//again added 2 to variable $a becomes 5
return $x * $y; //returns 5*5=25
}
$a = 2;
$b = swap(&$a, &$a);
print "$a, $b";
?>
Hope this explanation helps.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Two hours ago the following script worked. Now for some reason, I'm getting an error "Warning: Division by zero in _ on line 30". Here's the script. Can anyone tell me what I'm doing wrong and how to correct it?
Basically this script is pulling data from two elements on another website, dividing them to get a number which in turn is being used to set the width of an element. Been troubleshooting this for a while. Thanks so much in advance!
<?php
define("FFF_SIXDEGREES", "http://www.stayclassy.org/fundraise?fcid=257739");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, FFF_SIXDEGREES);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if(!($results = curl_exec($curl))) {
print("{ \"total\": \"$0.00\" }");
return;
}
$pattern = '/<li class="goalTitle">Raised so far:<\/li>\s*<li>\$([\d\.,]+)<\/li>/';
preg_match($pattern, $results, $matches);
$total = $matches[1];
$total = str_replace(",", "", $total);
// printf("<h2 class=\"raised-total\">$%s</h2>", formatMoney($total, true));
$pattern2 = '/<li class="goalTitle">My goal:<\/li>\s*<li>\$([\d\.,]+)<\/li>/';
preg_match($pattern2, $results, $matches);
$total2 = $matches[1];
$total2 = str_replace(",", "", $total2);
// printf("<h2 class=\"goal-total\">$%s</h2>", formatMoney($total2, true));
$diff = ($total/$total2) * 100; // THIS IS THE LINE OF CODE IN QUESTION
function formatMoney($number, $fractional=false)
{
if ($fractional) {
$number = sprintf('%.2f', $number);
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
echo "<div class=\"progress-header\" style=\"width:$diff%;\"><span class=\"raised-amount\">$$total</span><span class=\"goal-amount\">$$total2</span></div>";
?>
The error message is helpfull here. On that line:
$diff = ($total/$total2) * 100;
the $total2 variable maybe sometime equal to 0, so php cannot execute the calculation (it's impossible to divide by zero). So you chould do somthing like this :
if($total2) // This condition will be true if $total2 != 0
$diff = ($total/$total2) * 100;
else
$diff = 0;
Also, I can see on your code that $total and $total2 are string, converted into float. This can be a source of error too. You should force the conversion to float using floatval :
if(floatval($total2))
$diff = ( floatval($total) / floatval($total2) ) * 100;
else
$diff = 0;
$total2 is 0 because you set wrong RegExp.
First, add i after last / in both patterns to change mode to case insensitive (you've got eg. Raised So Far on website but you're using Raised so far). Next, check what are you getting in your both $matches.
Given that it is actually only a warning and doesn't cause the script to stop, you can also use the following to simply hush the messages on that line:
<?php
$var=4;
$diff = (100/$var) * 100;
echo $diff."<br><br>";
$var=0;
$diff = (100/$var) * 100;
echo $diff."<br><br>";
$var=0;
$diff = #(100/$var) * 100; // Note the # symbol before the possible wanring
echo $diff."<br><br>";
?>
Output:
2500
Warning: Division by zero in C:\Server\www\test1.php on line 8
0
0
Basically, it will render as a zero. It's not the best way, but in terms of efficiency, if you are dealing with a lot of data, it might be better to not display the warning as compared to performing validation on each row of data.
It's probably not what I would do (though I have done it once) but I thought I would offer it as a suggestion.