IndiaBIX.com
Arithmetic Aptitude Data Interpretation
Logical Reasoning Verbal Reasoning Non Verbal Reasoning
General Knowledge
Sudoku Number puzzles Missing letters puzzles Logical puzzles Playing cards puzzles Clock puzzles
C Programming C# Programming Java Programming
Networking Database Questions Computer Science Basic Electronics Digital Electronics Electronic Devices Circuit Simulation Electrical Enigneering Engineering Mechanics Technical Drawing
Placement Papers Group Disucssion HR Interview Technical Interview Body Language
Aptitude Test Verbal Ability Test Verbal Reasoning Test Logical Reasoning Test C Programming Test Java Programming Test Data Interpretation Test General Knowledge Test
Data Structures Operating Systems Networking DATABASE Database Basics SQL Server Basics SQL Server Advanced SQL Server 2008 JAVA Core Java Java Basics Advanced Java UNIX Unix File Management Unix Memory Management Unix Process Managemnt C Interview Questions The C Language Basics .NET Interview Questions .NET Framework ADO.NET ASP.NET Software Testing

C Programming - Floating Point Issues - Discussion

@ : Home > C Programming > Floating Point Issues > General Questions - Discussion

Read more:

"Time and tide wait for none."
- Alexander Pope
6. 

We want to round off x, a float, to an int value, The correct way to do is

[A]. y = (int)(x + 0.5)[B]. y = int(x + 0.5)
[C]. y = (int)x + 0.5[D]. y = (int)((int)x + 0.5)

Answer: Option B

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.


Sravan said: (Mon, Aug 9, 2010 08:48:17 AM)    
 
Is any difference between int and (int) ?

Ashish Gupta said: (Mon, Sep 20, 2010 06:09:09 PM)    
 
We can use both

a) y= int(x+0.5)

b) y= (int)(x+0.5)

Nikhil said: (Mon, Nov 1, 2010 09:14:33 AM)    
 
Why answer is (int)(x) +0. 5?

Vishak said: (Wed, Jan 26, 2011 04:43:39 AM)    
 
@Ashish: i too thought the same why cant we ? is there a proper explanation ?

Well Wisher said: (Thu, Feb 10, 2011 12:55:39 AM)    
 
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.

Vinoth said: (Thu, Mar 17, 2011 12:00:36 PM)    
 
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...

Anuj Kumar said: (Thu, Apr 7, 2011 04:39:21 PM)    
 
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.

Abha said: (Sat, Aug 27, 2011 09:21:17 PM)    
 
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. ?

Rani said: (Fri, Sep 9, 2011 06:45:12 PM)    
 
But option (A)=4
option(B)=3
means both are correct we need only round off not say Floor or ceil

Kanta Patidar said: (Fri, Sep 9, 2011 06:50:12 PM)    
 
option (a)=4
option(c)=3
option(d)=3
it means tino option is right

Vamshi Krishna said: (Fri, Nov 25, 2011 06:52:28 AM)    
 
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: (Thu, Dec 29, 2011 06:34:33 PM)    
 
What is return function can you explain for me?

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

Bhupesh said: (Fri, Jan 20, 2012 12:19:16 AM)    
 
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.

Write your comments here:
Name *:     Email:


© 2008-2011 by IndiaBIX™ Technologies. All Rights Reserved | Copyright | Terms of Use & Privacy Policy

Advertise     Contact us: info@indiabix.com     Follow us on twitter!