The following code when executed, outputs 1.234567880630, when it is supposed to output 1.234567890000....
Code:int main() { float x=1.234567890000; printf("%.12lf",x); }
The following code when executed, outputs 1.234567880630, when it is supposed to output 1.234567890000....
Code:int main() { float x=1.234567890000; printf("%.12lf",x); }
you are declaring x as a float and printing it as a double
You should also know that floating point math in most PC based computers is imprecise.
Click on THIS and read it.
You're not going to work all the way through all possible floats before reading Goldberg are you?
Floating point discrepancy
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thnx for ur reference.
but, when i compiled the program by defining x as a double and then using %lf in printf the output was correct. whereas, when i defined x as a flaot and used %if in printf as juice had done the output was wrong (as written by juice).
i maybe wrong, but my explanation is that when x is being defined as a float and assigned a value of a 1.234567890000 it was to long to store as a float variable so the compiler striped off the part from 9 onwards. but when it was told to print x as a double it printed till 1.2345678( which was stored) and then printed random vaues. so the output was 1.234567880630
i think u provided it to the person(juice) who started the thread.
as far as myself is concerned i didn't read the link. but i tested the program with all the four options ie.
float x printf ("%.12f",x);
float x printf ("%.12lf",x); (this was used by juice)
double x printf ("%.12f",x);
double x printf ("%.12lf",x);
the correct answer ie. x=1.234567890000 came only when x was defined as a double.
that's what i said in my reply.
and comp.lang.c q15.2 says that we should use %f with double also. so the correct one should be double x printf ("%.12f",x);
i replied the exact thing that i saw while compiling
That doesn't mean you can't read it too.
The point is that floating point math in PCs is inaccurate... You cannot count on exact results due to a "last bit ambiguity" that arrises because not all floating point values can be accurately represented in the underlying binary notation used by computers.
Here... run this little demonstration piece, you'll see what I mean...
This should count up 1.0 1.1 1.2 etc to 10.9 ...Code:// demonstration of floating point inaccuracies #include <math.h> #include <stdio.h> int main (void) { int i; float x = 1.0; for (i = 0; i < 100; i++) { printf("%f\t",x); x += 0.1; } return 0; }
And here is the output...
And yes, that's totally normal when using floating point math.
Last edited by CommonTater; 11-18-2011 at 08:44 AM.
yes, the program runs exactly as you said. and i understand the concept that there will be floating point discrepancies in pc . and that it is completely normal. but, i compiled the same program that you gave by defining x as double it gave exact correct result. my question is that when i am getting correct result by defining x as double why shouldn't i use it? and why is the discrepancy not working then ?
and juice defined x as a float and used %lf in printf , is this correct ?
There is no reason not to use it so long as you're aware of the pitfalls... For example I would never use floating point math for financial transactions...
The same discrepancies exist but due to the higher precision of doubles they not show up in such a trivial example.
I was merely trying to inform the OP of one of the pitfalls in using floating point math...