Asked  7 Months ago    Answers:  5   Viewed   40 times

This is really weird. I have this piece of code.

$rewardAmt = $amt;
      print_r("is float");die;
} else {
      print_r("is not float"); die;

value of $amt is 0.01. But it is going into else condition. So I did a var_dump of $amt. it says string(4) So I decided to typecast $amt

   $rewardAmt = (float)$amt;

But the problem with this is even if the value of $amt is 1, it still gets typecast to float and goes into if condition, which shouldn't happen. Is there any other way to do this ? Thanks



If you change the first line to

$rewardAmt = $amt+0;

$rewardAmt should be cast to a number.

Wednesday, March 31, 2021
answered 7 Months ago

A number like 6.2 can't be represented exactly using floating-point math in computers as there is no finite base-2 representation of it. What you are seeing when echo-ing the number is something intended for human reading, and thus the value will be rounded to what floats can provide in accuracy (about 6 decimal places for 32-bit and 17 for 64-bit FP values).

When serializing those values, however, you really want the exact value (i. e. all bits that are in there) and not just the nearest "nice" value. There could be more than one float/double representation which evaluates to approximately 6.2 and when serializing you usually really want to store he exact values to the last bit you are having in order to restore them correctly. That's why you're getting ridiculous "accuracy" in values there. It's all just to preserve the exact bit representation of what you started with.

But why exactly do you want to control the serialized output that tightly? I mean, it's just there so you can round-trip your data structure and read it back in later. You certainly don't want to use that serialized representation somewhere in output for humans or so. So if it's just about "nice-looking" values, you shouldn't use serialize which has an entirely different purpose.

Wednesday, March 31, 2021
answered 7 Months ago

There's no difference between the PHP versions that would cause this. There are differences between different mysql drivers, however, and that could cause the issue that you are seeing.

Homestead comes with php5-mysqlnd installed, which is the "MySql Native Driver". When using this driver, floating points and integers fetched from the database will be assigned as numeric datatypes in PHP. If you are not using the native driver (php5-mysql), floating points and integers fetched from the database will be assigned as strings in PHP.

The following code demonstrates how this affects your output:

$f = 9.99000;
$s = "9.99000";

echo $f; // shows 9.99
echo $s; // shows 9.99000

You can check to see if the server is using the native driver with the command php -i | grep mysqlnd. This is just searching through your phpinfo() for any mention of the native driver. If this doesn't return anything, then you are not using the native driver, and your numeric data will be returned as strings.

If you do not have the native driver installed, you will need to remove the old driver and install the new driver:

apt-get remove php5-mysql

apt-get install php5-mysqlnd

Assuming this was your issue in the first place, this will fix it. You can also check out this question and answer for more information.

Wednesday, March 31, 2021
answered 7 Months ago

You should absolutely use isEmpty(). Computing the size() of an arbitrary list could be expensive. Even validating whether it has any elements can be expensive, of course, but there's no optimization for size() which can't also make isEmpty() faster, whereas the reverse is not the case.

For example, suppose you had a linked list structure which didn't cache the size (whereas LinkedList<E> does). Then size() would become an O(N) operation, whereas isEmpty() would still be O(1).

Additionally of course, using isEmpty() states what you're actually interested in more clearly.

Tuesday, June 22, 2021
answered 4 Months ago
if ($number % 6 != 0) {
  $number += 6 - ($number % 6);

The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.

If decreasing is acceptable then this is even faster:

$number -= $number % 6;
Sunday, August 1, 2021
TV Nath
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :