C++

Asked • 03/27/19

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

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 overflowing its type). While `A*B` could cause overflow, at same time expression `A*B - C*D` can be really small. How can I compute it correctly?For example: `MAX * MAX - (MAX - 1) * (MAX + 1) == 1`, where `MAX = LLONG_MAX - n` and n - some natural number.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.