+ 6
Line 11 appears to overflow when b is 10000000000000000000 , producing 3875820019684212736 when multiplied by 100. A CodePlayground link will make this easier for others if you have more questions.
5th Sep 2017, 4:03 PM
Kirk Schafer
Kirk Schafer - avatar
+ 6
It's when there are no more bits available in the data type to hold the result. unsigned int i; i=0xFFFFFFFF; // max integer, 4 bytes cout << i << endl; // 4294967295 cout << i+1 << endl; // 0 : overflow 0xFFFFFFFF represents 32 1's: 1111.....1111 (each hexadecimal F is 1111) adding another 1 rolls them all over to 0.
5th Sep 2017, 4:23 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
Overflow, is simply when you exceed the range of something. When a tap remains open even when the bucket under it is filled, the water then 'overflows'. Similarly, when you input a larger data item than what a particular type may hold, or when you exceed the memory limit of a register, array or the stack where the OS holds the values of variables, it is referred as overflow. Overflow is usually followed by unexpected results. (Eg - When you tried storing 65536 in a short, but got a -1 instead.)
6th Sep 2017, 8:56 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
int divided by int always results in int , so if you are allocating float to int it will definitely give the wrong result try to use type casting or just use either of a and b as float
5th Sep 2017, 6:10 PM
Himanshu Dubey
Himanshu Dubey - avatar
+ 3
The type long long int in C++ has a range from -(2^32) to (2^32)-1 (signed range), and 0 to (2^64)-1 (unsigned range). You are using the signed range, which reaches its limit after 9223372036854775807. So, use unsigned long long, as you don't need negative inputs in this case. (Since, 10,000,000,000,000,000,000 < 18,446,744,073,709,551,615 ). But if you need even larger inputs, you will have to use a large integer library. This is as long double, which can handle extremely large numbers, is not so good at precision ( max precision = 19 digits.). As you operate on numbers of type long double, the bits gets altered due to a modulus error, and thus, your answer no longer remains precise. So, it is best to either limit your data's range to unsigned long long, or use a 3rd Party large integer library, like Boost Multiprecision Library. ( http://www.boost.org/users/download/ ) ( That has a range of -(2^1024) to (2^1024)-1 ).
6th Sep 2017, 8:50 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
You probably know that data is stored in computer memory as a series of binary bits which can take the value of 1 or 0. Therefore one bit can be used to only differentiate between two different numbers. Adding a bit to a data type doubles the number of numbers you can represent so that if you use 2 bits you get 2*2=4 numbers, 4 bits gives you 2^4=16 numbers, 8 bits -> 2^8=256 numbers, and so forth. Each variety of integer in c++ takes up a certain number of bits in memory which means that there are a finite number of integers you can actually represent with a variable of that type. To simplify this idea imagine that you are using a 4-bit chunk of memory to store an unsigned integer (This is unrealistically small). This could hold the numbers 0 through 15. If you try to store a number bigger than 15 in this variable some of the bits will get dropped (similar to the water in a bucket analogy) and the wrong value will be stored in the variable. I have an example c++ program below which you can use to experiment with this. https://code.sololearn.com/cckPsze9QMiq/#cpp
6th Sep 2017, 4:21 PM
Justin Christensen
Justin Christensen - avatar
+ 3
That will throw an overflow exception. I'd recommend to try and fix those errors as quick as possible because they might lead to bigger issues in the code that are going to spend a lot of your time. There are programs that might help if you need, such as checkmarx and others. Good luck with it.
1st Oct 2017, 8:37 AM
Ben hart
+ 1
You want the BigInteger struct. It's in System.Numerics. I think it also does more than integers as well.
7th Sep 2017, 3:18 AM
Sean Douglas
Sean Douglas - avatar