Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   C (https://www.askmehelpdesk.com/forumdisplay.php?f=438)
-   -   Integer (https://www.askmehelpdesk.com/showthread.php?t=154177)

  • Nov 20, 2007, 11:22 AM
    muziti
    integer
    why is this happening?
    including all necessary header files ,math.h and stdio.h ,
    int main()
    {

    int I=2;
    int y=pow(10,I);
    printf("%d",y);
    return 0;
    }


    this program prints 99.
    can u help;
    thank u.
  • Nov 20, 2007, 11:50 AM
    red_cartoon
    a text book example of 'loss of precision due to conversion from floating type to integer type'

    pow( x, y) returns a double. When you try to put that inside an int, you experience a roundoff error.
  • Nov 21, 2007, 06:39 AM
    asterisk_man
    pow is defined as:
    double pow(double x, double y);
    so the two integers you supply, 10 and I, are cast into doubles, the function is evaluated and a double is returned. When that double is then cast into an int so that it can be stored in y some precision is lost due to the way that floating point numbers are represented.

    hope this helps.
  • Dec 18, 2007, 09:37 AM
    anuragplanet
    Quote:

    Originally Posted by muziti
    why is this happening?
    including all necessary header files ,math.h and stdio.h ,
    int main()
    {

    int i=2;
    int y=pow(10,i);
    printf("%d",y);
    return 0;
    }


    this program prints 99.
    can u help;
    thank u.

    I don't know what the problem is because my compiler isn't showing any problem and printing 100 as the answer
    May be your math.h has been tampered
    If you get any more such problems, try loading turbo c again and then use it

  • All times are GMT -7. The time now is 05:31 AM.