C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - General Questions (Q.No. 6)
6.
We want to round off x, a float, to an int value, The correct way to do is
y = (int)(x + 0.5)
y = int(x + 0.5)
y = (int)x + 0.5
y = (int)((int)x + 0.5)
Answer: Option
Explanation:

Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number.

y = (int)(x + 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)

Example:


#include <stdio.h>

int main ()
{
  float x = 3.6;
  int y = (int)(x + 0.5);
  printf ("Result = %d\n", y );
  return 0;
}

Output:
Result = 4.

Discussion:
24 comments Page 2 of 3.

Vamshi krishna said:   1 decade ago
I think here is the question to round of x and not the y to which it is assigning so i think (int)x+0.5 is appropriate...!!!

If not can anyone give me a proper explanation???

Ganga said:   1 decade ago
What is return function can you explain for me?

Why it z return0 we can also put only return know!

Bhupesh said:   1 decade ago
if x=3.1 then y=(int)(3.1+0.5)

so out put would 4 again, the float 3.1 value should be converted to int 3 only.

Please let me understand this concept.

Ravindra said:   1 decade ago
(int)((int) x+0.5) This is also correct. It also rounds off. Then why only a option. D option also correct. Reply.

Ishika Dhanraj said:   1 decade ago
Question is Round off.

So if x = 7.2 is rounded than we need 7(rounded value) That's why we add. 5 in it int (7.2+.5) =7.

If x = 7.6 than int (7. 6+. 5) = int (8. 1) = 8.

Arkadyutiboral said:   1 decade ago
#include<stdio.h>

int main ()
{
float x = 3.6;
int y = (int)(x + 0.5);
int y1 = (int)(x + 0.5);
int y2 = (int)x + 0.5;
int y3 = (int)((int)x + 0.5);
printf ("Result = %d %d %d %d\n", y,y1,y2,y3 );
return 0;
}

O/P ; Result = 4 4 3 3

Aenk said:   1 decade ago
If we enter x=3.3 it means 3<x<3.5 we get 3 value so in this example is .5 addition is not needed.

Neev said:   1 decade ago
There is a difference between "rounding off a value from float to integer" and "converting a value from float to integer".

Now, the rule of rounding off says

1) If the value after decimal point is greater then .5 then the value of that decimal number will be rounded off to the next integer number For example if we want to round off 5.79, since after the decimal point the value is .79 which is greater than .50, the rounded value of 5.79 will be next integer 6

2) If the value after decimal point is less then .5 then the value of that decimal number will be rounded off to the number itself eliminating the value after decimal point. For example if we want to round off 8.42, since after the decimal point the value is .42 which is lesser than .50, the rounded value of 8.42 will be the number itself elimination the value after the decimal point i.e. 8.

Now, the meaning of "converting from float to integer" is simply keeping the number itself, eliminating the numbers after the decimal point.

In C, in "rounding up a float to integer" involves "rounding up" as well as "converting from float to integer".
For which, first 0.5 is added to number and then converting the number from float to integer.

Suppose in above program we atomize the single step: (int)(x+0.5) into two steps:

1) a = x+0.5
and
2) b = (int)(a)

1) "a = x+0.5" does the work of "rounding up" and then
2) "b = (int)(a) does the work of converting it from float to int

Narendra jain said:   1 decade ago
For rounding of we have to use only int (x) because we have to round of only x and also remember what the syntax of explicit type conversion.

Santosh kumar said:   1 decade ago
Can any one explain me about type casting?


Post your comments here:

Your comments will be displayed after verification.