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.
Related
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;
}
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:
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
This question already has answers here:
int variable with leading zero?
(5 answers)
Closed 7 years ago.
I want to know the difference between echo integer 204 and 0204 without quote.
echo 204; // output : 204
echo 0204; // output : 132
Numbers beginning with 0 (in many programming languages) are octal numbers. 204 outputs how you would expect because it begins with a number 1 through 9, signifying a decimal number. See php's documentation on integers.
Refer #Example 1
echo 204; // output : 204 because its a decimal number
echo 0204; // output : 132 because its a octal number
The answer is simple the first is decimal the second is Octal
Putting a leading zero on an integer literal in PHP code instructs PHP that the number is in Octal (base 8) format.
Echo, on the other hand, when invoked by itself with just a number, just takes the number fed to it and prints it in ordinary decimal form.
Octal 204 = Decimal 132.
http://php.net/manual/en/language.types.integer.php
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.