How to use fmod() function to get the floating-point remainder (modulo) of the division x/y in C++

1 Answer

0 votes
using namespace std;

int main()
{
	cout << "fmod(4.3, 2.0) = " << fmod(4.3, 2.0) << endl;
	cout << "fmod(18.5, 4.2) = " << fmod(18.5, 4.2) << endl;
	cout << "fmod(5.1, 3.0) = " << fmod(5.1, 3.0) << endl;
	cout << "fmod(-5.1, 3.0) = " << fmod(-5.1, 3.0) << endl;
	cout << "fmod(5.1, -3.0) = " << fmod(5.1, -3.0) << endl;
	cout << "fmod(-5.1, -3.0) = " << fmod(-5.1, -3.0) << endl;
	cout << "fmod(0.0, 1.0) = " << fmod(0.0, 1.0) << endl;
	cout << "fmod(-0.0, 1.0) = " << fmod(-0.0, 1.0) << endl;
	cout << "fmod(3.1, INFINITY) = " << fmod(3.1, INFINITY) << endl;

	return 0;
}

/*
run:

fmod(4.3, 2.0) = 0.3
fmod(18.5, 4.2) = 1.7
fmod(5.1, 3.0) = 2.1
fmod(-5.1, 3.0) = -2.1
fmod(5.1, -3.0) = 2.1
fmod(-5.1, -3.0) = -2.1
fmod(0.0, 1.0) = 0
fmod(-0.0, 1.0) = -0
fmod(3.1, INFINITY) = 3.1

*/

 



answered Mar 18, 2016 by avibootz
...