Negative result due to overflow despite using long long int
You either need to declare one of the variables you're multiplying as a long or you need to cast it to long during the multiplication.
Declaring 1 variable as a long:
long A = 200;
int B = 400, C=150, D=210, ...
Declaring all variables as long:
long A = 200, B = 400, C=150, D=210, ...
Casting variable A to long during multiplication:
non_Zero_value_number=(percent*((long) ABC*D))/100;
The problem is that C++ is doing all of the calculations in int before turning the end-result into a long. Which means that you still have to deal with the int overflow once the int value exceeds 2147483647 (see https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits)
Since 10 x 200 x 400 x 150 x 210 = 25200000000, you are exceeding the limits of int by several orders of magnitude
Note: This question is very similar to the one in the link (although for C++ instead of C#):
Multiplying int with long result c#
3 * 1000000000 overflows as an int, but the variable is long long. Why?
Integer constants are, by default int
s.
1000000000
That can fit into an int
. So, this constant gets parsed as an int
. But multiplying it by 3 overflows int.
10000000000
This is too big to an int, so this constant is a long long
, so the resulting multiplication does not overflow.
Solution: explicitly use long long
constants:
long long calcOne = 3 * 100000000LL; // 3e8, essentially
long long calcTwo = 3 * 1000000000LL; // 3e9, essentially
long long calcThree = 3 * 10000000000LL; // 3e10, essentially
long long is 8 bytes, but I get integer overflow?
Because the numbers on the right hand side are of type int
, not long long
, so int
arithmetic is performed in the expression, leading to an overflow.
If you add LL
to one of them, it'll promote them all.
integer overflow in c++
All the literals in (1000000 * 2) * 1000000
are int
types, and the compiler is warning you that this overflows the int
on your platform.
It doesn't matter that you are assigning the result of this expression to a different type.
One solution is to use (2ll * 1000000) * 1000000
which forces the implicit conversion of the other terms.
Long integer overflow in C++
Because here
long long int x = 100000 * 99999;
two ints are multiplied. Try
long long int x = 100000LL * 99999;
C++ long overflowing prematurely
long is not necessarily 64 bits. try 'long long' instead.
why there is a integer overflow in this multiplication?
Add some L
s*. long x = 2L * 1024L * 1024L * 1024L;
(Technically, as long as one literal is of type long
the others will be promoted to long
)
The overflow happens because 2
, etc. is of type int
by default and the overflow happens before the assignment.
See integer literals which explains the different literals.
Related Topics
C-Style Strings as Template Arguments
Opencv Videocapture Lag Due to the Capture Buffer
How to Detect the Number of Physical Processors/Cores on Windows, MAC and Linux
Make Shared_Ptr Not Use Delete
Windows 7 Timing Functions - How to Use Getsystemtimeadjustment Correctly
Getting Mangled Name from Demangled Name
Read Unicode Utf-8 File into Wstring
Is a Moved-From Vector Always Empty
How Do Objects Work in X86 at the Assembly Level
Change the Current Working Directory in C++
Overriding a Base's Overloaded Function in C++
Why Is There Not an Std::Is_Struct Type Trait
Why Is a NaïVe C++ Matrix Multiplication 100 Times Slower Than Blas
Should I Unify Two Similar Kernels with an 'If' Statement, Risking Performance Loss