Thread: sscanf problem - very simple im sure!!

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    sscanf problem - very simple im sure!!

    Hi, here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char* argv[])
    
    {
    	double a,valid_input;
    	
    	valid_input=sscanf(argv[1], "%lg", &a);
    	printf("a= %.14lg\n", a);
    	return(0);
    }
    When I put: assgn1A 1.69, I expect the printf to give me a=1.69000000000000 but it gives me 1.689999991233153 or something similar!

    What am I doing wrong?

    Thanks, Scott

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You're doing nothing wrong except assuming that floating point values can precisely represent any arbitrary number.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Floating point variables are incapable of representing some values precisely.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Thanks for replies guys

    So how can I fix this so that if i put in a number that is say up to 5 decimal places, it will print that number exactly?

    Thanks, Scott

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Just curious, what compiler are you using?

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    gcc?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    printf() handles both floats and doubles with %g (unlike *scanf(), which requires you to tell it whether you're passing a float or a double). So you can just use a format specifier like
    Code:
    %.5g
    in your printf() call.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    hmm okay. I think i've oversimplified my problem

    Here's what I'm trying to do:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc, char* argv[])
    {
    	double a, b, c, disc;
    	double valid_input_1, valid_input_2, valid_input_3;
    	
    	valid_input_1=sscanf(argv[1], "%lg", &a);
    	valid_input_2=sscanf(argv[2], "%lg", &b);
    	valid_input_3=sscanf(argv[3], "%lg", &c);
    	
    	disc=(b*b-4*a*c);
    	
    	printf("a equals %.5lg\n",a);
    	printf("b equals %.5lg\n",b);
    	printf("c equals %.5lg\n",c);
    	printf("Disciminant equals %.5lg\n",disc);
    	printf("b*b=%.5lg\n",(b*b));
    	printf("-4ac=%.5lg\n", (-4*a*c));
    	
    }
    if i put a=0.25, b=1.69, and c=2.8561 (1.69 squared) - i get this out:

    Code:
    a equals 0.25
    b equals 1.69
    c equals 2.8561
    Disciminant equals -4.4409e-16
    b*b=2.8561
    -4ac=-2.8561
    Now disc should equal 0 but it doesn't!
    Is it still not possible??

    Thanks, Scott

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    0.25 can be exactly represented. 1.69 and 2.8561 can not, although you can get awfully close. Doing math with almost the right numbers gives almost the right answers.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    ah okay, so it's not doable basically. Thanks for reply!

    Scott

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with sscanf
    By march5th in forum C Programming
    Replies: 4
    Last Post: 03-18-2009, 09:36 AM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple sscanf mystery
    By registering in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 11:47 PM