How to avoid overflow in expr. A * B - C * D

后端 未结 15 1000
萌比男神i
萌比男神i 2021-01-29 18:18

I need to compute an expression which looks like: A*B - C*D, where their types are: signed long long int A, B, C, D; Each number can be really big (not

15条回答
  •  离开以前
    2021-01-29 18:51

    This seems too trivial I guess. But A*B is the one that could overflow.

    You could do the following, without losing precision

    A*B - C*D = A(D+E) - (A+F)D
              = AD + AE - AD - DF
              = AE - DF
                 ^smaller quantities E & F
    
    E = B - D (hence, far smaller than B)
    F = C - A (hence, far smaller than C)
    

    This decomposition can be done further.
    As @Gian pointed out, care might need to be taken during the subtraction operation if the type is unsigned long long.


    For example, with the case you have in the question, it takes just one iteration,

     MAX * MAX - (MAX - 1) * (MAX + 1)
      A     B       C           D
    
    E = B - D = -1
    F = C - A = -1
    
    AE - DF = {MAX * -1} - {(MAX + 1) * -1} = -MAX + MAX + 1 = 1
    

提交回复
热议问题