#include <iostream>
#include <string.h>
using namespace std;
int testMul(int a, int b)
{
return !a || a * b / a == b;
}
int main()
{
int a, b;
while(cin >> a >> b) cout << testMul(a, b) << endl;
return 0;
}
When the input is
-1 -2147483648
, there is a runtime error.Divided by 0?
I think it is amazing.
By the way, has anyone concluded a way that can judge if there is a overflow in multiplication?
When you multiply -1 and -2147483648 you get signed overflow, which is an undefined behaviour in C++, the result can be any, up to your computer catching fire. So getting division by zero is actually not that bad of an outcome.
Related
I made a program for codechef and its wrong apparantly (although all tests have been positive). The code is:
#include <iostream>
using namespace std;
int g (int a,int b){
return b == 0 ? a : g(b, a % b);
}
int l (int a, int b){
return (a*b)/(g(a,b));
}
int main() {
int n;
cin >> n;
int a[n],b[n];
for (int x = 0;x<n;x++){
cin >> a[x] >> b[x];
}
for (int x = 0;x<n;x++){
cout << g(a[x],b[x]) << " "<< l(a[x],b[x]) << endl;
}
return 0;
}
Codechef won't tell me what integers dont work, and im pretty sure my gcd function is legit.
Since gcd is properly defined as the largest non-negative common divisor, you can save yourself the annoying details of signed division, e.g.,
static unsigned gcd (unsigned a, unsigned b)
{
/* additional iteration if (a < b) : */
for (unsigned t = 0; (t = b) != 0; a = t)
b = a % b;
return a;
}
Likewise for lcm; but the problem here is that (a*b) may overflow. So if you have two large (signed) int values that are co-prime, say: 2147483647 and 2147483629, then gcd(a,b) == 1, and (a*b)/g overflows.
A reasonable assumption on most platforms is that unsigned long long is twice the width of unsigned - although strictly speaking, it doesn't have to be. This is also a good reason to use exact types like [u]int32_t and [u]int64_t.
One thing you can be sure of is that a/g or b/g will not cause any issues. So a possible implementation might be:
static unsigned long long lcm (unsigned a, unsigned b)
{
return ((unsigned long long) a) * (b / gcd(a, b)));
}
If your test values are 'positive' (which is what I think you mean), you can cast them prior to (unsigned) prior to call. Better yet - replace all your int variables with unsigned int (though the loop variables are fine), and save yourself the trouble to begin with.
The program I'm coding should find the root of a given double.
The condition is: you're not allowed to use while and for loops. any kind of loops are not allowed.
Our professor said it's forbidden to use the stdlib function sqrt().
I started to code but it's still not working. hope anyone can help.
#include <cstdlib>
#include <iostream>
using namespace std;
double mysqrt(double a, double b, double c);
int main(int argc, char** argv) {
double dBegin{0};
double dOldroot{0};
double dNewroot{0};
double a{0};1
cin >> dBegin;
dOldroot = dBegin;
mysqrt(a, dOldroot, dNewroot);
cout << dNewroot;
return 0;
}
double mysqrt(double a, double b, double c) {
c = (b + (a / b)) / 2.0;
if (a != 8) {
c = mysqrt(a++, b, c);
}
return c;
}
I'm using Newton-Raphson's Method to find out the square-root of a given number num (in my code).
You may find this video link useful: Click Here. Using this algorithm i've solved this problem.
Here is my code.
#include <iostream>
using namespace std;
/* we are gonna use Newton-Raphson's method to find its square because
it converges quickly, even calculators use this algo. to find the sqr-root */
double find_sqrt(double x, int num, int count) {
if(count == 0)
return x;
double f_x = x*x - num;
double f_dx = 2*x;
double res = x - (f_x/f_dx);
x = find_sqrt(res, num, count -1);
return x;
}
int main() {
double num;
cin >> num;
/* Here 20 is the maximum number of times it will run and
num/2 is the random number send to the function between the range 1 to num */
cout << find_sqrt(num/2, num, 20);
return 0;
}
The result might not be much precise but it will always be almost near to the actual square-root of the number.
Reason: Floating-precision error. You must be knowing about this i believe.
You have junk 1 to cause compile error after double a{0};
Your code will do infinite recursion because the first argument won't updated. Using double for counter isn't also a good idea.
You are throwing away the value returned from mysqrt in main().
Using arguments as local variables without reading its value isn't a good idea.
Fixing these errors, your code will be like this:
#include <cstdlib>
#include <iostream>
using namespace std;
double mysqrt(int a, double b);
int main(int argc, char** argv) {
double dBegin{0};
double dOldroot{0};
double dNewroot{0};
int a{0};
cin >> dBegin;
dOldroot = dBegin;
dNewroot = mysqrt(a, dOldroot);
cout << dNewroot;
return 0;
}
double mysqrt(int a, double b) {
double c = (b + (a / b)) / 2.0;
if (a != 8) {
c = mysqrt(a + 1, b);
}
return c;
}
This code failed to calculate square root, but compiled and exited soon when ran.
Following is code for SPOJ GCD2. It's running well on my machine and Ideone, but getting runtime error (SIGFPE) on SPOJ. I have checked all the test cases also available at spojtoolkit.com.
I am unable to figure out why this code is showing runtime error (SIGFPE) on spoj. SIGFPE means Erroneous arithmetic operation.
Why is this code showing runtime error on SPOJ?
#include <bits/stdc++.h>
using namespace std;
int gcd(int x,int a)
{
if(a==0)
return x;
else
return gcd(a, x%a);
}
int getmod(string b,int a)
{
int n=b.size();
int d;
d= (b[0]-'0') % a;
for(int i=1; i!=n; i++)
{
d=d*10;
d=d + (b[i]-'0');
d= d % a;
}
return d;
}
int main()
{
int tc;
cin >> tc;
int a;
string b;
while(tc--)
{
cin >>a>>b;
int x=getmod(b,a);
cout << gcd(x,a)<<endl;
}
return 0;
}
int getmod(string b,int a)
{
int n=b.size();
int d;
d= (b[0]-'0') % a;
If a = 0, this will error, because % 0 is like dividing by zero. a can be zero according to the problem statement.
The error covers division by zero:
The SIGFPE signal reports a fatal arithmetic error. Although the name is derived from “floating-point exception”, this signal actually covers all arithmetic errors, including division by zero and overflow. If a program stores integer data in a location which is then used in a floating-point operation, this often causes an “invalid operation” exception, because the processor cannot recognize the data as a floating-point number.
You can fix it by checking if a == 0 and simply returning b as the answer in that case. Else call your current function as is.
#include <iostream>
using namespace std;
double calc(int a, int b);
int main()
{
int n1, n2;
cout << "Enter a number for a: ";
cin >> n1;
cout << "Enter a number for b: ";
cin >> n2;
cout << calc(n1, n2) << endl;
system("PAUSE");
return 0;
}
double calc(int a, int b)
{
double s;
s = (a) / ((sqrt(a / b)));
return s;
}
This program is meant to check whether the two integers are greater than zero. If it is it will calcualte the formula. Otherwise if one of the integers is zero or less than zero it will not return anything and exit the program.
My question here is that no matter what I input for a and b, i keep getting 1.#INF as the output and I have no idea why. I've checked the formula in a seperate program and it worked fine.
Any ideas?
Here, you are operating with int numbers:
s = (a) / ((sqrt(a / b)));
If a is less then b, then a/b (both are integers, remember, so the fractional part of the result will simply be lost) will be equal to 0, which leads to division by 0. You need to cast one of the numbers to double:
s = (a) / ((sqrt(static_cast<double>(a) / b)));
sqrt takes and returns a double. When you call it with integer arguments it will be converted in a double, and will thus get the value of infinity.
change your function signature to:
double calc(double a, double b);
and declare n1 and n2 as double.
You say that the function will exit the program when one of the integers are 0 or less, but where?
Try to check them like this:
Additionally, you should have a check whether a is greater than b
double calc(int a, int b)
{
double s;
if(a <= 0) exit(-1);
if(b <= 0) exit(-1);
if(a < b) exit(-1);
s = (a) / ((sqrt(a / b)));
return s;
}
You are having problems with infinity. For it use isinf. Here is some sample usage:
#include <stdio.h> /* printf */
#include <math.h> /* isinf, sqrt */
int main()
{
printf ("isinf(-1.0/0.0) : %d\n",isinf(-1.0/0.0));
printf ("isinf(sqrt(-1.0)): %d\n",isinf(sqrt(-1.0)));
return 0;
}
output:
isinf(-1.0/0.0) : 1 isinf(sqrt(-1.0): 0
I just wrote this code :
#include <iostream>
using namespace std;
unsigned long long int choose(unsigned long long int k, unsigned long long int n)
{
if (k==n or k==0)
{
return 1;
}
else
{
return (choose(n-1,k-1)+choose(n-1,k));
}
}
int main(){
cout << choose(3, 6);
}
but I got Run-Time Error, I think my problem is in my variables, I did debugging but I couldn't find the answer, why I got run time error?
Since your variables are unsigned, substracting 1 from them when they're already 0 will make them roll over to the max value.
When n reaches 0 and you then call choose(n-1,k), this'll happen and that's the source of the issue (stackoverflow).