Somewhat easy problem

This is a discussion on Somewhat easy problem within the C Programming forums, part of the General Programming Boards category; Hey, I am having a small problem with a program I am doing. What I am trying to do is ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Somewhat easy problem

    Hey, I am having a small problem with a program I am doing. What I am trying to do is multiply a series of fractions until they equal 3.1416 at least. Now I have my fractions correct, but for some reason it won't divide them into variable C, then multiply it by the place holder variable d until it reaches 3.1416. C comes up as 0 each time I try. Ive tried lots of different ways but for some reason it always returns 0 or 1. Any ideas or suggestions?

    Code:
    #include <stdio.h>
    
    void main(void)
    {	
    	int i; // iteration loop
    	int topFrac, botFrac, d; //topFrac = top fraction, botFrac = bottom fraction, 
    	// c = their result, d = temp var to hold c for next loop.
    
    	double pi; //value the product will be assigned
    	float c;
    	d = 1; // carry over variable
    	botFrac = 1;
    	c = 1;
    	topFrac = 2;
    
    	for(i=1; i<=200; i++)
    	{
    		
    		if(i%2==0)
    		{
    			topFrac = topFrac + 2;
    			
    		}
    		else
    		{
    			botFrac = botFrac + 2;
    			
    		}
    		d = c;
    		c = topFrac / botFrac;
    		printf("%d / %d = %f\n", topFrac, botFrac, c);
    				
    	}
    
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    int topFrac, botFrac, d;
    Perhaps you should declare them as double. Its only then you can expect result as a double. If you divide int/int u will get int. If you do int/double = get double. If you fo 10 / 100.0 you will get double. Thats how it works. Either one of your operand should be double or float.

    And make sure you return an int from your main. It should be int main not void main.

    Code:
    int main
    {
        return 0;
    }
    See FAQ for more details.

    ssharish

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thanks buddy! Although it still doesn't work, I did get some results at least. I think I am just going to scrap it, I think I went the wrong way with it. I hate PI lol. Thanks again and good advice. Ill remember that!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I think in this case, since topFrac and botFrac are always whole numbers,it would probably be better to make them ints (or unsigned ints, since they're also nonnegative) and use a cast when necessary to force floating-point division - for example,
    Code:
    c = (float)topFrac / botFrac;
    Standard 32-bit IEEE-754 floats only have 23 binary bits of precision == 6 or 7 decimal digits. If you make the variables floats, they will start to be imprecise when their values get about that big, which is much smaller than the maximum value of a 32-bit signed or unsigned int (about 2 billion and 4 billion, resp.). On the other hand, if you make them 64-bit IEEE-754 doubles, they have 52 binary bits of precision == about 15 or 16 decimal digits, so they can get bigger than if they were 32-bit ints, but doing simple integer increments such as adding 2 are more expensive than the same operation on ints.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    Standard 32-bit IEEE-754 floats only have 23 binary bits of precision..
    24 bits if you count the hidden bit

    You'll begin to have some problems representing integer with IEEE32 format once you reach 2^24 which is 16 777 216...

    For the IEEE64 format, you'll have the same problems when you'll reach 2^54 which is 18 014 398 509 481 984...


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  2. Relatively easy math problem...
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-18-2005, 08:48 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. Math Problem....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-11-2002, 03:37 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21