Thread: factoring - baffled

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    factoring - baffled

    Any ideas why these 2 expressions (as far as I can see = exactly the same), give vastly different results:

    Code:
    Y = Y + (T/6)*(2*V + 2*P + 2*P + T*S);
    I make the following change (introduce T in to the brackets...
    Code:
    Y = Y + (1/6)*(T*2*V + T*2*P + T*2*P + T*T*S);
    Using the second line of code instead of the first gives a vastly different answer?

    any ideas as to why? I am completely clueless

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What are the types involved? Integer division might come into play. What are the ranges of the values involved? Maybe the result of the intermediate computations exceed the maximum value for the given type.
    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

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well 1/6 is probably zero since you're probably using integer division. T/6 may or may not be the same thing, it depends on the type of T.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    All doubles.

    Oh I see. So what is the best way to perform (1/6):

    Code:
    static_cast<double>(1/6)
    ?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by strokebow
    So what is the best way to perform (1/6)
    Just write (1.0 / 6.0)

    Quote Originally Posted by strokebow
    Code:
    static_cast<double>(1/6)
    ?
    That's another way of writing 0.0
    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

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks both for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-08-2011, 11:27 AM
  2. Baffled By Question
    By ASCII in forum C Programming
    Replies: 9
    Last Post: 08-28-2011, 04:41 PM
  3. Factoring
    By ugsquish in forum C++ Programming
    Replies: 1
    Last Post: 09-21-2005, 09:57 PM
  4. Newbie baffled
    By Steve MacD in forum C Programming
    Replies: 8
    Last Post: 02-17-2005, 10:44 AM
  5. baffled!
    By The Dog in forum C Programming
    Replies: 3
    Last Post: 07-03-2002, 05:58 PM