writing mathematical operations in C

This is a discussion on writing mathematical operations in C within the C Programming forums, part of the General Programming Boards category; Hi guys I started C programming today, so this is a super simple question... When writing mathematical operations, how do ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    Question writing mathematical operations in C

    Hi guys

    I started C programming today, so this is a super simple question...

    When writing mathematical operations, how do you use parenthesis () to make sure the operations are made in the right order?

    The equation I want to write would be, in mathematical language,

    x1 = x0 - (x0 * x0 * x0 - 3) / (3 * x * x)

    where x0 is the initial value of x and x1 is the new value we are after.

    So far in C, I am writing

    x = x - (x*x*x - 3) / (3*x*x);

    but this does not seem to give the proper result (e.g., if x0 = 1, I shoulg get x1 = 1.666667).

    Why?

    Thanks

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    OK

    You were right,

    I had written %d instead of %f in the printf call

    Thanks

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161

    Re: writing mathematical operations in C

    Originally posted by p_s_ross

    x = x - (x*x*x - 3) / (3*x*x);
    Based on your equation and the fact you seem to have gotten a correct answer, you were lucky.

    The reason you use parens it to make sure your equation is processed correctly. Various ways your equations could be parsed:
    x = x - (x*x*(x - 3)) / (3*x*x);
    x = x - (x*((x*x) - 3)) / (3*x*x);
    x = (x - (x*x*(x - 3))) / (3*x*x);
    each of which will give different results

    You happen to want
    x = x - (((x*x*x) - 3)) / (3*x*x));
    which without parens C will process correctly.

    Look into operator hierarchy and use more parens to force the equation you want. It also makes the equation easier to figure out 3 months from now.
    Last edited by WaltP; 06-13-2003 at 08:29 AM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimize file writing
    By Opariti in forum Windows Programming
    Replies: 7
    Last Post: 10-23-2008, 01:32 PM
  2. doing floating operations using integer operations
    By ammalik in forum C Programming
    Replies: 10
    Last Post: 08-15-2006, 04:30 AM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

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