I have implemented a program in C++ and it showed a very strange bug.
First of all, if I assigned my variable a like this: long long a = 1e9 + 10 and then print the value of a, it ran correctly. But if I set a to 1e18 + 10 and then print the value of a, it showed that a equals 10^18 only. Can anyone help me with this? I tried a lot but I can't understand why. Thanks.
This is my code:
#include <iostream>
using namespace std;
int main() {
long long a = 1e9 + 10;
cout << a << endl;
a = 1e18 + 10;
cout << a << endl;
return 0;
}
1e18 is a value having type double. The presicion of type double is typically around 15 decimal digits, so adding 10 to 1e18 may not change the value of double.
You can add a cast to long long before addition to eliminate the issue in this case, but generally you should avoid using floating-point numbers to deal with integers.
#include <iostream>
int main(void) {
long long value = static_cast<long long>(1e18) + 10;
std::cout << value << '\n';
return 0;
}
Related
I am adding numbers from 1 to n in C++. I have used both the iteration method and mathematical formula. The code works fine for up to 9 digits.
But when I give input a 10 digit number, the formula and iteration methods give separate answers.
I have tried to look it up on google but couldn't find any solution for this. My code:
#include <bits/stdc++.h>
using namespace std;
int main(){
unsigned long long i, n, sum = 0, out_put;
cout << "Sum of numbers from 1 to: ";
cin >> n;
/// using mathematical formula
out_put = n*(n+1);
out_put = out_put/2;
cout << " = " << out_put << endl;
/// using iteration
for (i=1; i<=n; i++){
sum = sum+i;
}
cout << " == " << sum << endl;
return 0;
}
How do know which one is the correct one? If I assume the formula can't be wrong then why is the iteration method giving incorrect answer? I have used unsigned long long to prevent overflow but still didn't work.
What you are seeing is overflow happening on your calculations at different points. 9,999,999,999 * 10,000,000,000 is ~9.9e19 while an unsigned long long holds ~1.8e19. So the result wraps around and you get one answer.
Your for loop is also going to overflow but it is going to do so at a different point meaning the answers will diverge from one another since the modulo arithmetic is happening with a smaller number.
Your problem is that n*(n+1) can be too large to store in an unsigned long long, even though the end result (half of that) which you calculate via iteration may still fit.
Assuming your unsigned long long has 64 bits, it can hold integers up to 18446744073709551615. Anything above that will restart from 0.
Edit: As Nathan points out, you can of course have both calculations overflow. The sum would still give the correct result modulo 2^64, but the direct calculation can be off because the division does not generally yield the same result modulo 2^64 after you have wrapped around.
#include <bits/stdc++.h>
using namespace std;
int main(){
unsigned long long i, n, sum = 0, out_put;
cout << "Sum of numbers from 1 to: ";
cin >> n;
/// using mathematical formula
out_put = n*(n+1);
out_put = out_put/2;
cout << " = " << out_put << endl;
/// using iteration
for (i=1; i<=n; i++){
sum = sum+i;
}
cout << " == " << sum << endl;
return 0;
}
I've wrote a small program to convert a char to an int. The program reads in chars of the form '1234' and then outputs the int 1234:
#include <iostream>
using namespace std;
int main(){
cout << "Enter a number with as many digits as you like: ";
char digit_char = cin.get(); // Read in the first char
int number = digit_char - '0';
digit_char = cin.get(); // Read in the next number
while(digit_char != ' '){ // While there is another number
// Shift the number to the left one place, add new number
number = number * 10 + (digit_char - '0');
digit_char = cin.get(); // Read the next number
}
cout << "Number entered: " << number << endl;
return 0;
}
This works fine with small chars, but if I try a big char (length 11 and above) like 12345678901 the program returns the wrong result, -539222987.
What's going on?
12345678901 in binary is 34 bits. As a result, you overflowed the integer value and set the sign bit.
Type int is not wide enough to store such big numbers. Try to use unsigned long long int instead of the type int.
You can check what maximum number can be represented in the given integer type. For example
#include <iostream>
#include <limits>
int main()
{
std::cout << std::numeric_limits<unsigned long long int>::max() << std::endl;
}
In C you can use constant ULLONG_MAX defined in header <limits.h>
Instead of using int, try to use unsigned long long int for your variable number.
That should solve your problem.
Overflowed integer. Use unsigned long long int.
I'm having a problem with the following simple code, I don't know why the output will become negative... The program is supposed to calculate the sum of all odd and five-digit numbers like 10001, 10003, 10005, etc.
#include <iostream>
using namespace std;
int main()
{
int num, sum = 0;
for (num = 10001 ; num <= 99999 ; num+=2){
sum += num;
}
cout << num << " " << sum;
return 0;
}
It means that there is an overflow of type int. That is this type can not represent the sum. I advice to declare variable sum like
long long int sum = 0;
After that you can compare the result with the maximum value stored in type int. For example
#include <limits>
//...
std::cout << std::numeric_limits<int>::max() << " " << sum << std::endl;;
Your int will likely overflow. Switch it to long
int num = 0;
long long sum = 0L;
Assuming you have a 4 byte int, the maximum value will be 2^31 - 1 == 2147483647. See this example
Your sum will come out to 2475000000 which will overflow.
I am working in a project and in a certain time I have this problem. I have two very large numbers and I want to divide them and get an integer/long long integer. This is what's happening:
Code
#include <iostream>
using namespace std;
int main(){
long long n,m;
cin >> n >> m;
cout << n/m << endl;
}
The inputs can be numbers until 100,000,000,000,000,000 so the division is performing wrong.
Output
#1 n: 76543210987654321 m: 7654321
#2 76543210987654321/7654321 = 1410312449
The right answer is 10,000,000,130 so I am wondering what is happening...
The correct result is neither 1410312449 nor 10000000130. It is equal to 10000000129
At least it is what the GCC shows at www.ideone.com. You can try it yourself.
#include <iostream>
int main()
{
long long n = 76543210987654321;
long long m = 7654321;
std::cout << n / m << std::endl;
return 0;
}
It seems that you place the result of the operation in an object of type int. Consider the following code
#include <iostream>
int main()
{
long long n = 76543210987654321;
long long m = 7654321;
int x;
x = n / m;
std::cout << n / m << std::endl;
std::cout << x << std::endl;
return 0;
}
The output is
10000000129
1410065537
It seems, that the result of division is truncated to 32-bit value (int).
Try to use explicit cast to long long via static_cast.
Also, it is interesting to know result of sizeof(long long) on your compiler.
This small program is made to figure out the first and second digits of a 2 digit number. However, when I try using it on the number 99 then it prints 9 and 8, but other 2 digit numbers seem to work fine though. This is probably trivial, but I'm relatively new at programming.
#include <iostream>
using namespace std;
void test(int num) {
float numValue = (num*1.0) / 10;
cout << numValue << endl;
// prints 9.9
int firstDigit = num / 10;
cout << firstDigit << endl;
// prints 9
int secondDigit = (numValue - firstDigit) * 10;
cout << secondDigit << endl;
// prints 8, supposed to be (9.9 - 9) * 10
}
int main()
{
test(99);
return 0;
}
That happens because (numValue - firstDigit) is not exactly 0.9, but rather 0.89999..., because floating-point numbers are not accurate generally. So when you multiply 0.8999... by 10, you get the result of 8.999... However, then you are putting it into an int variable, so it gets trimmed and becomes exactly 8.
You don't really need floating point arithmetic for your exact task though, using integers will be enough.