Fun with integers

firebus's picture


php > print 1100690227200 / 1099511627776;
1.0010719299316

php > print 1100690227200 % 1099511627776;
PHP Warning: Division by zero in php shell code on line 1

?


php > print PHP_INT_MAX;
2147483647

php > print gettype(1100690227200);
double

!

There is no integer division operator in PHP.

Operands of modulus are converted to integers (by stripping the decimal part) before processing.

*facepalm*

This makes perfect sense of course - modulus is only defined for integers. Somehow the 'divide by zero' error message leaves something to be desired...


Comments

What sound does a PHP geek make when he chuckles?

I don't know, but I just made it. (sorry, lost steam for my punchline to my own joke)


firebus's picture

Something like

the weird chuckle I got out of jrod the other day when I made a joke about the topological equivalence between the food pyramid and a quiche? It was really weird!

This was for a routine where I'm converting between a single variable representing a size in Bytes and 3 variables representing the same size (with less precision) in MB, GB and TB.

So I'm doing something like this:

$size = 1100690227200
$tb = (int) ($size / pow(1024, 4);
$size = $size % pow(1024 *4);
$gb = (int) ($size / pow(1024, 3);
$size = $size % (pow(1024, 3);
$mb = (int) ($size / pow(1024, 2);

There's probably a more elegant way to do this...


I am without word...

I am without word...


Powered by Drupal - Design by Artinet - Amazon Affiliate