Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By whiteflags
  • 1 Post By laserlight

factoring - baffled

This is a discussion on factoring - baffled within the C++ Programming forums, part of the General Programming Boards category; Any ideas why these 2 expressions (as far as I can see = exactly the same), give vastly different results: ...

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

    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
    20,954
    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.
    strokebow likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    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.
    strokebow likes this.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    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
    20,954
    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
    strokebow likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-08-2011, 10: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, 09:44 AM
  5. baffled!
    By The Dog in forum C Programming
    Replies: 3
    Last Post: 07-03-2002, 05:58 PM

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