c++ - Do integer divisions to integers need a cast to float or double to be more precise? -


If I split the integer a and b for the output Want to do one third integer c , do I need to put the split in float or repeat it before dividing to ensure the most accurate?

  int a, b; Int c1 = a / b; // without CR2 (float) A / B;  

Is c1 more, less or less accurate as c2 ?

P>

No, you have to restrict yourself from casting.

With big values, e.g. 1000000000L / 1000000001 L is clearly 0, but when float is inserted, it returns 1.0, because (float) a == (float) a + 1

  main () {int a = 1000000000; Printf ("% d \ n% d", a / (a ​​+ 1), (int) ((float) a / (a ​​+ 1))); }  

Output:

  0 1  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -