Unexpected output

This is a discussion on Unexpected output within the C Programming forums, part of the General Programming Boards category; The following code when executed, outputs 1.234567880630, when it is supposed to output 1.234567890000.... Code: int main() { float x=1.234567890000; ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    Unexpected output

    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);
    }

  2. #2
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    you are declaring x as a float and printing it as a double

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You should also know that floating point math in most PC based computers is imprecise.

    Click on THIS and read it.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    Quote Originally Posted by joybanerjee39 View Post
    you are declaring x as a float and printing it as a double
    Question 15.2

  6. #6
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    Quote Originally Posted by dmh2000 View Post
    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

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joybanerjee39 View Post
    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
    Did you actually read any of the links we provided?

  8. #8
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    Quote Originally Posted by CommonTater View Post
    Did you actually read any of the links we provided?
    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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joybanerjee39 View Post
    i think u provided it to the person(juice) who started the thread.
    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...
    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; }
    This should count up 1.0 1.1 1.2 etc to 10.9 ...
    And here is the output...

    Name:  FPErrors.PNG
Views: 84
Size:  43.2 KB

    And yes, that's totally normal when using floating point math.
    Last edited by CommonTater; 11-18-2011 at 08:44 AM.

  10. #10
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    Quote Originally Posted by CommonTater View Post
    That doesn't mean you can't read it too.
    obviously that doesn't mean that i can't read it. but have u read the rest portion of my reply? i think i explained there that, i said what i saw.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joybanerjee39 View Post
    obviously that doesn't mean that i can't read it. but have u read the rest portion of my reply? i think i explained there that, i said what i saw.
    Take another look at message #9...

  12. #12
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    Quote Originally Posted by CommonTater View Post
    Take another look at message #9...
    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 ?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joybanerjee39 View Post
    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...

  14. #14
    Registered User joybanerjee39's Avatar
    Join Date
    Oct 2011
    Location
    kolkata
    Posts
    107
    Quote Originally Posted by CommonTater View Post
    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...
    thnx a lot for ur reply
    i will keep the concept about floating point discrepancies in mind. and will definitely read the article.
    but you didn't answer that whether it is correct define a variable as a float and then print it using %lf ?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joybanerjee39 View Post
    thnx a lot for ur reply
    i will keep the concept about floating point discrepancies in mind. and will definitely read the article.
    but you didn't answer that whether it is correct define a variable as a float and then print it using %lf ?
    I believe that may be compiler/library dependent...

    In Pelles, it doesn't seem to matter and the result is the same with %lf as with %f...
    Can't speak for other compilers, though.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unexpected Output, modal window!
    By gaurav_13191 in forum Windows Programming
    Replies: 13
    Last Post: 06-29-2011, 04:47 PM
  2. Unexpected Output in structure
    By bhagwat_maimt in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2006, 10:50 PM
  3. unexpected output
    By crash88 in forum C Programming
    Replies: 2
    Last Post: 05-16-2006, 10:03 PM
  4. unexpected ( unwanted ) output in a file
    By Rpog in forum C Programming
    Replies: 2
    Last Post: 04-15-2004, 08:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21