diff --git a/paddle/fluid/operators/elementwise/elementwise_mod_op.h b/paddle/fluid/operators/elementwise/elementwise_mod_op.h index e568a5dc72c08a5673147af0fb9b38bdca3c9921..4306a471b76c5bd4f0a5284052d7d39aa5fbc279 100644 --- a/paddle/fluid/operators/elementwise/elementwise_mod_op.h +++ b/paddle/fluid/operators/elementwise/elementwise_mod_op.h @@ -29,7 +29,9 @@ struct ModFunctor { template struct ModFunctorFP { - inline HOSTDEVICE T operator()(T a, T b) const { return std::fmod(a, b); } + inline HOSTDEVICE T operator()(T a, T b) const { + return fmod(b + fmod(a, b), b); + } }; template diff --git a/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py b/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py index fcda179a093cf2306b9d79264c9118fe3b68b35c..c4740e063ff1baaaf4f2f13090eae5fcb7d3ec6c 100644 --- a/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py +++ b/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py @@ -71,7 +71,7 @@ class TestElementwiseModOpFloat(TestElementwiseModOp): def init_input_output(self): self.x = np.random.uniform(-1000, 1000, [10, 10]).astype(self.dtype) self.y = np.random.uniform(-100, 100, [10, 10]).astype(self.dtype) - self.out = np.fmod(self.x, self.y) + self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y) def test_check_output(self): self.check_output(atol=2e-5)