Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 two 0s??

Try this out:

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

Result is 4. Expected 0.

I must be forgetting something exceedingly obvious...

like image 556
batman Avatar asked Feb 23 '23 23:02

batman


1 Answers

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.

like image 152
mario Avatar answered Mar 06 '23 15:03

mario