Prepare for your Next Interview
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 ...
|
|||
|
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; } |
| The Following 5 Users Say Thank You to swetha_joein For This Useful Post: | ||
| Sponsored Links |
|
|||
|
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. |
|
|||
|
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. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| 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 |