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,883 questions

51,809 answers

573 users

How to use trunc() function to round N toward zero, and get the nearest integer not greater in magnitude than N in C++

1 Answer

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

using namespace std;

int main()
{
	cout << "trunc(3.3) = " << trunc(3.3) << endl;
	cout << "trunc(3.5) = " << trunc(3.5) << endl;
	cout << "trunc(4.5) = " << trunc(4.5) << endl;
	cout << "trunc(4.6) = " << trunc(4.6) << endl;
	cout << "trunc(-3.3) = " << trunc(-3.3) << endl;
	cout << "trunc(-3.5) = " << trunc(-3.5) << endl;
	cout << "trunc(-4.5) = " << trunc(-4.5) << endl;
	cout << "trunc(-4.6) = " << trunc(-4.6) << endl;
	cout << "trunc(-0.0) = " << trunc(-0.0) << endl;

	return 0;
}

/*
run:

trunc(3.3) = 3
trunc(3.5) = 3
trunc(4.5) = 4
trunc(4.6) = 4
trunc(-3.3) = -3
trunc(-3.5) = -3
trunc(-4.5) = -4
trunc(-4.6) = -4
trunc(-0.0) = -0

*/

 



answered Mar 31, 2016 by avibootz
...