By the condition, after entering a, the calculation is made using these two formulas, the answer must match. Where did I make a mistake? Answers do not match.

Formulas

double a = Convert.ToInt32(textBox3.Text); double z = 1 - (1 / 4) * Math.Pow(Math.Sin(2 * a),2) + Math.Cos(2 * a); double z1 = Math.Pow(Math.Cos(a),2) + Math.Pow(Math.Cos(a), 4); textBox1.Text = Convert.ToString(z); textBox2.Text = Convert.ToString(z1); 
  • one
    It is necessary to round ...... or observe the type (1 - (1 / (double)4)..... because the integer division of 1 by 4 will be 0, not 0.25 - Alexey Shimansky
  • Confuses double a = Convert.ToInt32(...); . Are you sure a must be an integer? - Dmitry D.
  • @DmitryD. No, I'm not sure, but in the end, thanks to Alexey, the answer coincided. Why are you confused? - Little Hare

2 answers 2

The integer division of 1 by 4 is 0, not 0.25. Accordingly, to obtain the expected result, bring to the desired type:

(1/(double)4)

http://ideone.com/kqEU38

Or, on the advice of @Pavel Mayorov, write immediately with the required type one of the members: (1 / 4.0) or (1.0 / 4)

  • How does (double)(1/4) help? It seems that all the same, an integer 1/4 will be executed first, and only then the result (i.e. zero) will be double to double ? - yeputons
  • @yeputons is me awake) - Alexey Shimansky
  • Why so hard ?! (1 / 4.0) or (1.0 / 4) - Pavel Mayorov
  • @PavelMayorov, see above)) ... added in response .. If you wish, you can edit it). - Alexey Shimansky

Compare 2 options:

1 :

 double a = 22.22; double z = 1 - (1 / 4) * pow(sin(2 * a),2) + cos(2 * a); double z1 = pow(cos(a),2) + pow(cos(a), 4); printf( "%f, %f\n", z,z1 ); 

Conclusion:

 1.897070, 1.848254 

2 :

 double a = 22.22; double z = 1 - 0.25 * pow(sin(2 * a),2) + cos(2 * a); double z1 = pow(cos(a),2) + pow(cos(a), 4); printf( "%f, %f\n", z,z1 ); 

Conclusion:

 1.848254, 1.848254 

Doesn't it lead to any thoughts? And if so?

 printf( "%f, %f\n", (1.0/4), (double)(1/4) );