0
$\begingroup$

Is the following equation always true?

x / 3 + y /3 + z / 3 = (x + y + z) / 3

I hope this is not too simple of a question. Every example I can think of, this equation is true. However, in a program I am writing, it appears that the left side of it works but not the right side. So I am trying to figure out if I just got lucky or I am out of my mind.

$\endgroup$
  • 1
    $\begingroup$ Your equation is correct. $\endgroup$ – N. F. Taussig Oct 4 '14 at 22:27
  • 1
    $\begingroup$ It's definitely true by the laws of adding fractions. You're probably writing your program slightly incorrectly. Can you post your code? $\endgroup$ – Ian Coley Oct 4 '14 at 22:27
  • 1
    $\begingroup$ check your program again.. Does it return only integer numbers? Maybe that's your problem $\endgroup$ – Exodd Oct 4 '14 at 22:28
  • $\begingroup$ The code is rather elaborate, but I got different results from float degreesWeight = Degrees.x / 3 + Degrees.y / 3 + Degrees.z / 3; versus float degreesWeight = (Degrees.x + Degrees.y + Degrees.z) / 3; I must have missed something somewhere, thank you $\endgroup$ – Evorlor Oct 4 '14 at 22:28
  • $\begingroup$ So I tried the other way again, and got the same results as the first way. I must have missed a typo or something. Sorry to take up your time, but thank you! $\endgroup$ – Evorlor Oct 4 '14 at 22:34
2
$\begingroup$

As mathematical equation of real numbers, it is always correct, due to the distributive law: $$x/3 + y/3 + z/3 = \tfrac13 x+\tfrac13 y+\tfrac13 z = \tfrac13(x+y+z) = (x+y+z)/3$$

However note that floating point operations on the computer only approximate the operations on real numbers; especially a division by three will in general not be exact on a floating point number. It may happen that rounding errors will work out OK for one of the expressions, but not for the other one, or it may happen that one expression overflows (which just means, an intermediate result is too large to be represented in the floating point format of the computer), while the other one doesn't.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.