Thread: preciseness. lol

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    preciseness. lol

    hey all,
    well I am trying to complete an assignment, and getting real close, but not close enough for my liking .

    please take a look at the attached PDF, and then take a look at my program:

    Code:
    /* prog02assign.c */
    
    #include <stdio.h>
    /*#include <math.h>*/
    
    int main(void)
    {
    	int i;
    	float f;
    	unsigned double p = 3.140000000, d2;
    
    	printf("Please enter a decimal value between 1 and 100: ");
    	scanf("%d", &d2);
    
    	printf("\nVariable\t0 Decimals\t5 Decimals\t9 Decimals\n");
    
    	i = 2 * p;
    
    	printf("i (2*pi)\t%d\t\t%f\t%.9f\n", (int)i, (float)i, (float)i);
    
    	f = 3 * p;
    
    	printf("f (3*pi)\t%d\t\t%f\t%.9f\n", (int)f, f, f);
    
    	d2 = d2 * p;
    
    	printf("d2 (n*pi)\t%d\t\t%f\t%.9f\n\n\n", (int)d2, (float)d2, (float)d2);
    
    	return 0;
    }
    when you enter 35.628 see how close my results are to the screenshot? close, but not close enough IMO. math.h is commented out because my MSVC++ 6.0's math.h does not appear to define a symbolic constant for pi.

    note that I am declaring an unsigned double, which I don't think is right, the compiler is issuing a warning for it. but without d2 being unsigned, I get ugly signed results printed for d2.

    could anyone please help me with making my output more precise here? any help is greatly appreciated, I know this program is ugly, but its the closest I can get to the screenshot.


    thank you in advance!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Pi is not "3.1400000".
    Floating point numbers are not accurate.
    Put two and two together, and get 4.123235434563465345345.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    changing the value of p to something more precise like 3.141592653 (which is as precise as I can get) makes no difference on the output here, I suppose thats what you mean by floating point numbers are not accurate.

    I am just curious as to why when I input 35.628 into my program I get 105 for d2 and in his screenshot he gets 111. thats quite a difference. I'm somewhat of a perfectionist, and this is bugging me, as you see I have the output formatted exactly as his .

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > scanf("%d", &d2);
    I'm surprised you get anything meaningful at all.
    Input a double is "%lf", not "%d"

    Also, all those (float) casts of doubles in printf are pointless, because it just converts them straight back into doubles (it's a variable argument).
    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.

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    thank you very much Salem.

    it is now outputting just as I wanted.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lol need some help :D
    By lolol in forum C Programming
    Replies: 28
    Last Post: 03-28-2008, 07:46 AM
  2. The Recruit - LOL using C
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-25-2003, 05:47 AM
  3. Visual C++ 6.0 Debugs my computer lol
    By RoD in forum Tech Board
    Replies: 9
    Last Post: 03-30-2003, 05:13 PM
  4. lol, funniest acting program EVER -- and i need help with it :(
    By Leeman_s in forum Windows Programming
    Replies: 1
    Last Post: 01-09-2003, 07:27 PM
  5. Lol!
    By no-one in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2002, 12:29 PM