ceil???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <conio>
#include <math>
int main()
{
   cout<<"\n1. sqrt(9) is "<<sqrt(9);
   cout<<"\n2. pow(5,3) is "<<pow(5,3);
   cout<<"\n3. ceil(2.5) is "<<ceil(2.5);
   cout<<"\n4. floor(2.5) is "<<floor(2.5);
   cout<<"\n5. ceil(2.1) is "<<ceil(2.1);
   cout<<"\n6. floor(2.1) is "<<floor(2.1);

   getch();
   return 0;
}


What is ceil use for? TQ...
ceil is used to round up a number.
1. Is that ceil use to round up to interger number ?
2. floor use to round down to integer number?

TQ...
yes.

tip: Want to round a number to the closest integer? use floor( number + 0.5 );
Thanks Jikax,
can I know what u mean by closest integer? I tried to modified the number inside line 9(2.4+0.5 should be 2.9, and closest integer is 3 right?), but the answer still 2...why is this happen? TQ...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <conio>
#include <math>
int main()
{
	cout<<"\n1. sqrt(9) is "<<sqrt(9);
   cout<<"\n2. pow(5,3) is "<<pow(5,3);
   cout<<"\n3. ceil(2.5) is "<<ceil(2.5);
   cout<<"\n4. floor(2.4+0.5) is "<<floor(2.5);  
   cout<<"\n6. floor(2.1) is "<<floor(2.1);

   getch();
   return 0;
}
Last edited on
Yes, that's because the closest integer to 2.4 is 2.
Last edited on
@Athar

I mean for this cout<<"\n4. floor(2.4+0.5) is "<<floor(2.5); as Jikax said use +0.5, but not sure is that what he mean...
it had nothing to do with his question, was just a tip. Sorry for the confusion
@jikax, but can it round to the nearest by using cout<<"\n4. floor(2.4+0.5) is "<<floor(2.5);?

Eg: cout<<"\n4. floor(2.6+0.5) is "<<floor(2.5);
originally i use 2.6 which if after floor, it should go to 2 but if i add with 0.5, then after floor it will go to 3.1, how? TQ...
i ment this with the closest intger:

example:

2.6 -> 3 is closest
2.4 -> 2 is closest

floor(2.6 + 0.5) = 3
floor(2.4 + 0.5) = 2
@Jikax

as u said, the floor of 2.6 is 3(closest), but when i run the program below, it gave me "2"...why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <conio>
#include <math>
int main()
{
	cout<<"\n1. sqrt(9) is "<<sqrt(9);
   cout<<"\n2. pow(5,3) is "<<pow(5,3);
   cout<<"\n3. ceil(2.5) is "<<ceil(2.5);
   cout<<"\n4. floor(2.6) is "<<floor(2.6);
   cout<<"\n5. ceil(2.1) is "<<ceil(2.1);
   cout<<"\n6. floor(2.1) is "<<floor(2.1);

   getch();
   return 0;
}
i didn't say that, i said the closest integer of 2.6 is 3.
NOT the floor of 2.6, wich is 2.

so floor(2.6) will give you 2 (it goes down,floors).

i just gave you a tip on how to round a number to the closest integer.
And the closest integer of 2.6 is 3.
And you can get it by adding 0.5 to that number and flooring that.
so

1
2
3
4
5
6
7
8
9
int main()
{
  float a = 2.6;
  
  cout << "the floor of "           << a << " is: " << floor(a) << endl; 
  cout << "the closest integer of " << a << " is: " << floor(a + 0.5) << endl;

  return 0;
}


the floor of 2.6 is: 2
the closest integer of 2.6 is: 3
Topic archived. No new replies allowed.