# factoring - baffled

• 08-16-2012
strokebow
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
• 08-16-2012
laserlight
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.
• 08-16-2012
whiteflags
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.
• 08-16-2012
strokebow
All doubles.

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

Code:

`static_cast<double>(1/6)`
?
• 08-16-2012
laserlight
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
• 08-16-2012
strokebow
Thanks both for the help!