#include <cmath>
#include <iostream>
int main()
{
// C++11: remainder = numerator - integerDividend * denominator
// Notes:
// integerDividend = round(numerator/denominator) and is even when
// abs(integerDividend - numerator/denominator) = 1/2
std::cout << std::remainder(1.0, 1.0) << " "; // 0 = 1 - round(1/1) * 1
std::cout << std::remainder(1.0, 2.0) << " "; // 1 = 1 - round(1/2) * 2
std::cout << std::remainder(1.0, 3.0) << " "; // 1 = 1 - round(1/3) * 3
std::cout << std::remainder(1.0, 4.0) << " "; // 1 = 1 - round(1/4) * 4
std::cout << std::remainder(2.0, 1.0) << " "; // 0 = 2 - round(2/1) * 1
std::cout << std::remainder(3.0, 1.0) << " "; // 0 = 3 - round(3/1) * 1
std::cout << std::remainder(4.0, 1.0) << " "; // 0 = 4 - round(4/1) * 1
std::cout << ": ";
std::cout << std::remainder(2.0, 2.0) << " "; // 0 = 2 - round(2/2) * 2
std::cout << std::remainder(2.0, 3.0) << " "; // -1 = 2 - round(2/3) * 3
std::cout << std::remainder(2.0, 4.0) << " "; // -2 = 2 - round(2/4) * 4
std::cout << std::remainder(3.0, 2.0) << " "; // -1 = 3 - round(3/2) * 2
std::cout << std::remainder(4.0, 2.0) << " "; // 0 = 4 - round(4/2) * 2
std::cout << ": ";
std::cout << std::remainder(3.0, 3.0) << " "; // 0 = 3 - round(3/3) * 3
std::cout << std::remainder(3.0, 4.0) << " "; // -1 = 3 - round(3/4) * 4
std::cout << std::remainder(4.0, 3.0) << " "; // 1 = 4 - round(4/3) * 3
std::cout << ": ";
std::cout << std::remainder(4.0, 4.0) << " "; // 0 = 4 - round(4/4) * 4
std::cout << ": ";
std::cout << std::remainder(3.0, 5.0) << " "; // -2 = 3 - round(3/5) * 5
std::cout << std::remainder(5.0, 3.0) << " "; // -1 = 5 - round(5/3) * 3
std::cout << std::remainder(-10.0, 3.0) << " ";//-1=10-round(-10/3)*3
std::cout << std::endl;
return 0;
}
// Output: 0 1 1 1 0 0 0 : 0 -1 2 -1 0 : 0 -1 1 : 0 : -2 -1 -1
Reference: http://www.cplusplus.com/reference/cmath/remainder/
Monday, December 11, 2017
C++11: std::remainder()
C++11 added the std::remainder() function. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment