douji2520 2010-09-11 02:11
浏览 20

PHP 64位数?

I have a simple function that I'm using but for some reason the number doesn't calculate correctly as it would in a calculator. I think it has something to do with the numbers being too large, or something to do with 64 bit. Is there any way I can convert them so that they would work correctly?

$sSteamComID = 76561197990369545;
$steamBase = 76561197960265728;

function convertToSteamID($sSteamComID) {
    $sServer = bcmod($sSteamComID, '2') == '0' ? '0' : '1';
 $sCommID = bcsub($sSteamComID, $sServer);
 $sCommID = bcsub($sCommID, $steamBase);
 $sAuth = bcdiv($sCommID, '2');

 echo "$sServer:$sAuth";
}

convertToSteamID($sSteamComID);

This function outputs 0:15051912 on a server when it should be printing 1:15051908

  • 写回答

1条回答 默认 最新

  • dsxcv5652 2010-09-11 04:04
    关注

    The missing global $steamBase was the problem, as already mentioned in a comment. (Tip: turn on E_NOTICE during development.) However, I'd like to address your question:

    I think it has something to do with the numbers being too large, or something to do with 64 bit. Is there any way I can convert them so that they would work correctly?

    PHP integers are signed and platform-dependent. Using 64-bit numbers will not work if you are on a 32-bit host.

    So your concern is valid. But even on a 64-bit system:

    $x = 9223372036854775808; // highest bit (64th) set
    var_dump($x);
    
    --> float(9.2233720368548E+18)
    

    Note that PHP's BC Math routines operate on strings, not integers. Thus, you should be storing your big numbers as strings.

    This will work around the potential problem of integers being converted to floats, which will happen even on your 64-bit environment if you are using large, unsigned integers.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法