Thread: C programming 8 decimal places

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    C programming 8 decimal places

    Hello all, this is my first ever forum post so apologies in advance for any mistakes I make.

    I was asked to create a program which would calculate a circle's circumference and area given the user enters the radius. One specification is to declare pi to 8 decimal places. I understand this cannot be achieved using the float data type but I cannot get double to work, so have used float as a temporary solution. My code is:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int r;
        float pi = 3.14159265;
        printf("\nEnter the cirle's radius to calculate its area and circumfrence\n\n");
        scanf("%i", &r);
        printf("\nThe circle with a radius of %i has an area of %f and a circumference of %f\n\n", r, pi*r*r, 2*pi*r);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You didn't really make any mistakes, but your post is lacking information for us to help you.

    Do you have an actual question? What specifically didn't work about the double? Did it compile? If not, tell us what compiler you're using, and copy-paste the exact error messages with line number. Did it not run or crash? How far did it get before it stopped? Did it run but produce incorrect output? What input did you give, what output did you expect, and what output did you actually get?

    Also, why is the radius an int and not a float or double?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    All you need to do in that code is change the keyword "float"
    Code:
    float pi = 3.14159265;
    to "double", which will turn that line into
    Code:
    double pi = 3.14159265;

    Lemme guess, you tried something like
    Code:
        double float pi = 3.14159265;
    If you feel silly, learn from it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    3
    Quote Originally Posted by anduril462 View Post
    You didn't really make any mistakes, but your post is lacking information for us to help you.

    Do you have an actual question? What specifically didn't work about the double? Did it compile? If not, tell us what compiler you're using, and copy-paste the exact error messages with line number. Did it not run or crash? How far did it get before it stopped? Did it run but produce incorrect output? What input did you give, what output did you expect, and what output did you actually get?

    Also, why is the radius an int and not a float or double?
    To clarify - when I use the double function instead, the value for pi is still printed at 6 decimal places. The area and circumference values change however. Does this mean when I use double, the calculation uses pi to 8 decimal places but the printf function limits the visual representation of pi to 6 decimal places?

    (From the brief I assumed the radius value entered would just be an integer - I know I could easily change it to float to enable input of decimal numbers.)

    C programming 8 decimal places-screenshot-2012-10-12-19-40-14-jpgC programming 8 decimal places-screenshot-2012-10-12-19-54-08-jpg

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    3

    Compiler

    I use the gcc compile command in the Ubuntu terminal.

    (unrelated) Also how can you quickly view your created threads?

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try
    Code:
    printf("%.9f", pi);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should note that floats have only 6 digits of precision anyway, so printing 9 digits won't make it any more accurate.

    A double can hold about 15 digits of precision, so you can print something more meaningful with a larger format precision.
    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.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Salem View Post
    You should note that floats have only 6 digits of precision anyway, so printing 9 digits won't make it any more accurate.

    A double can hold about 15 digits of precision, so you can print something more meaningful with a larger format precision.
    I assumed (never a good idea) that he'd switched to double, since grumpy already mentioned that. But now that I look at the picture of his code, I see he's still using a float.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 09:04 AM
  2. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  3. Float Decimal Places
    By TyrioN in forum C Programming
    Replies: 1
    Last Post: 08-15-2004, 02:30 AM
  4. Getting Two Decimal Places
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2003, 08:34 PM
  5. decimal places
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 07:08 PM

Tags for this Thread