I want to found RHS = LHS in php [duplicate] - php

This question already has answers here:
Is floating point math broken?
(31 answers)
Comparing floats - same number, but does not equal? [duplicate]
(3 answers)
Closed 2 years ago.
I want to check the given right hand side equation is equal to the left hand side or not. The value of $x = (19/30).
(((6*($x)) - 5)/(7 - (3*($x)))) = ((4 - (8 * $x))/((4 * $x) + 2))
I had tried below mentioned code, but it shown the RHS value is greater than LHS.
<?php
function check($m, $n)
{
$rhs = abs($m);
$lhs = abs($n);
if($rhs > $lhs){
echo "$m <strong style='color:red;'>is greater than</strong> $n";
}
elseif($rhs < $lhs) {
echo "$m <strong style='color:blue;'>is smaller than</strong> $n";
}
else{
echo "$m <strong style='color:green;'>is equal</strong> $n";
}
}
// $m = ((6*(19/30)) - 5)/(7 - (3*(19/30)));
// $n = (4 - (8 * (19/30)))/((4 * (19/30)) + 2);
$x = 19/30;
$m = abs(((6*($x)) - 5)/(7 - (3*($x))));
$n = abs((4 - (8 * $x))/((4 * $x) + 2));
check($m, $n);
?>
The output:
0.23529411764706 is greater than 0.23529411764706

Related

Can not get proper result for type casting in PHP [duplicate]

This question already has answers here:
How to convert float value to integer in php?
(6 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have a Float variable
$float = 37.80
Then I multiply the float by 100:
$mul = $float *100
Now I have 3780.
$typecasting = (int) $mul;
Now when I print $typecasting, it shows 3779
For example:
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = (int) $mul;
echo $typecasting;
// $typecasting shows 3779
I can't understand, how it works.
This problem occurs only for 32.80, 33.80, ...4.80. and also for multiples of 2, i.e. 65.60.
Just use floatval() http://php.net/manual/en/function.floatval.php
$float = 37.80;
$mul = $float*100;
echo $mul;
echo '<br>';
//$mul = 3780
$typecasting = floatval($mul);
echo $typecasting;
// $typecasting shows 3779

Division remainder return 2 zero [duplicate]

This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 4 years ago.
Modulus (%) operator return single zero. It is correct but i want to 2 zero after using modulus (%) operator. I don't want to use if condition.
$k = 60;
$result = $k%60;
echo "Result = ".$result;
Output
Result = 0
Expected output
Result = 00
try it :
$k = 60;
$result = $k%60;
echo str_pad($result, 2, '0', STR_PAD_LEFT);
or test this method :
$result=0
$pad_length = 2;
$pad_char = 0;
$str_type = 'd'; // treats input as integer, and outputs as a (signed)
decimal number
$format = "%{$pad_char}{$pad_length}{$str_type}";
// output and echo
printf($format, $result);

Replacing two Variables with each other's Value [duplicate]

This question already has answers here:
Is there a PHP function for swapping the values of two variables?
(20 answers)
Swap two variables value without using third variable in php [duplicate]
(2 answers)
Closed 4 years ago.
In this code
$x = 1;
$y = 2;
echo $x.$y; //12
I want to replace $x and $y vlaues with each other, So $x = 2 and $y = 1, Is that possible to be achieved without assigning middle-variables? Something like using a ready-function or like
$x = 1;
$y = 2;
echo $x.$y; //12
$x = &$y;
$y = &$x;
echo $x.$y //22
But instead of 22 I want to get 21.

variables float no equals in php why? [duplicate]

This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same

Check whether an integer is palindrome or not in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I check if a number is a palindrome?
If an integer is to not allowed to be treated a string and typecasting is not allowed, how can we figure out whether the number is palindrome or not(in PHP)?
The program which I have come up with is:
function checkPalindrome($number){
$reverse_number = 0;
$number_backup = $number;
while($number > 0){
$reverse_number = $reverse_number * 10 + $number % 10;
$number /= 10;
}
return $reverse_number == $number_backup;
}
At step "$number/=10", the result generated won't be integer which is creating the problem.
If you are not allowed to typecast and string-handling is forbidden, you need to do some extra calculations:
while($number > 0){
$lsd = $number % 10;
$reverse_number = $reverse_number * 10 + $lsd;
$number = ($number - $lsd) / 10;
}

Categories