Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,880 questions

51,806 answers

573 users

How to use rint() function to round N to an integral value using the rounding direction fegetround in C++

1 Answer

0 votes
#include <iostream>
#include <cmath>
#include <cfenv> 

using namespace std;

int main()
{
	fesetround(FE_TONEAREST);
	cout << "rounding to nearest :" << endl;
	cout << "rint(3.3) = " << rint(3.3) << endl;
	cout << "rint(3.5) = " << rint(3.5) << endl;
	cout << "rint(4.5) = " << rint(4.5) << endl;
	cout << "rint(4.6) = " << rint(4.6) << endl;
	cout << "rint(-3.3) = " << rint(-3.3) << endl;
	cout << "rint(-3.5) = " << rint(-3.5) << endl;
	cout << "rint(-4.5) = " << rint(-4.5) << endl;
	cout << "rint(-4.6) = " << rint(-4.6) << endl << endl;

	fesetround(FE_DOWNWARD);
	cout << "rounding down: " << endl;
	cout << "rint(3.3) = " << rint(3.3) << endl;
	cout << "rint(3.5) = " << rint(3.5) << endl;
	cout << "rint(4.5) = " << rint(4.5) << endl;
	cout << "rint(4.6) = " << rint(4.6) << endl;
	cout << "rint(-3.3) = " << rint(-3.3) << endl;
	cout << "rint(-3.5) = " << rint(-3.5) << endl;
	cout << "rint(-4.5) = " << rint(-4.5) << endl;
	cout << "rint(-4.6) = " << rint(-4.6) << endl << endl;;

	fesetround(FE_UPWARD);
	cout << "rounding up: " << endl;
	cout << "rint(3.3) = " << rint(3.3) << endl;
	cout << "rint(3.5) = " << rint(3.5) << endl;
	cout << "rint(4.5) = " << rint(4.5) << endl;
	cout << "rint(4.6) = " << rint(4.6) << endl;
	cout << "rint(-3.3) = " << rint(-3.3) << endl;
	cout << "rint(-3.5) = " << rint(-3.5) << endl;
	cout << "rint(-4.5) = " << rint(-4.5) << endl;
	cout << "rint(-4.6) = " << rint(-4.6) << endl << endl;

	fesetround(FE_TOWARDZERO);
	cout << "rounding toward zero: " << endl;
	cout << "rint(3.3) = " << rint(3.3) << endl;
	cout << "rint(3.5) = " << rint(3.5) << endl;
	cout << "rint(4.5) = " << rint(4.5) << endl;
	cout << "rint(4.6) = " << rint(4.6) << endl;
	cout << "rint(-3.3) = " << rint(-3.3) << endl;
	cout << "rint(-3.5) = " << rint(-3.5) << endl;
	cout << "rint(-4.5) = " << rint(-4.5) << endl;
	cout << "rint(-4.6) = " << rint(-4.6) << endl;

	return 0;

}

/*
run:

rounding to nearest :
rint(3.3) = 3
rint(3.5) = 4
rint(4.5) = 4
rint(4.6) = 5
rint(-3.3) = -3
rint(-3.5) = -4
rint(-4.5) = -4
rint(-4.6) = -5

rounding down:
rint(3.3) = 3
rint(3.5) = 3
rint(4.5) = 4
rint(4.6) = 4
rint(-3.3) = -4
rint(-3.5) = -4
rint(-4.5) = -5
rint(-4.6) = -5

rounding up:
rint(3.3) = 4
rint(3.5) = 4
rint(4.5) = 5
rint(4.6) = 5
rint(-3.3) = -3
rint(-3.5) = -3
rint(-4.5) = -4
rint(-4.6) = -4

rounding toward zero:
rint(3.3) = 3
rint(3.5) = 3
rint(4.5) = 4
rint(4.6) = 4
rint(-3.3) = -3
rint(-3.5) = -3
rint(-4.5) = -4
rint(-4.6) = -4

*/

 



answered Mar 29, 2016 by avibootz
...