Why do I get a SIGFPE?

SIGFPE may occur due to

  1. division by zero
  2. modulo operation by zero
  3. integer overflow (when the value you are trying to store exceeds the range) - trying using a bigger data type like long.

SIFFPE is very easy to debug.

14 Likes

Why am i getting runtime here??

include

include

long long int a,b,c=0;
int calc();
int mod();
int main()

{

int t;

scanf("%d",&t);
while(t--)    
{
 scanf("%lld%lld",&a,&b);

 printf("%lld\n",calc());

}
return 0;

}
int mod(){

 if(a>b)
 {
      if(a%b==0)
      {
           return 1;
      }
      return 0;

 }
 else {
      if(b%a==0)
      {
           return 1;
      }
      return 0;
 }

}
int calc()
{
// printf(“a”);
if(mod()==0)
{
while(a!=b)
{
a*=2;
c++;

       }
       return c;
  }
  while(mod())
  {
      if(a%2==0)
      {
           a/=2;
           c++;
      }
      else{ a=(a-1)/2;
       c++;
  }
  }}

1 Like

It is a signal representing core dumped means if you do any invalid operation the you will get “SIGFPE”
error. In case floating point error means division by 0 cause this error so please check your code and make sure that you are not operating this type of operation means make sure that you are not dividing by 0

#include<stdio.h>
#include<math.h>

int gcd(int n1, int n2)
{
int min,i, hcf;

min=(n1>n2)?n2:n1;

for(i=min; i>=1; --i)
{
    if(n1%i==0 && n2%i==0)
    {
        hcf=i;
        break;
    }
}
// printf("%d\n",hcf);
return hcf;

}

int fd(int a, int b)
{
int count, i, j, total;
count=total=0;
for(i=a; i<=b; i++)
{
count=0;
for(j=1; j<i; j++)
if(gcd(i,j)==1)
count++;

    if(i%count==0)
        total++;
}
//printf("%d\n",total);
return total;

}

int main()
{
long long a,j, b,i=0,n, yes;
scanf("%lld", &n);
while(n–)
{
scanf("%lld%lld", &a,&b);

    yes=fd(a,b);
    printf("%lld\n",yes);
}
 return 0;

}
why i m getting sigfpe error in this program…can any1 help??

#include<stdio.h>
main()
{
long int a,b,c,d,e,j,I,n;
scanf("%ld",&a);
for(I=0;I<a;I++)
{
scanf("%ld%ld",&b,&c);
e=bc;
for(j=e;j>1;j–)
{
n=j
j;
if(e%n==0)
{
d=e/n;
printf("%ld",d);
break;
}
}
}
}I am getting sigfpe.Where is the problem in my code .Can any one to it???

#include
using namespace std;
int main()
{
long long int n;
cin>>n;
long long int a[n+1],mul[n+1]={1};
mul[0]=1;
for(long long int i=1;i<=n;i++)
{
cin>>a[i];
mul[i]=1;
}
for(long long int i=1;i<=n;i++)
{
mul[i]=mul[i-1]*a[i];
}

long long int t;
cin>>t;
while(t–)
{
long long int re=1,li,ri,mi;
cin>>li>>ri>>mi;
re=(mul[ri])/(mul[li-1]);
if(re>=mi)
{
re=re%mi;
}
cout<<re<<endl;
}
return 0;
}
iam getting run time error what is the problem

Why am I getting RUNTIME(SIGFPE) error? can anyone help??

Link to my code: CodeChef: Practical coding for everyone
Thanks in advance!

Note:- In C++ if you fail to typecast the variables properly , you may end up facing a SIGFPE . I faced one as I was storing an int value in a vector of long long int !

3 Likes

use if conditions t ocheck whether anything results in 0 or not

why am i getting RUNTIME SIGFPE error?

link to my code: CodeChef: Practical coding for everyone

@ashishpm Bro keep dividing the multiplication as its increasing as it is causing Integer Overflow…you are doing whole multiplication at once which is causing To extend the long long range making it negative.

how to reduce time running?

#include
using namespace std;

int main() {
long n,a[10],i,j,rem,max=0;

 cin >> n;

for(i=0;i<n;i++)
{
    cin >> a[i];
}
if (n>10)
    return 0;
while(i<n)
{
    rem=a[i++]%a[j++];
    if (rem>max)
        max=rem;
    if (j==n)
        j=0;
}
cout<<"the result of the puzzle"<<max;

return 0;

}

```

#include <bits/stdc++.h>

using namespace std;

int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);

int t; cin >> t;
while (t--) {
	long long l, r, g;
	cin >> l >> r >> g;
    long long cnt = r / g - (l - 1) / g;
	cout << (cnt >= 0 ? cnt : r / g == 1) << "\n";
}
return 0;

}

I got SIGFPE in above code. When I change "\n" to endl, it become ok. But why ?
1 Like

#include<bits/stdc++.h>

using namespace std;

#define ll long long

#define I INT_MAX

vector primes;

int total(int i)

{

int j=0,done=0,cnt=0;

while(i>1)

{       

    if(!(i%primes[j]))

        {

            i/=primes[j];

            if(!done) {cnt++;done=1;}

            continue;

        }

    else

    {

        j++;

        done=0;

    }        

}

return cnt;

}

int main()

{

int j;

//finding primes

for(int i=2;i<400;i++)

{

    for(j=2;j*j<=i;j++)

    {

        if(i%j==0) break;

    }if(j*j>i) primes.push_back(i);

}

int t,n,a,b,k;

cin>>t;

while(t--)

{

    int cnt=0;

    cin>>a>>b>>k;

    for(int i=a;i<=b;i++)

    {

        if(total(i)==k) cnt++;

    }

    cout<<cnt<<endl;

}



return 0;

}

here’s my code. I cannot detect error in my code. error was SIGFPE

I tried this, ain’t getting any error. Can you show me a piece of code where we get error for using %1.

SIGFPE is sometimes also caused if use too much memory for a particular input.
It can be avoided by including corner cases.

SIGFPE
This is my code. i tried very hard to find out the problem but i am not able to do so …so if anyone can find out the problem in my code it will be really helpful

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,r;
cin>>n>>r;
int a[n],b[n];
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
}

    for(i=0;i<n;i++)
    {
        cin>>b[i];
    }
    vector<pair<int,int>> effective(n);
    for(i=0;i<n;i++)
    {
        effective.push_back({a[i]-b[i],a[i]});
    }
    sort(effective.begin(),effective.end());
    i=0;
    int count=0;
    int f,s;
    int instant_count=0;
    while(i<n)
    {
        f=effective[i].first;
        s=effective[i].second;
        if(r<s)
        {
            break;
        }
        
        if(s<=r)
        {
            count=count+r/s;
            instant_count=r/s;
            r=r%s+instant_count*(s-f);
        }
        if(r>=s)
        {
            i--;
        }
        
        i++;
        
    }
    cout<<count<<"\n";
    
    
}

}

if you miss to take values in code like"cin>>a>>c"etc .it happens

How many solutions to one error. Thanks, I think it will be useful to me

Bro factorial n-3 when n=3 makes it factorial (0!) which is throwing SIGFPE error