Related
Been learning C++ in a class, but for one of the exercises I've been getting a [Warning] Integer Overflow in [-Woverflow], what's causing the problem and how do I fix it? (The program is meant to calculate the output of complete matter->energy transformation in Joules, and here it is)
#include <iostream>
using namespace std;
int main()
{
long antimass = 0;
//long amosus = 0;
cout<< "Breakdown, M\n 3-23\n Exersize 5\n";
cout<< "Enter The Mass of your antimatter (in grams)!\n";
cin >> antimass;
//amosus = antimass%1000;
cout<< "output in joules = "<< (antimass%1000*(299792458*299792458))<<endl;
}
Thanks to user17732522 for figuring out what was wrong!
I replaced the pesky Long (line 6) with Double and replaced the % (line 12) with /
and that fixed it!
user17732522's original post:
"It means 299792458*299792458 has a value larger than can be stored in the type of the expression. Given the context you probably should be using floating point here, i.e. use double instead of long and add .0 or just . after the constants to mark them as floating point literals instead of integer literals."
I have the following code
int i, a, z;
i = 2343243443;
a = 5464354324324324;
z = i * a;
cout << z << endl;
When these are multiplied it gives me -1431223188 which is not the answer. How can I make it give me the correct answer?
The result overflows the int (and also std::uint64_t)
You have to use some BigInt library.
As Jarod42 suggested is perfectly okay, but i am not sure whether overflow will take place or not ?
Try to store each and every digit of number in an array and after that multiply. You will definitely get the correct answer.
For more detail how to multiply using array follow this post http://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial
Use pan paper approach as we used in 2nd standard.
Store two numbers in two different array in reverse order. And take ans array as size of (arr1.size + arr2.size).And also initilize ans array to zero.
In your case
arr1[10]={3,4,4,3,4,2,3,4,3,2},
arr2[15]={4,2,3,4,2,3,,4,5,3,4,5,3,4,6,4,5};
for(int i=0;i<arr1_length;i++)
{
for(int j=0;j<arr2_length;j++)
{
ans[i+j]+=arr1[i]*arr2[j];
ans[i+j+1]=ans[i+j+1]+ans[i+j]/10;
ans[i+j]%=10;
}
}
Then ans array contain the result.Please print carefully ans array. it may contain leading zero.
ints only hold 32 bits. When the result of a multiplication is larger than 2^31 - 1, the result rolls over to a large negative value. Instead of using the int data type, use long long int, which holds 64 bits.
You should first try to use 64-bit numbers (long or better, unsigned long if everything is positive). With unsigned long you can operate between 0 and 18446744073709551615, with long between -9223372036854775808 and 9223372036854775807.
If it is not enough, then there is no easy solution, you have to perform your operation at software level using arrays of unsigned long for instance, and overload "<<" operator for display. But this is not that easy, and I guess you are a beginner (no offense) considering the question you asked.
If 64-bit representation is not enough for you, I think you should consider floating-point representation, especially "double". With double, you can represent numbers between about -10^308 and 10^308. You won't be able to have perfectly accurate computatiosn on very large number (the least significant digits won't be computed), but this should be a good-enough option for whatever you want to do here.
First, you have to make your value long longs
Second, you would want to add a (long long) in front of the multiplication. This is because when you have (ab) it returns an int therefore if you want it to return a long long you would want (long long)(ab).
I would like to elaborate on, and clarify Shravan Kumar's Answer using a full-fledged code. The code starts with a long integers a & b, which are multiplied using array, converted to a string and then back into the long int.
#include <iostream>
#include <string>
#include<algorithm>
using namespace std;
int main()
{
//Numbers to be multiplied
long a=111,b=100;
//Convert them to strings (or character array)
string arr1 = to_string(a), arr2 = to_string(b);
//Reverse them
reverse(arr1.begin(), arr1.end());
reverse(arr2.begin(), arr2.end());
//Getting size for final result, just to avoid dynamic size
int ans_size = arr1.size() + arr2.size();
//Declaring array to store final result
int ans[ans_size]={0};
//Multiplying
//In a reverse manner, just to avoid reversing strings explicitly
for(int i=0; i<arr1.size();i++)
{
for(int j=0; j<arr2.size();j++)
{
//Convert array elements (char -> int)
int p = (int)(arr1[i]) - '0';
int q = (int)(arr2[j]) - '0';
//Excerpt from Shravan's answer above
ans[i+j]+=p*q;
ans[i+j+1]=ans[i+j+1]+ans[i+j]/10;
ans[i+j]%=10;
}
}
//Declare array to store string form of final answer
string s="";
for(auto i=0;i<ans_size; ++i)
s += to_string(ans[i]);
reverse(s.begin(), s.end() );
//If last element is 0, it should be skipped
if(s[0] =='0')
{
string ss(s,1,s.size()-1);
s=ss;
}
//Final answer
cout<< s;
return 0;
}
Use float instead. It can go up to about 3.4028235 × 1038
// its may heplfull for you
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000
void reverse(char *from, char *to ){
int len=strlen(from);
int l;
for(l=0;l<len;l++)to[l]=from[len-l-1];
to[len]='\0';
}
void call_mult(char *first,char *sec,char *result){
char F[MAX],S[MAX],temp[MAX];
int f_len,s_len,f,s,r,t_len,hold,res;
f_len=strlen(first);
s_len=strlen(sec);
reverse(first,F);
reverse(sec,S);
t_len=f_len+s_len;
r=-1;
for(f=0;f<=t_len;f++)temp[f]='0';
temp[f]='\0';
for(s=0;s<s_len;s++){
hold=0;
for(f=0;f<f_len;f++){
res=(F[f]-'0')*(S[s]-'0') + hold+(temp[f+s]-'0');
temp[f+s]=res%10+'0';
hold=res/10;
if(f+s>r) r=f+s;
}
while(hold!=0){
res=hold+temp[f+s]-'0';
hold=res/10;
temp[f+s]=res%10+'0';
if(r<f+s) r=f+s;
f++;
}
}
for(;r>0 && temp[r]=='0';r--);
temp[r+1]='\0';
reverse(temp,result);
}
int main(){
char fir[MAX],sec[MAX],res[MAX];
while(scanf("%s%s",&fir,&sec)==2){
call_mult(fir,sec,res);
int len=strlen(res);
for(int i=0;i<len;i++)printf("%c",res[i]);
printf("\n");
}
return 0;
}
You can use queue data structure to find the product of two really big numbers with O(n*m). n and m are the number of digits in a number.
#include<iostream>
#include<cmath>
using namespace std;
double bisection(double errorVal, double userNum){
double upper=userNum, lower=0;
double mid=(lower+upper)/2.0;;
while(mid*mid!=userNum){
double mid=(lower+upper)/2.0;
if(mid*mid>userNum){
upper=mid;
} else {
lower=mid;
}
}
return mid;
}
int main(){
double errorVal=0, userNum=0;
std::cout<<"Please enter a number (larger than 0) to calculate its square root, and the desired margin of error."<<std::endl;
std::cin>>userNum>>errorVal;
bisection(errorVal,userNum);
std::cout<<"The calculated result is "<<bisection(errorVal,userNum)<<". The error is "<<abs(bisection(errorVal,userNum)-sqrt(userNum))<<"."<<std::endl;
}
This is a program I have written to find the square root of any number inputted via the bisection method. I must be doing something wrong here because I am not getting any output once I enter the two input parameters, the process just gets stuck there.
I would also like to know how to properly implement errorVal, as to specify the margin of error allowed. Thanks.
The error value is used to fix any rounding inaccuracies which occur while doing floating point operations.
The following statement would seldom be true, therefor your loop is likely to continue for a long time.
while(mid*mid==userNum)
The usual way to compare two floating points after calculation is
fabs(x1-x2) < e //where, fabs retrieves the absolute value,
//x1,2 are the numbers to compare
//and e is the epsilon chosen.
So, fixing the error value, or commonly referred to as epsilon, would fix the loop as well.
double bisection(double errorVal, double userNum){
double upper=userNum, lower=0;
double mid=(lower+upper)/2.0;
//error val added
//** fabs(mid*mid - userNum) < errorVal is true if the numers are "equal"
//** and you want to run the loop as long as the are NOT "equal"
while(!(fabs(mid*mid - userNum) < errorVal)){
mid=(lower+upper)/2.0;
if(mid*mid>userNum){
upper=mid;
} else {
lower=mid;
}
}
return mid;
}
See:
http://www.cplusplus.com/reference/cmath/fabs/
https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
recently I bump into a problem while comparing a double in an if statement. I was trying to cout the number of whole numbers in a double. Being a beginner, I am not sure what gone wrong in my code.
This is my code:
#include <iostream>
using namespace std;
int main(){
int x=0;//convert double to int
long double Out;//Result
long double In=10;//Input double
//Loop Begin
while(In>0){
x=In;//convert double to int
Out= (x/In);//Out(test if whole number, will return 1)
//test for 1
////////////////
if(Out == 1 ){
cout<<"[Whole Number] ";
}
////////////////
//test end
cout<<"In :"<<In<<", ";
cout<<"X :"<<x<<", ";
cout<<"Out :"<<Out<<endl;
In-=0.1;//decrease to finish loop (eventually)
}
//Loop End
cin.get();
return 0;
}
This program will test and output the whole numbers in the double (In). I realized that the accuracy of the double was affecting the if statement which is why I can't get the "[Whole Number]" result. Although I found out that if I used (0.9999) in "if(Out >= 0.9999)" the comparison would work. But I am not sure of a solution, please help! Much appreciated!
Your while loop never stops , its a infinite loop . You are not doing anything with the value of "In" in the while loop hence it will always be greater than 0 ,therefore a infinite loop .
You should probably approach the problem more directly with modf:
double int_part, frac_part;
frac_part = std::modf(in, &int_part);
if (frac_part == 0) {
// int_part contains integer value.
} else {
// process the double non-integer floating point value.
}
Your code works perfectly fine. If you subtract 0.1 from 10.0, then chances are that the result is not an integer due to rounding errors, and your code tells you exactly that. The code isn't wrong, your expectations are wrong.
if (Out >= 0.9999)
is obviously not a solution, because it will always be true if In >= 10000.0.
Do to the way floating point numbers are converted to binary representation by the computer they are inherently inaccurate and thus make logical comparisons somewhat challenging (http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems). When performing such comparisons to floating point numbers you typically will do so utilizing an epsilon constant (http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm) that represents the maximum acceptable error in the comparison. In your case you need to select a suitable value for epsilon (say .000001). Then change your comparison to:
if(abs(out - 1) < epsilon){ //Take the difference between out and 1
cout<<"[Whole Number]"; //If it is "close enough" print to console
}
I am more of a Java guy but I believe you will need #include stdlib.h to utilize the abs() function.
Hope that helps!
Try using the modulus operator: http://www.cprogramming.com/tutorial/modulus.html
Something like if(In % 1 == 0) should work.
I have the following code
int i, a, z;
i = 2343243443;
a = 5464354324324324;
z = i * a;
cout << z << endl;
When these are multiplied it gives me -1431223188 which is not the answer. How can I make it give me the correct answer?
The result overflows the int (and also std::uint64_t)
You have to use some BigInt library.
As Jarod42 suggested is perfectly okay, but i am not sure whether overflow will take place or not ?
Try to store each and every digit of number in an array and after that multiply. You will definitely get the correct answer.
For more detail how to multiply using array follow this post http://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial
Use pan paper approach as we used in 2nd standard.
Store two numbers in two different array in reverse order. And take ans array as size of (arr1.size + arr2.size).And also initilize ans array to zero.
In your case
arr1[10]={3,4,4,3,4,2,3,4,3,2},
arr2[15]={4,2,3,4,2,3,,4,5,3,4,5,3,4,6,4,5};
for(int i=0;i<arr1_length;i++)
{
for(int j=0;j<arr2_length;j++)
{
ans[i+j]+=arr1[i]*arr2[j];
ans[i+j+1]=ans[i+j+1]+ans[i+j]/10;
ans[i+j]%=10;
}
}
Then ans array contain the result.Please print carefully ans array. it may contain leading zero.
ints only hold 32 bits. When the result of a multiplication is larger than 2^31 - 1, the result rolls over to a large negative value. Instead of using the int data type, use long long int, which holds 64 bits.
You should first try to use 64-bit numbers (long or better, unsigned long if everything is positive). With unsigned long you can operate between 0 and 18446744073709551615, with long between -9223372036854775808 and 9223372036854775807.
If it is not enough, then there is no easy solution, you have to perform your operation at software level using arrays of unsigned long for instance, and overload "<<" operator for display. But this is not that easy, and I guess you are a beginner (no offense) considering the question you asked.
If 64-bit representation is not enough for you, I think you should consider floating-point representation, especially "double". With double, you can represent numbers between about -10^308 and 10^308. You won't be able to have perfectly accurate computatiosn on very large number (the least significant digits won't be computed), but this should be a good-enough option for whatever you want to do here.
First, you have to make your value long longs
Second, you would want to add a (long long) in front of the multiplication. This is because when you have (ab) it returns an int therefore if you want it to return a long long you would want (long long)(ab).
I would like to elaborate on, and clarify Shravan Kumar's Answer using a full-fledged code. The code starts with a long integers a & b, which are multiplied using array, converted to a string and then back into the long int.
#include <iostream>
#include <string>
#include<algorithm>
using namespace std;
int main()
{
//Numbers to be multiplied
long a=111,b=100;
//Convert them to strings (or character array)
string arr1 = to_string(a), arr2 = to_string(b);
//Reverse them
reverse(arr1.begin(), arr1.end());
reverse(arr2.begin(), arr2.end());
//Getting size for final result, just to avoid dynamic size
int ans_size = arr1.size() + arr2.size();
//Declaring array to store final result
int ans[ans_size]={0};
//Multiplying
//In a reverse manner, just to avoid reversing strings explicitly
for(int i=0; i<arr1.size();i++)
{
for(int j=0; j<arr2.size();j++)
{
//Convert array elements (char -> int)
int p = (int)(arr1[i]) - '0';
int q = (int)(arr2[j]) - '0';
//Excerpt from Shravan's answer above
ans[i+j]+=p*q;
ans[i+j+1]=ans[i+j+1]+ans[i+j]/10;
ans[i+j]%=10;
}
}
//Declare array to store string form of final answer
string s="";
for(auto i=0;i<ans_size; ++i)
s += to_string(ans[i]);
reverse(s.begin(), s.end() );
//If last element is 0, it should be skipped
if(s[0] =='0')
{
string ss(s,1,s.size()-1);
s=ss;
}
//Final answer
cout<< s;
return 0;
}
Use float instead. It can go up to about 3.4028235 × 1038
// its may heplfull for you
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX 1000
void reverse(char *from, char *to ){
int len=strlen(from);
int l;
for(l=0;l<len;l++)to[l]=from[len-l-1];
to[len]='\0';
}
void call_mult(char *first,char *sec,char *result){
char F[MAX],S[MAX],temp[MAX];
int f_len,s_len,f,s,r,t_len,hold,res;
f_len=strlen(first);
s_len=strlen(sec);
reverse(first,F);
reverse(sec,S);
t_len=f_len+s_len;
r=-1;
for(f=0;f<=t_len;f++)temp[f]='0';
temp[f]='\0';
for(s=0;s<s_len;s++){
hold=0;
for(f=0;f<f_len;f++){
res=(F[f]-'0')*(S[s]-'0') + hold+(temp[f+s]-'0');
temp[f+s]=res%10+'0';
hold=res/10;
if(f+s>r) r=f+s;
}
while(hold!=0){
res=hold+temp[f+s]-'0';
hold=res/10;
temp[f+s]=res%10+'0';
if(r<f+s) r=f+s;
f++;
}
}
for(;r>0 && temp[r]=='0';r--);
temp[r+1]='\0';
reverse(temp,result);
}
int main(){
char fir[MAX],sec[MAX],res[MAX];
while(scanf("%s%s",&fir,&sec)==2){
call_mult(fir,sec,res);
int len=strlen(res);
for(int i=0;i<len;i++)printf("%c",res[i]);
printf("\n");
}
return 0;
}
You can use queue data structure to find the product of two really big numbers with O(n*m). n and m are the number of digits in a number.