Thread: percision display

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    percision display

    hey my question is i have a program that computers the power and the source code is like
    this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int exp,c;
        double pow=1,bse;
        scanf("%lf %d",&bse,&exp);
        for(c=0;c<exp;c++)
             pow*=bse;
        printf("%f ",pow);
        return 0;
    }
    Input is : 95 12
    Output is: 540360087662636990000000.000000

    My question is that the output is not really accurate because when i type it on the
    calculator the output is this :540360087668636962890685 could someone explain to me
    why this happens?? and if how could i program correctly so that i could came out an
    accurate output.. Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Doubles compress a very large dynamic range into a small number of bits.
    To do this, they only store the most significant part of the number.
    In the case of doubles, this amounts to about 15 decimal digits.

    If you need all the digits to be stored accurately, you typically need a library like GMP
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    umm there's other alternative solution than having that library??

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Yes, you make your own implementation of the same.

    Do you know how to do +,-,*,/ on paper?
    Take what you would do on paper, and declare large char arrays to store your numbers in.
    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
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    ohh thanks a lot salem for the hint....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  2. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 08:39 AM
  3. Help needed Please
    By jereland in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 05:30 AM
  4. about scan 7-segment display via parallel port
    By mobdawg in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 06:11 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM