cast don't work! RRS feed

  • Question

  • Hi,

    If i cut the () , the cast don't work ... Why?

    Don't work

    sum += d * (double)(4/x) ;


    sum += d * (double)4/x ;

    Monday, July 29, 2013 6:47 PM


  • Hi Arash,

    Because when you use (double)(4/x) it firstly does 4/x calculation and if the x is defined as integer the result will be integer too for example: 4/5 will be calculated as 0. But if the x is defined as Double there will be no calculation error.

    When you use (double)4/x system first casts 4 to double and then does the calculation and the calculation will be 4.0/x.

    • Marked as answer by Arash_89 Monday, July 29, 2013 7:56 PM
    Monday, July 29, 2013 7:13 PM