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;
}
@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 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