Thread: How to fix a simple problem with division of numbers

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    20

    How to fix a simple problem with division of numbers

    Dear all,

    I am writing a simple program that performs some statistical calculations. The code looks like:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int x, n;
    float f_A;
    float chi2 = 0;
    int genotype[4];
    float expected[4];
    
    
    int main(void)
    {
    
    // code here
    
    n = genotype[1]+genotype[2]+genotype[3];
    f_A = ((2*genotype[1])+(genotype[2]))/(n*2);
    
    // code here
    
    return 0;
    }
    This code doesn't work well, because the variable f_A is not computed. In other words, the code fails to calculate correctly f_A.

    If I, nevertheless, replace the formula

    f_A = ((2*genotype[1])+(genotype[2]))/(n*2);


    to

    f_A = ((2*genotype[1])+(genotype[2]));
    f_A = f_A/(n*2);

    the code gives correct results.

    Am I missing some very basic information about C programming?

    Thank you for any tips.

    cheers!

    Tiago

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    22
    You are performing integer division here:

    f_A = ((2*genotype[1])+(genotype[2]))/(n*2);

    You have to instruct the compiler that it's floating point division, by putting a cast operator
    to one of the operands :

    f_A = (float) ((2*genotype[1])+(genotype[2])) / (n*2);

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    22
    also, depending on the precision of your data,you should really use type double if you get incorrect answers
    Last edited by dnj23; 08-10-2010 at 05:55 PM.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    Thank you very much! worked like a charm!

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I would suggest losing several of those brackets, and a multiply by 2.0f would avoid the need for a cast.
    However, why not divide both the numerator and demoninator by 2 and simplify, giving just this, which will also work great:
    Code:
    f_A = (genotype[1] + 0.5f * genotype[2]) / n;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    22
    also make sure if n should = 0,to handle it appropriately.

    I've crashed many of my programs with unforseen division by zero.

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    Should use double.

    Code:
    double x, n;
    double f_A;
    float chi2 = 0;
    double genotype[4];
    double expected[4];
    
    
    int main(void)
    {
    
    // code here
    
    n = genotype[1]+genotype[2]+genotype[3];
    f_A = ((2*genotype[1])+(genotype[2]))/(n*2);
    Otherwise with all this integer division you are very dependent on the order of multiplication, division.

    The most common mistake is A*B/C . It is assumed that A*B comes before division by C - not guaranteed with Microsoft!!!

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    90
    Sorry - forgot that n*2 should be n* (double)2!!!!

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by johnggold
    The most common mistake is A*B/C . It is assumed that A*B comes before division by C - not guaranteed with Microsoft!!!
    Are you sure? These operators have the same precedence and are left-associative. This implies that A*B/C is equivalent to (A*B)/C. If a Microsoft compiler differs, then it is a Microsoft compiler bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by johnggold View Post
    Sorry - forgot that n*2 should be n* (double)2!!!!
    Wouldn't n*2.0 be easier? (Which is part of iMalc's point.)

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    The most common mistake is A*B/C . It is assumed that A*B comes before division by C - not guaranteed with Microsoft!!!
    Why should it matter in this case?

    Code:
    A*(B/C)==(A*B)/C

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KBriggs
    Why should it matter in this case?
    Other than possible differences with respect to underflow and overflow, it does not matter when we are talking about floating point arithmetic. However, johnggold wrote with respect to integer arithmetic, in which case it does matter since integer division could change the result, e.g., (3*3)/2 == 4, but 3*(3/2) == 3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM
  2. Simple Gets() problem
    By akira181 in forum C++ Programming
    Replies: 17
    Last Post: 05-25-2006, 03:28 AM
  3. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  4. problem with random numbers
    By xxwerdxx in forum C Programming
    Replies: 2
    Last Post: 11-24-2005, 08:56 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM