Jump to content

32-bit vs 64-bit division different


woodworks

Recommended Posts

Trying to write a decimal to base-32 converter and ran into a strange problem.

 

If I run this code:

 

<?php
$result = 657911351231 / 32;
echo $result;
?>

 

On a 32-bit machine the output is: 20559729725.969

 

On a 64-bit machine the output is: 20559729726

 

What gives? Why is the 32-bit system producing a more accurate result?

 

How do I force PHP to return the more accurate result on the 64-bit machine?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/251347-32-bit-vs-64-bit-division-different/
Share on other sites

Turns out the bug has nothing to do with 32-bit vs 64-bit. It's a bug that was addressed in PHP 5.3. The system I am running it on is an old Ubuntu 8.04 server which has PHP 5.2 installed. Also, the bug is just a formatting issue caused by the conversion from float to string. Using printf to format the output instead of a simple echo will force PHP 5.2 to display the correct value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.