Thread: Printf Trouble

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    42

    Printf Trouble

    I am having trouble getting printf to print out my values correctly. In my program (calculator) I need to print out a load of numerical variables. Now, currently I use the %.*g printf flag, as %.*f is quite happy to 'pad' the value out with many 0's if it does not have '*' decimal places. %.*g does not do this, but it does like scientific notation. Why is this a problem? Well take a look at this:
    Code:
    printf("%.*g", 16, tan(180 * 3.14159265358979323846 / 180)); //16 is the precision of a 64 bit double
    Now, it gives me:
    -1.224646799147353e-16
    Which is as good as 0 (the correct answer), however because (please correct me if I wrong) printf is printing out more digits than what a 64 bit double is accurate to you end up with a load of crap on the end which I really do not want. So, what should I do? I need a printf flag which is like %f but that does not append a load of 0's to the end, or should I just manually check for the input numbers and manually correct the output?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I need a printf flag which is like %f but that does not append a load of 0's to the end
    Use %f with sprintf, then manually remove zeros as you see fit.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. I have some questions :(
    By geekrockergal in forum C Programming
    Replies: 19
    Last Post: 02-01-2009, 09:44 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 PM
  5. Whats Wrong Whith This!?
    By SmokingMonkey in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2003, 09:42 PM