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.

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

Vinoth said:   1 decade ago
Sravan int is different from (int).in given program is compiled in C++ compiler.
In C language we use paranthesis to a variable.
eg:y=int (x)
here x is an float variable and y is an integer variable.
In C++ we put paranthesis to data type.
Eg:y=(int) x
eg we take x=5.6
in question asked is rounding int value(y) for given float value(x) so we add 0.5 to that number.Then the final result is
1.in C language
y=int (x+0.5)
2.in C++
y=(int) (x+0.5)
the result is same in both C and C++ compilers y=6.
ALL THE BEST...

Well wisher said:   1 decade ago
Why we should add 0.5 to x :(x+.5)

We can get the answer directly.

y=(int)(x);

I hope this is enough.

In case the round off vale shold not be a min one then we can go for (x+0.5)

Because if u type the above code having x=3.99

Then the roound off value would be 3.

That is not we are looking for.

Anuj Kumar said:   1 decade ago
Why we should add 0.5 to x :(x+.5)

We can get the answer directly.

y=(int)(x);

I hope this is enough.

In case the round off vale shold not be a min one then we can go for (x+0.5)

Because if u type the above code having x=3.99

Then the roound off value would be 3.

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

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???

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.

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.

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.

Abha said:   1 decade ago
According to me ans should be C because type castng is done to variable and not to a constant term. Tell me whether my perception is wrong or right. ?


Post your comments here:

Your comments will be displayed after verification.