#include <cmath>
#include <iostream>
using namespace std;
int main()
{
int quo = 0.0;
// C++11: remainder = numerator - integerDividend * denominator
// Notes:
// integerDividend = round(numerator/denominator) and is even when
// abs(integerDividend - numerator/denominator) = 1/2
cout << remquo(1.0, 1.0, &quo) << ":" << quo << " "; // 0:0
cout << remquo(1.0, 2.0, &quo) << ":" << quo << " "; // 1:1
cout << remquo(1.0, 3.0, &quo) << ":" << quo << " "; // 1:0
cout << remquo(1.0, 4.0, &quo) << ":" << quo << " "; // 1:0
cout << remquo(2.0, 1.0, &quo) << ":" << quo << " "; // 0:0
cout << remquo(3.0, 1.0, &quo) << ":" << quo << " "; // 0:2
cout << remquo(4.0, 1.0, &quo) << ":" << quo << " "; // 0:3
cout << ": ";
cout << remquo(2.0, 2.0, &quo) << ":" << quo << " "; // 0:4
cout << remquo(2.0, 3.0, &quo) << ":" << quo << " "; // -1:1
cout << remquo(2.0, 4.0, &quo) << ":" << quo << " "; // -2:1
cout << remquo(3.0, 2.0, &quo) << ":" << quo << " "; // -1:0
cout << remquo(4.0, 2.0, &quo) << ":" << quo << " "; // 0:2
cout << ": ";
cout << remquo(3.0, 3.0, &quo) << ":" << quo << " "; // 0:2
cout << remquo(3.0, 4.0, &quo) << ":" << quo << " "; // -1:1
cout << remquo(4.0, 3.0, &quo) << ":" << quo << " "; // 1:1
cout << ": ";
cout << remquo(4.0, 4.0, &quo) << ":" << quo << " "; // 0:1
cout << endl;
return 0;
}
// Output:
// 0:0 1:1 1:0 1:0 0:0 0:2 0:3 : 0:4 -1:1 2:1 -1:0 0:2 : 0:2 -1:1 1:1 : 0:1
Reference: http://en.cppreference.com/w/cpp/numeric/math/remquo
Monday, December 11, 2017
C++11: std::remquo()
C++11 added the function std::remquo(). Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment