add check for mod when y in (-1, 0)

This commit is contained in:
buxue 2020-09-02 18:08:26 +08:00
parent 57215ef000
commit 4d59fe9ee1
2 changed files with 101 additions and 5 deletions

View File

@ -133,11 +133,7 @@ T InnerScalarMod(T x, T y) {
if (std::is_integral<T>::value) {
return static_cast<int>(x) % static_cast<int>(y);
}
int x_int = std::floor(x);
int y_int = std::ceil(y);
int max = x_int / y_int;
float ret = x - y * max;
return ret;
return x - y * std::floor(x / y);
}
template <typename T, typename U>

View File

@ -0,0 +1,100 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test mod"""
import mindspore.nn as nn
from mindspore import context
context.set_context(mode=context.GRAPH_MODE)
def test_positive_mod_positive():
class Mod(nn.Cell):
def __init__(self, x, y):
super(Mod, self).__init__()
self.x = x
self.y = y
def construct(self):
return self.x % self.y
x = 3.0
y = 1.3
mod_net = Mod(x, y)
expect = x % y
assert abs(mod_net() - expect) < 0.000001
def test_positive_mod_negative():
class Mod(nn.Cell):
def __init__(self, x, y):
super(Mod, self).__init__()
self.x = x
self.y = y
def construct(self):
return self.x % self.y
x = 3.0
y = -1.3
mod_net = Mod(x, y)
expect = x % y
assert abs(mod_net() - expect) < 0.000001
def test_negative_mod_positive():
class Mod(nn.Cell):
def __init__(self, x, y):
super(Mod, self).__init__()
self.x = x
self.y = y
def construct(self):
return self.x % self.y
x = -3.0
y = 1.3
mod_net = Mod(x, y)
expect = x % y
assert abs(mod_net() - expect) < 0.000001
def test_negative_mod_negative():
class Mod(nn.Cell):
def __init__(self, x, y):
super(Mod, self).__init__()
self.x = x
self.y = y
def construct(self):
return self.x % self.y
x = -3.0
y = -1.3
mod_net = Mod(x, y)
expect = x % y
assert abs(mod_net() - expect) < 0.000001
def test_int_mod_int():
class Mod(nn.Cell):
def __init__(self, x, y):
super(Mod, self).__init__()
self.x = x
self.y = y
def construct(self):
return self.x % self.y
x = 3
y = 2
mod_net = Mod(x, y)
expect = x % y
assert abs(mod_net() - expect) < 0.000001