Created
July 12, 2021 04:14
-
-
Save krshrimali/10ea4ec1aff9b48eff3e78b970608016 to your computer and use it in GitHub Desktop.
Check for Integral Inputs for Remainder Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cmath> | |
#include <vector> | |
int main() { | |
std::vector<std::vector<int>> samples = { {-7, -3}, {-7, 3}, {-5, 3}, {3, -2}, {3, 2}, {7, 2}, {7, -2} }; | |
std::cout << "Output format: a rem b is: std::remainder, a % b in C++, output of torch impl, output modified impl\n"; | |
for(auto vec: samples) { | |
int a = vec[0], b = vec[1]; | |
// PyTorch implementation for integers | |
int out_torch = a % b; | |
int intermediate = out_torch; | |
if (out_torch != 0 && (out_torch < 0) != (b < 0)) out_torch += b; | |
// Matches C++ output of std::remainder | |
auto out_correct = a - b * static_cast<int>(std::nearbyint(static_cast<float>(a)/b)); | |
std::cout << a << " rem " << b << " is: " << std::remainder(a, b) << ", " << | |
intermediate << ", " << out_torch << ", " << out_correct << '\n'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment