Core Dumped Due to Floating Point Exception

To locate the source of a crash, you have two options: use a debugger to step through the code or set breakpoints, or add temporary printf() statements throughout the code to identify the location of the problem. In most cases, a divide by zero error will result in a floating point exception and a core dump.

Question:

Hi everyone, I am attempting to implement the Trapezoidal Rule using

parallel

and

C++

. Although it compiles without errors, when I try to run it, I receive the error message “Floating point
exception (core dumped
).” I used the following command to compile the code: “g++ -Wall assignmemt.cpp -o output -lpthread”. When I attempted to run the code with the command “./output”, I received the message “usage: ./output nn is the number of terms and should be >= 1”. When I tried to run it again with the command “./output 3 5”, I got the error message “Floating point
exception (core
dumped)”.

   
#include 
#include 
#include 
#include 
const int MAX_THREADS = 1024;
long thread_count;
long long n;
double sum;
pthread_mutex_t mutex;
#define f(x) 1/(1+pow(x,2))
void Usage(char* prog_name) {
   fprintf(stderr, "usage: %s   n", prog_name);
   fprintf(stderr, "   n is the number of terms and should be >= 1n");
   exit(0);
}  
void Get_args(int argc, char* argv[]) {
   if (argc != 3) Usage(argv[0]);
   thread_count = strtol(argv[1], NULL, 10);  
   if (thread_count <= 0 || thread_count > MAX_THREADS) Usage(argv[0]);
   n = strtoll(argv[2], NULL, 10);
   if (n <= 0) Usage(argv[0]);
}  
void* Trapezoidal_Rule(void* rank) {
  long long  integration=0 , stepSize, k;
  long subInterval = (long) rank;
  int i;
  stepSize = (1 - 0)/subInterval;
  integration = f(0) + f(1);
  
  for(i=1; i<= subInterval-1; i++)
 {
  k = 0 + i*stepSize;
  integration = integration + 2 * (f(k));
 }
  pthread_mutex_lock(&mutex);
  integration = integration * stepSize/2;
  pthread_mutex_unlock(&mutex);
  return NULL;
}
int main(int argc, char* argv[]) {
  long       thread;  
  pthread_t* thread_handles;
  
  
  
  Get_args(argc, argv);
  
  thread_handles = (pthread_t *) malloc(thread_count*sizeof(pthread_t));
  
  
  
  for (thread = 0; thread < thread_count; thread++)
    pthread_create(&thread_handles[thread], NULL, Trapezoidal_Rule, (void*)thread);
  
  for (thread = 0; thread < thread_count; thread++)
    pthread_join(thread_handles[thread], NULL);
  
  
  
  
  
  printf("   integration is = %.15f n", sum);
  
  pthread_mutex_destroy(&mutex);
  free(thread_handles);
  return 0;
} 


Solution:


Most of the time, when encountering floating point
exception: core dumps
, the issue is a division by zero. I can’t recall experiencing this problem for any other reason.

Frequently Asked Questions

Posted in Uncategorized