开发者

PHP Math: Why is mod (%) not working with fac(13)?

Okay, this one is beyond me: I must be having a long day. Why does (13! mod 10) come out as 4, when the number ends in tw开发者_C百科o 0s??

Try this out:

<?php $thirteen_fac = 6227020800;
echo $thirteen_fac % 10; ?>

Result is 4. Expected 0.

I must be forgetting something exceedingly obvious...


6227020800 is too large for an integer (on a 32 bit system anyway). PHP will store it as float in your variable. The modulo operation will thus use an inexact up/downrounded number as basis.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜