PHP模数运算符为双精度数

Mis*_*hko 3 php double modulo

在这里读到:

具有非整数的模数将产生不可预测的结果.

但是,我尝试使用它,它似乎给出了非常可预测的结果:

function mod($a, $b) {
    echo "$a % $b = " . ($a % $b) . '<br>';
}
mod(10.3, 4);
mod(10.3, 2);
mod(-5.1, 3);

// OUTPUT:
//   10.3 % 4 = 2
//   10.3 % 2 = 0
//   -5.1 % 3 = -2
Run Code Online (Sandbox Code Playgroud)

换句话说,double似乎转换为integer第一.

%第一个操作数是double什么时候有没有定义?

Cla*_*ass 5

使用:

fmod(10.3, 4)
Run Code Online (Sandbox Code Playgroud)

我也必须做同样的事情,但我注意到双打被转换为int并且使用fmod返回一个double.

如果这有帮助

来自http://php.net/manual/en/language.operators.arithmetic.php

The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.