Skip to content

Instantly share code, notes, and snippets.

@krshrimali
Created July 12, 2021 04:16
Show Gist options
  • Save krshrimali/5e0beca95ad0ca0bd4f05c0edb031bba to your computer and use it in GitHub Desktop.
Save krshrimali/5e0beca95ad0ca0bd4f05c0edb031bba to your computer and use it in GitHub Desktop.
Check for floating inputs for remainder implementation
#include <iostream>
#include <cmath>
#include <vector>
int main() {
std::vector<std::vector<double>> samples { {-7.5, -3.}, {-7.7, 3.1}, {-5.5, 3.}, {3., -2.5}, {3., 2.}, {7.3, 2.2}, {7.5, -2.} };
std::cout << "Output format: a rem b is: std::remainder, output before PR, output after PR\n";
for(auto vec: samples) {
double a = vec[0], b = vec[1];
// Before https://github.com/pytorch/pytorch/pull/37758
auto out_before = a - b * static_cast<int>(std::floor(a/b));
// After https://github.com/pytorch/pytorch/pull/37758
auto out_after = std::fmod(a, b);
if ((out_after != 0) && ((b < 0) != (out_after < 0))) out_after += b;
std::cout << a << " rem " << b << " is: " << std::remainder(a, b) << ", " <<
out_before << ", " << out_after << '\n';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment