Maths – Rounding and related Functions – CSKC
Sign In

Search

Code Learning #CPP#2135    siteicon   siteicon  

Maths – Rounding and related Functions

Rounding and related functions like ceil, floor, round, rint, trunc etc. in c++

Learning Objectives

  • Learning the use of ceil, floow, round, rint and trunc functions in the math library.

Source Code

TC++ #2135

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

double a=2.34; double b=-5.61;  double c=3.55;
Three double type variables have been declared and initialised here for use in maths rounding functions.

cout<<“ceil(“<<a<<“) = “<<ceil(a)<<endl;
ceil() or the function which is called the ceiling function brings the current value to its ceiling or nearest high value of integral type, so 2.34 should become 3.

cout<<“floor(“<<b<<“) = “<<floor(b)<<endl;
floor() function bring the value to its nearest lower side integral type so -5.61 will become -6 as -6 is lower than 5.61.

cout<<“round(“<<c<<“) = “<<round(c)<<endl;
round() function rounds off to nearest integral type which means if it is 5.3 it will become 5 and if it is 5.5 it will become 6. In the case of given value 3.55 it will become 4

cout<<“rint(“<<c<<“) = “<<rint(c)<<endl;
This is a special rounding function which not only rounds of but also makes the output as integer data type. so rint(3.55) will become 4 with integer types as its data type.

cout<<“trunc(“<<c<<“) = “<<trunc(c)<<endl;
The trunc() function truncates the fractional portion without changing the data type. So 3.55 will become 3 but the data type will remain double type if input is double.

Notes

  • On modern compilers the use of cmath library header is recommended in place of math.h


Suggested Filename(s): math-round.cpp



Share

Created: 3-Jan-2018 18:00 | Updated: 23-Dec-2018 19:26|