Geeks Talk

Prepare for your Next Interview




Program output

This is a discussion on Program output within the C and C++ forums, part of the Software Development category; What do you think would be the output of the following program and why? (If you are about to say "f is 1.0", I would say check ...


Go Back   Geeks Talk > Software Development > C and C++

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-16-2008
Junior Member
 
Join Date: Dec 2007
Location: India
Posts: 13
Thanks: 2
Thanked 7 Times in 3 Posts
swetha_joein is on a distinguished road
Program output

What do you think would be the output of the following program and why? (If you are about to say "f is 1.0", I would say check it out again)

#include

int main()
{
float f=0.0f;
int i;

for(i=0;i<10;i++)
f = f + 0.1f;

if(f == 1.0f)
printf("f is 1.0 n");
else
printf("f is NOT 1.0n");

return 0;
}
Reply With Quote
The Following 5 Users Say Thank You to swetha_joein For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 02-17-2008
Junior Member
 
Join Date: Feb 2008
Location: India
Posts: 3
Thanks: 1
Thanked 3 Times in 1 Post
iammilind is on a distinguished road
Re: Program output

Ouput is :

f is NOT 1.0n

This is because floating point numbers doesn't have accuracy in certain cases (unlike 'double'). The interpretation inside the machine will be different than what we see.Thus, even though we see the value as 1.0f, due to different storage interpretation, it will fail to compare with actual 1.0f.
The full article and details can be found in wikipedia related to floating point arithmatics.
Reply With Quote
  #3 (permalink)  
Old 02-26-2008
Junior Member
 
Join Date: Feb 2008
Location: india
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
crazyaboutc is on a distinguished road
Re: Program output

yet the result is coming out to be 1.0f but coz of the interpretiion of floating point numbers value gets diferent frm the value tat it ought to be....................
tat's y here result won't be 1.0f but somewat different frm tat.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Knowledge about Program ID in Cobol program RyanJames MainFrame 2 02-14-2008 09:32 AM
First output value is getting overwritten by second output Geek_Guest QTP 2 01-05-2008 04:40 AM
What is a output?. How will Come?. Golda C and C++ 19 11-27-2007 06:05 AM
what will be the output? mahesh9920 C and C++ 8 10-31-2007 05:30 PM
Output Value bharathi_ark Testing Issues 5 12-27-2006 07:13 AM


All times are GMT -4. The time now is 06:22 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved