Ask Experts Questions for FREE Help !
Ask
    muziti's Avatar
    muziti Posts: 5, Reputation: 1
    New Member
     
    #1

    Nov 20, 2007, 11:22 AM
    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.
    red_cartoon's Avatar
    red_cartoon Posts: 52, Reputation: 6
    Junior Member
     
    #2

    Nov 20, 2007, 11:50 AM
    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.
    asterisk_man's Avatar
    asterisk_man Posts: 476, Reputation: 32
    Full Member
     
    #3

    Nov 21, 2007, 06:39 AM
    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.
    anuragplanet's Avatar
    anuragplanet Posts: 2, Reputation: 1
    New Member
     
    #4

    Dec 18, 2007, 09:37 AM
    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

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Holy Crap, I'm going to be a Dad. [ 5 Answers ]

I'm a recently separated (a few months), newly (found out Monday) frightened dad to be. I've never been so scared in my life because the person carrying my child is not my soon to be ex-wife. It's not that the person who is carrying my child isn't the most amazing woman I've ever met... it's that...

Hormonal moment leads to haircut. Crap. [ 3 Answers ]

A couple weeks ago I was sitting at home and out of no where, decided get my hair cut -- short. At this point I had been growing it for about 3 years so it was long (about half way down my back) and beautiful. I called the salon, they had an opening, and I asked my stylist for something short......

Crap telephone conversations [ 4 Answers ]

Now this may seem daft, my boyfriend and I have been together for 3 years, get on very well when we are together, I see him 5 nights a week, on the nights I don't see him he calls me but we hardly ever have anything to say, I struggle to get conversation out of him as he has never been one for...

Okay, so I tore the crap out of my basement floor... [ 5 Answers ]

... to rough-in for the bathroom I'm putting in. my questions are this: 1) Do I have to use Cast or can I use PVC for the stuff I'm putting in? 2) With the layout and location of things, it's going to end up where my bathroom sink and wall will be directly over the main line in the floor. ...


View more questions Search