PHP, modulus and floating point numbers (srsly?)
On a nice sunny and quite warm september afternoon I decided I'll do the unspeakable and try to calculate the modulus of a float value. This is easy as pie, so I wrote
[sanchez] ~$ php -r "echo 0.1 % 1;"
What I expected was of course:
0.1
What I got was:
0
PHP, seriously? You're implicitly assuming integers, thus casting the numbers to integer and spit out just a simple and plain 0? PHP, we spent a very long time together and I mostly enjoyed it but I think you have to leave soon...
If you want the expected behaviour one has to use 'fmod':
[sanchez] ~$ php -r "echo fmod(0.1,1);"
In addition I asked my self: WWJSD (what would JavaScript do):
[sanchez] ~$ node > 0.1 % 1 0.1
WWPYD (what would Python do):
[sanchez] ~$ python Python 2.7.3 (default, Aug 1 2012, 05:14:39) >>> 0.1 % 1 0.1
So once again: PHP, srsly?










