Monday, February 9, 2009

Strange PHP Bug

Here's a little more info on the strange bug I encountered...

In PHP, the pow() function is used for calculating powers. "pow(a, b)" returns a^b. Then there's another function called rand(), which can be used for generating random numbers. "rand(1, 100)" returns a random number between 1 and 100.

What do these functions have in common? I don't know. Apparently though, on the new server, once a random number is generated, the next pow() function will always return 0. Mind = blown.

Example:
<?php

echo
pow(1.1, 50); // Prints: 117.39085288
echo pow(1.1, 50); // Prints: 117.39085288
// etc...

$rand = rand(1, 100);

echo pow(1.1, 50); // Prints: 0
echo pow(1.1, 50); // Prints: 117.39085288
echo pow(1.1, 50); // Prints: 117.39085288
// etc...

?>
So really, the delay is not my fault! I submitted a ticket to NameCheap 5 minutes ago, we'll see what they make of it.

Bayo~

4 comments:

  1. For a temporary fix, couldn't you just make pow() run once after every rand()?

    ReplyDelete
  2. curb, that sounds like a programming hack, and its bad news for anything long term

    ReplyDelete
  3. I was thinking of it as purely a short-term fix. Definitely not something that would stay there forever.

    ReplyDelete
  4. Hah, that's not a bad idea. Wouldn't want any other mathematical bugs though. And frankly, if there's one bug, I always fear there are more. It might have effect on different functions too.

    The hosting company replied and agreed it was a strange bug. It's only reproducible on the server we're on, and all the other servers are working with the exact PHP version. Anyway, they're moving us to a new server now.

    ReplyDelete