This question already has answers here:
Comparing floats - same number, but does not equal? [duplicate]
(3 answers)
Is floating point math broken?
(31 answers)
Closed 2 months ago.
dump($available_funds);
dump($meal_price);
if ($available_funds < $meal_price) {
dd('hit');
return false;
}
$available_funds and $meal_price are both 'double' values set to 2.78
Why would the if statement be hit when the values are the same?
I have attempted to (float) the variables and floatval() to try and update the types to see if this would resolve the condition but had no luck.
The problem may be due to the precision of the double data type. double values can have up to 15 decimal digits of precision, but in some cases, the actual value stored may not have the same precision as the declared type. This can cause problems when comparing double values, as the values may not be exactly equal even if they appear to be the same.
One solution to this problem is to use the round() function to round the values to a specific number of decimal places before comparing them. For example, you could use the following code to compare the values with two decimal places of precision:
$available_funds = round($available_funds, 2);
$meal_price = round($meal_price, 2);
if ($available_funds < $meal_price) {
dd('hit');
return false;
}
Related
This question already has answers here:
Comparing String to Integer gives strange results
(5 answers)
How does PHP compare strings with comparison operators?
(4 answers)
Closed 3 years ago.
This is my code
$preference = '151000';
$range = 'above';
if($preference <= $range){
echo "Yes"; die;
}else{
echo "No"; die;
}
This provides 'Yes', i want to know why.
You can see it in the php manual. https://www.php.net/manual/en/language.operators.comparison.php
When comparing a string, number or resource with another string, number or resource:
Translate strings and resources to numbers, usual math
Btw: '151000' is a string, not a number. 15100 would be a number.
Here you basically compare two strings and php uses their ASCII codes to compare them. The first symbol 1 is lower than 'a'.
If you want to compare two strings properly, use function:
strcmp()
If you want compare different types, you can read about PHP type comparison tables.
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
Read more here.
This question already has answers here:
Delete digits after two decimal points, without rounding the value
(15 answers)
Closed 3 years ago.
I'm trying to show one decimal place for rating number I have
but it returns unexpected value.
I used number_format and the round functions and both have the same issue, or i'm doing something wrong.
I tried to make this number show one decimal number
4.96 and it always returns 5 instead of 4.9
number_format(4.96, 1)
round(4.96, 1)
round(4.96, 1,PHP_ROUND_HALF_DOWN)
both functions returns 5 instead of 4.9
I searched all answers but couldn't find anything helpful.
Rounding 4.96 will round .9 up, so it will be 5 in all cases. If you want to do it without rounding, you may have to tweak it a bit to fool it:
floor(4.96 * 10) / 10; // 4.9
Here's a function you can use to achieve this.
function convertToSingleDecimal($num, $precision = 2) {
return floor($num) . substr(str_replace(floor($num), '', $num), 0, $precision + 1);
}
print convertToSingleDecimal("4.96", 1);
This question already has answers here:
PHP is_nan() not check for a number
(2 answers)
PHP returning NaN
(5 answers)
In Java, what does NaN mean?
(11 answers)
Why is NaN not equal to NaN? [duplicate]
(6 answers)
Closed 4 years ago.
What is the concept of NaN in PHP? When and where is it used, and why this is useful?
What is this below function used for? And please, I need an explanation of the code below.
<?php echo is_nan(200) . "<br>"; echo is_nan(acos(1.01)); ?>
NaN means "Not a Number". We basically call imaginary numbers (eg: Complex Numbers) as NaN, for eg: square root of -1 (i), acos(1.01). These numbers cannot be computed / calculated.
Reference:
nan/"not a number" is not meant to see if the data type is
numeric/textual/etc..
NaN is actually a set of values which can be stored in floating-point
variables, but dont actually evaluate to a proper floating point
number.
The floating point system has three sections: 1 bit for the sign
(+/-), an 8 bit exponent, and a 23 bit fractional part. There are
rules governing which combinations of values can be placed into each
section, and some values are reserved for numbers such as infinity.
This leads to certain combinations being invalid, or in other words,
not a number.
From the documentation of is_nan() function:
bool is_nan ( float $val )
Details: Checks whether val is 'not a number', like the result of acos(1.01).
Returns TRUE if val is 'not a number', else FALSE.
So, basically this function is used for checking the validity of return values of mathematical functions and operations, and expects a float as a parameter.
Now, 200 is a valid number. So, is_nan(200) will return False.
While, acos(1.01) is trying to find arc cosine of 1.01. From Mathematics, we very well know that cosine function returns value in the range of -1 to +1. So value of 1.01 (more than 1) cannot exist. Hence, acos(1.01) cannot be determined. Thus it is not a valid number. Hence, is_nan(acos(1.01)) will return True.
This question already has an answer here:
Typecasting vs function to convert variable type in PHP
(1 answer)
Closed 6 years ago.
I've got some strings that I need to cast as floats but have seen two apparently different ways of doing it:
$float = floatval ($float);
and
$float = (float) $float;
Are there any differences between the two methods?
In all other cases it will be evaluated as a float. In other words, the $string is first interpreted as INT, which cause overflow (The $string value 2968789218 exceeds the maximum value ( PHP_INT_MAX ) of 32-bit PHP, which is 2147483647.), then evaluated to float by (float) or floatval()
Please have look at PHP Convert String into Float/Double
The best answer for this is Typecasting vs function to convert variable type in PHP
This question already has answers here:
Compare floats in php
(17 answers)
Closed 7 years ago.
I was working on a code and I could not understand the weird result I was getting.
<?php
$a = 0.01;
$p = pow(0.1, 2); // result: 0.01
if( $a < $p ){
echo "true";
}
?>
The result of this condition is always "true" while both of the variables have same value, but the result coming from pow is changing something internally. Seems like I would not be able to rely on this function. Would someone please help me figuring this out ?
its because of float inaccuracy,
take a look at answered question mentioned in comment by b0s3
Read the red warning first
http://www.php.net/manual/en/language.types.float.php. You must never
compare floats for equality. You should use the epsilon technique.
For example:
if (abs($a-$b) < EPSILON) { … } where EPSILON is constant representing
a very small number (you have to define it)
https://stackoverflow.com/a/3149007/4998045
so you can trust pow function but you cant trust float comparsion
PHP Docs said:
base raised to the power of exp. If both arguments are non-negative integers and the result can be represented as an integer, the result will be returned with integer type, otherwise it will be returned as a float.
Maybe you need to convert all to int or all to float.
if( (float)$a < (float)$p ){
echo "true";
}
See it run:
http://phpfiddle.org/main/code/2hv5-n2fw