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 1 of 3.

Abhisek said:   9 years ago
@Neev very well explanation. Thank you.

MrMino said:   10 years ago
The answer should be C:


float x = 4.2;
printf("%f\n", (int)x + 0.5 );

Outputs 4.500000.

Anshul Jain said:   10 years ago
Why option C is wrong? Please explain any one.

Sneha Naik said:   1 decade ago
Here round of of x is asked. So answer should be (int) x+0.5.

If they have asked to round of why then answer will be (int)(x+0.5).

Please correct me if I am wrong.

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

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.

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

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.

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

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.


Post your comments here:

Your comments will be displayed after verification.