Files
pytorch/c10/test/util/generic_math_test.cpp
Bin Bao 34c8033fd3 Fix a div_mod bug in generic_math.h (#157383)
Summary: There is a bug in integer div_mod that when the remainder is 0 and the divisor is negative, mod operation produces a negative number. Fixed in this PR.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/157383
Approved by: https://github.com/angelayi, https://github.com/jingsh
2025-07-02 12:22:57 +00:00

20 lines
550 B
C++

// Copyright (c) Meta Platforms, Inc. and affiliates.
#include <c10/util/generic_math.h>
#include <gtest/gtest.h>
#include <cmath>
using namespace ::testing;
TEST(GenericMathTest, div_floor_test) {
EXPECT_EQ(c10::div_floor_floating(5., 0.), INFINITY);
EXPECT_DOUBLE_EQ(c10::div_floor_floating(5., 2.), 2.);
EXPECT_DOUBLE_EQ(c10::div_floor_floating(5., -2.), -3.);
EXPECT_EQ(c10::div_floor_integer(5, 2), 2);
EXPECT_EQ(c10::div_floor_integer(5, -2), -3);
EXPECT_EQ(c10::div_mod(-9, -3), 0);
EXPECT_EQ(c10::div_mod(-9., -3.), 0.);
}