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

后端 未结 15 1014
萌比男神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:56

    For the sake of completeness, since no one mentioned it, some compilers (e.g. GCC) actually provide you with a 128 bit integer nowadays.

    Thus an easy solution could be:

    (long long)((__int128)A * B - (__int128)C * D)
    

提交回复
热议问题